Removed manual discounts code

This commit is contained in:
Mike Jolley 2017-08-18 10:36:10 +01:00
parent 12b3f94992
commit e99471c0cd
16 changed files with 25 additions and 826 deletions

View File

@ -234,7 +234,7 @@ jQuery( function ( $ ) {
$( '#woocommerce-order-items' )
.on( 'click', 'button.add-line-item', this.add_line_item )
.on( 'click', 'button.add-discount', this.add_discount )
.on( 'click', 'button.add-coupon', this.add_coupon )
.on( 'click', 'a.remove-coupon', this.remove_coupon )
.on( 'click', 'button.refund-items', this.refund_items )
.on( 'click', '.cancel-action', this.cancel )
@ -378,7 +378,7 @@ jQuery( function ( $ ) {
return false;
},
add_discount: function() {
add_coupon: function() {
var value = window.prompt( 'Enter a coupon code, percentage, or fixed discount amount.' );
if ( value != null ) {
@ -389,7 +389,7 @@ jQuery( function ( $ ) {
dataType : 'json',
order_id : woocommerce_admin_meta_boxes.post_id,
security : woocommerce_admin_meta_boxes.order_item_nonce,
discount : value
coupon : value
};
$.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) {

View File

@ -138,7 +138,6 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
'shipping_lines' => $this->get_items( 'shipping' ),
'fee_lines' => $this->get_items( 'fee' ),
'coupon_lines' => $this->get_items( 'coupon' ),
'discount_lines' => $this->get_items( 'discount' ),
)
);
}
@ -685,7 +684,6 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
'shipping' => 'shipping_lines',
'fee' => 'fee_lines',
'coupon' => 'coupon_lines',
'discount' => 'discount_lines',
) );
return isset( $type_to_group[ $type ] ) ? $type_to_group[ $type ] : '';
}
@ -837,8 +835,6 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
return 'tax_lines';
} elseif ( is_a( $item, 'WC_Order_Item_Coupon' ) ) {
return 'coupon_lines';
} elseif ( is_a( $item, 'WC_Order_Item_Discount' ) ) {
return 'discount_lines';
}
return apply_filters( 'woocommerce_get_items_key', '', $item );
}
@ -890,35 +886,6 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
}
}
/**
* Add a discount/coupon to this order and recalculate totals.
*
* @since 3.2.0
* @param string $discount Discount amount or coupon code.
*/
public function add_discount( $discount ) {
// Try to apply as a coupon first.
$coupon = new WC_Coupon( wc_format_coupon_code( $discount ) );
if ( $coupon->get_code() === wc_format_coupon_code( $discount ) && $coupon->is_valid() ) {
$this->apply_coupon( $coupon );
} else {
$item = new WC_Order_Item_Discount();
if ( strstr( $discount, '%' ) ) {
$item->set_amount( trim( $discount, '%' ) );
$item->set_discount_type( 'percent' );
$this->add_item( $item );
} elseif ( is_numeric( $discount ) && 0 < floatval( $discount ) ) {
$item->set_amount( floatval( $discount ) );
$item->set_discount_type( 'fixed' );
$this->add_item( $item );
}
$this->calculate_totals( true );
}
}
/**
* Apply a coupon to the order and recalculate totals.
*
@ -926,7 +893,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
* @param string|WC_Coupon $coupon Coupon code or object.
* @return true|WP_Error True if applied, error if not.
*/
protected function apply_coupon( $coupon ) {
public function apply_coupon( $coupon ) {
if ( ! is_a( $coupon, 'WC_Coupon' ) ) {
$code = wc_format_coupon_code( $coupon );
$coupon = new WC_Coupon( $code );
@ -945,7 +912,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
}
$discounts = new WC_Discounts( $this );
$applied = $discounts->apply_discount( $coupon );
$applied = $discounts->apply_coupon( $coupon );
if ( is_wp_error( $applied ) ) {
return $applied;
@ -1006,26 +973,6 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
$this->recalculate_coupons();
}
/**
* Calculate actual discount amounts for each discount row from line items.
*
* @since 3.2.0
*/
protected function calculate_discounts() {
$discounts = new WC_Discounts( $this );
// Re-calc manual discounts based on new line items.
foreach ( $this->get_items( 'discount' ) as $discount_key => $discount ) {
$result = $discounts->apply_discount( ( 'fixed' === $discount->get_discount_type() ? $discount->get_amount() : $discount->get_amount() . '%' ), $discount_key );
}
// Set discount totals.
foreach ( $discounts->get_manual_discounts() as $manual_discount_key => $manual_discount ) {
$item = $this->get_item( $manual_discount_key, false );
$item->set_total( wc_remove_number_precision( $manual_discount->get_discount_total() ) * -1 );
}
}
/**
* Apply all coupons in this order again to all line items.
*
@ -1293,7 +1240,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
}
// Trigger tax recalculation for all items.
foreach ( $this->get_items( array( 'line_item', 'fee', 'discount' ) ) as $item_id => $item ) {
foreach ( $this->get_items( array( 'line_item', 'fee' ) ) as $item_id => $item ) {
$item->calculate_taxes( $calculate_tax_for );
}
@ -1313,7 +1260,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
$existing_taxes = $this->get_taxes();
$saved_rate_ids = array();
foreach ( $this->get_items( array( 'line_item', 'fee', 'discount' ) ) as $item_id => $item ) {
foreach ( $this->get_items( array( 'line_item', 'fee' ) ) as $item_id => $item ) {
$taxes = $item->get_taxes();
foreach ( $taxes['total'] as $tax_rate_id => $tax ) {
$cart_taxes[ $tax_rate_id ] = isset( $cart_taxes[ $tax_rate_id ] ) ? $cart_taxes[ $tax_rate_id ] + (float) $tax : (float) $tax;
@ -1368,8 +1315,6 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
$cart_total = 0;
$fee_total = 0;
$shipping_total = 0;
$discount_total = 0;
$discount_total_tax = 0;
$cart_subtotal_tax = 0;
$cart_total_tax = 0;
@ -1391,13 +1336,6 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
$this->set_shipping_total( $shipping_total );
// Calculate manual discounts.
$this->calculate_discounts();
foreach ( $this->get_items( 'discount' ) as $item ) {
$discount_total += $item->get_total() * -1;
}
// Calculate taxes for items, shipping, discounts.
if ( $and_taxes ) {
$this->calculate_taxes();
@ -1409,13 +1347,9 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
$cart_total_tax += $item->get_total_tax();
}
foreach ( $this->get_items( 'discount' ) as $item ) {
$discount_total_tax += $item->get_total_tax() * -1;
}
$this->set_discount_total( $cart_subtotal - $cart_total + $discount_total );
$this->set_discount_tax( $cart_subtotal_tax - $cart_total_tax + $discount_total_tax );
$this->set_total( round( $cart_total - $discount_total + $fee_total + $this->get_shipping_total() + $this->get_cart_tax() + $this->get_shipping_tax(), wc_get_price_decimals() ) );
$this->set_discount_total( $cart_subtotal - $cart_total );
$this->set_discount_tax( $cart_subtotal_tax - $cart_total_tax );
$this->set_total( round( $cart_total + $fee_total + $this->get_shipping_total() + $this->get_cart_tax() + $this->get_shipping_tax(), wc_get_price_decimals() ) );
$this->save();
return $this->get_total();

View File

@ -1,69 +0,0 @@
<?php
/**
* Shows an order item discount.
*
* @since 3.2.0
* @var object $item The item being displayed
* @var int $item_id The id of the item being displayed
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<tr class="discount <?php echo ( ! empty( $class ) ) ? $class : ''; ?>" data-order_item_id="<?php echo esc_attr( $item_id ); ?>">
<td class="thumb"><div></div></td>
<td class="name">
<div class="view">
<?php
/* translators: %1$s: shipping method, %2$s: Discount amount. */
echo wp_kses_post( sprintf( __( '%1$s &mdash; %2$s', 'woocomerce' ), $item->get_name() ? $item->get_name() : __( 'Discount', 'woocommerce' ), 'fixed' === $item->get_discount_type() ? wc_price( $item->get_amount() ) : $item->get_amount() . '%' ) );
?>
</div>
<div class="edit" style="display: none;">
<input type="text" placeholder="<?php esc_attr_e( 'Discount name', 'woocommerce' ); ?>" name="order_item_name[<?php echo absint( $item_id ); ?>]" value="<?php echo ( $item->get_name() ) ? esc_attr( $item->get_name() ) : ''; ?>" />
<input type="hidden" class="order_item_id" name="order_item_id[]" value="<?php echo esc_attr( $item_id ); ?>" />
</div>
</td>
<td class="item_cost" width="1%">&nbsp;</td>
<td class="quantity" width="1%">&nbsp;</td>
<td class="line_cost" width="1%">
<?php echo wc_price( $item->get_total() ); ?>
</td>
<?php
if ( ( $tax_data = $item->get_taxes() ) && wc_tax_enabled() ) {
foreach ( $order_taxes as $tax_item ) {
$tax_item_id = $tax_item->get_rate_id();
$tax_item_total = isset( $tax_data['total'][ $tax_item_id ] ) ? $tax_data['total'][ $tax_item_id ] : '';
?>
<td class="line_tax" width="1%">
<div class="view">
<?php
echo ( '' !== $tax_item_total ) ? wc_price( wc_round_tax_total( $tax_item_total ), array( 'currency' => $order->get_currency() ) ) : '&ndash;';
if ( $refunded = $order->get_tax_refunded_for_item( $item_id, $tax_item_id, 'fee' ) ) {
echo '<small class="refunded">-' . wc_price( $refunded, array( 'currency' => $order->get_currency() ) ) . '</small>';
}
?>
</div>
<div class="edit" style="display: none;">
<input type="text" name="line_tax[<?php echo absint( $item_id ); ?>][<?php echo esc_attr( $tax_item_id ); ?>]" placeholder="<?php echo wc_format_localized_price( 0 ); ?>" value="<?php echo ( isset( $tax_item_total ) ) ? esc_attr( wc_format_localized_price( $tax_item_total ) ) : ''; ?>" class="line_tax wc_input_price" />
</div>
<div class="refund" style="display: none;">
<input type="text" name="refund_line_tax[<?php echo absint( $item_id ); ?>][<?php echo esc_attr( $tax_item_id ); ?>]" placeholder="<?php echo wc_format_localized_price( 0 ); ?>" class="refund_line_tax wc_input_price" data-tax_id="<?php echo esc_attr( $tax_item_id ); ?>" />
</div>
</td>
<?php
}
}
?>
<td class="wc-order-edit-line-item">
<?php if ( $order->is_editable() ) : ?>
<div class="wc-order-edit-line-item-actions">
<a class="edit-order-item" href="#"></a><a class="delete-order-item" href="#"></a>
</div>
<?php endif; ?>
</td>
</tr>

View File

@ -80,14 +80,6 @@ if ( wc_tax_enabled() ) {
do_action( 'woocommerce_admin_order_items_after_fees', $order->get_id() );
?>
</tbody>
<tbody id="order_discount_line_items">
<?php
foreach ( $discounts as $item_id => $item ) {
include( 'html-order-discount.php' );
}
do_action( 'woocommerce_admin_order_items_after_discounts', $order->get_id() );
?>
</tbody>
<tbody id="order_refunds">
<?php
if ( $refunds = $order->get_refunds() ) {
@ -174,10 +166,7 @@ if ( wc_tax_enabled() ) {
<p class="add-items">
<?php if ( $order->is_editable() ) : ?>
<button type="button" class="button add-line-item"><?php _e( 'Add item(s)', 'woocommerce' ); ?></button>
<?php if ( wc_tax_enabled() ) : ?>
<button type="button" class="button add-order-tax"><?php _e( 'Add tax', 'woocommerce' ); ?></button>
<?php endif; ?>
<button type="button" class="button add-discount"><?php _e( 'Apply discount', 'woocommerce' ); ?></button>
<button type="button" class="button add-coupon"><?php _e( 'Apply coupon', 'woocommerce' ); ?></button>
<?php else : ?>
<span class="description"><?php echo wc_help_tip( __( 'To edit this order change the status back to "Pending"', 'woocommerce' ) ); ?> <?php _e( 'This order is no longer editable.', 'woocommerce' ); ?></span>
<?php endif; ?>
@ -197,6 +186,9 @@ if ( wc_tax_enabled() ) {
<button type="button" class="button add-order-item"><?php _e( 'Add product(s)', 'woocommerce' ); ?></button>
<button type="button" class="button add-order-fee"><?php _e( 'Add fee', 'woocommerce' ); ?></button>
<button type="button" class="button add-order-shipping"><?php _e( 'Add shipping cost', 'woocommerce' ); ?></button>
<?php if ( wc_tax_enabled() ) : ?>
<button type="button" class="button add-order-tax"><?php _e( 'Add tax', 'woocommerce' ); ?></button>
<?php endif; ?>
<?php
// allow adding custom buttons
do_action( 'woocommerce_order_item_add_line_buttons', $order );

View File

@ -907,7 +907,7 @@ class WC_AJAX {
$order_id = absint( $_POST['order_id'] );
$order = wc_get_order( $order_id );
$order->add_discount( wc_clean( $_POST['discount'] ) );
$order->apply_coupon( wc_clean( $_POST['coupon'] ) );
ob_start();
include( 'admin/meta-boxes/views/html-order-items.php' );

View File

@ -36,7 +36,6 @@ class WC_Data_Store {
'order-refund' => 'WC_Order_Refund_Data_Store_CPT',
'order-item' => 'WC_Order_Item_Data_Store',
'order-item-coupon' => 'WC_Order_Item_Coupon_Data_Store',
'order-item-discount' => 'WC_Order_Item_Discount_Data_Store',
'order-item-fee' => 'WC_Order_Item_Fee_Data_Store',
'order-item-product' => 'WC_Order_Item_Product_Data_Store',
'order-item-shipping' => 'WC_Order_Item_Shipping_Data_Store',

View File

@ -1,82 +0,0 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* A discount.
*
* Represents a fixed, percent or coupon based discount calculated by WC_Discounts class.
*
* @author Automattic
* @package WooCommerce/Classes
* @version 3.2.0
* @since 3.2.0
*/
class WC_Discount {
/**
* Data array, with defaults.
*
* @var array
*/
protected $data = array(
'amount' => 0, // Discount amount.
'discount_type' => 'fixed', // Fixed, percent, or coupon.
'discount_total' => 0,
);
/**
* Get discount amount.
*
* @return int
*/
public function get_amount() {
return $this->data['amount'];
}
/**
* Discount amount - either fixed or percentage.
*
* @param string $raw_amount Amount discount gives.
*/
public function set_amount( $raw_amount ) {
$this->data['amount'] = wc_format_decimal( $raw_amount );
}
/**
* Get discount type.
*
* @return string
*/
public function get_discount_type() {
return $this->data['discount_type'];
}
/**
* Set discount type.
*
* @param string $discount_type Type of discount.
*/
public function set_discount_type( $discount_type ) {
$this->data['discount_type'] = $discount_type;
}
/**
* Get discount total.
*
* @return int
*/
public function get_discount_total() {
return $this->data['discount_total'];
}
/**
* Discount total.
*
* @param string $total Total discount applied.
*/
public function set_discount_total( $total ) {
$this->data['discount_total'] = wc_format_decimal( $total );
}
}

View File

@ -24,20 +24,6 @@ class WC_Discounts {
*/
protected $items = array();
/**
* Stores fee total from cart/order. Manual discounts can discount this.
*
* @var int
*/
protected $fee_total = 0;
/**
* Stores shipping total from cart/order. Manual discounts can discount this.
*
* @var int
*/
protected $shipping_total = 0;
/**
* An array of discounts which have been applied to items.
*
@ -45,13 +31,6 @@ class WC_Discounts {
*/
protected $discounts = array();
/**
* An array of applied WC_Discount objects.
*
* @var array
*/
protected $manual_discounts = array();
/**
* Constructor.
*
@ -72,7 +51,7 @@ class WC_Discounts {
* @param array $cart Cart object.
*/
public function set_items_from_cart( $cart ) {
$this->items = $this->discounts = $this->manual_discounts = array();
$this->items = $this->discounts = array();
if ( ! is_a( $cart, 'WC_Cart' ) ) {
return;
@ -98,8 +77,7 @@ class WC_Discounts {
* @param array $order Cart object.
*/
public function set_items_from_order( $order ) {
$this->items = $this->discounts = $this->manual_discounts = array();
$this->fee_total = $this->shipping_total = 0;
$this->items = $this->discounts = array();
if ( ! is_a( $order, 'WC_Order' ) ) {
return;
@ -116,12 +94,6 @@ class WC_Discounts {
}
uasort( $this->items, array( $this, 'sort_by_price' ) );
foreach ( $order->get_fees() as $item ) {
$this->fee_total += wc_add_number_precision( $item->get_total() );
}
$this->shipping_total = wc_add_number_precision( $order->get_shipping_total() );
}
/**
@ -156,11 +128,6 @@ class WC_Discounts {
*/
public function get_discounts( $in_cents = false ) {
$discounts = $this->discounts;
foreach ( $this->get_manual_discounts() as $manual_discount_key => $manual_discount ) {
$discounts[ $manual_discount_key ] = $manual_discount->get_discount_total();
}
return $in_cents ? $discounts : wc_remove_number_precision_deep( $discounts );
}
@ -197,16 +164,6 @@ class WC_Discounts {
return $in_cents ? $coupon_discount_totals : wc_remove_number_precision_deep( $coupon_discount_totals );
}
/**
* Get an array of manual discounts which have been applied.
*
* @since 3.2.0
* @return WC_Discount[]
*/
public function get_manual_discounts() {
return $this->manual_discounts;
}
/**
* Get discounted price of an item without precision.
*
@ -229,96 +186,6 @@ class WC_Discounts {
return absint( $item->price - $this->get_discount( $item->key, true ) );
}
/**
* Get total remaining after discounts.
*
* @since 3.2.0
* @return int
*/
protected function get_total_after_discounts() {
$total_to_discount = 0;
// Sum line item costs.
foreach ( $this->items as $item ) {
$total_to_discount += $this->get_discounted_price_in_cents( $item );
}
// Manual discounts can also discount shipping and fees.
$total_to_discount += $this->shipping_total + $this->fee_total;
// Remove existing discount amounts.
foreach ( $this->manual_discounts as $key => $value ) {
$total_to_discount = $total_to_discount - $value->get_discount_total();
}
return $total_to_discount;
}
/**
* Generate a unique ID for a discount.
*
* @param WC_Discount $discount Discount object.
* @return string
*/
protected function generate_discount_id( $discount ) {
$discount_id = '';
$index = 1;
while ( ! $discount_id ) {
$discount_id = 'discount-' . $discount->get_amount() . ( 'percent' === $discount->get_discount_type() ? '%' : '' );
if ( 1 < $index ) {
$discount_id .= '-' . $index;
}
if ( isset( $this->manual_discounts[ $discount_id ] ) ) {
$index ++;
$discount_id = '';
}
}
return $discount_id;
}
/**
* Apply a discount to all items.
*
* @param string|object $raw_discount Accepts a string (fixed or percent discounts), or WC_Coupon object.
* @param string $discount_id Optional ID for the discount. Generated from discount or coupon code if not defined.
* @return bool|WP_Error True if applied or WP_Error instance in failure.
*/
public function apply_discount( $raw_discount, $discount_id = null ) {
if ( is_a( $raw_discount, 'WC_Coupon' ) ) {
return $this->apply_coupon( $raw_discount );
}
$discount = new WC_Discount;
if ( strstr( $raw_discount, '%' ) ) {
$discount->set_discount_type( 'percent' );
$discount->set_amount( trim( $raw_discount, '%' ) );
} elseif ( is_numeric( $raw_discount ) && 0 < floatval( $raw_discount ) ) {
$discount->set_discount_type( 'fixed' );
$discount->set_amount( wc_add_number_precision( floatval( $raw_discount ) ) );
}
if ( ! $discount->get_amount() ) {
return new WP_Error( 'invalid_discount', __( 'Invalid discount', 'woocommerce' ) );
}
$total_to_discount = $this->get_total_after_discounts();
if ( 'percent' === $discount->get_discount_type() ) {
$discount->set_discount_total( $discount->get_amount() * ( $total_to_discount / 100 ) );
} else {
$discount->set_discount_total( min( $discount->get_amount(), $total_to_discount ) );
}
$discount_id = $discount_id ? $discount_id : $this->generate_discount_id( $discount );
$this->manual_discounts[ $discount_id ] = $discount;
return true;
}
/**
* Apply a discount to all items using a coupon.
*

View File

@ -89,9 +89,6 @@ class WC_Order_Factory {
case 'tax' :
$classname = 'WC_Order_Item_Tax';
break;
case 'discount' :
$classname = 'WC_Order_Item_Discount';
break;
}
$classname = apply_filters( 'woocommerce_get_order_item_classname', $classname, $item_type, $id );

View File

@ -1,223 +0,0 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Order Line Item (discount).
*
* @version 3.2.0
* @since 3.2.0
* @package WooCommerce/Classes
* @author WooCommerce
*/
class WC_Order_Item_Discount extends WC_Order_Item {
/**
* Data array.
*
* @var array
*/
protected $extra_data = array(
'amount' => 0, // Discount amount.
'discount_type' => 'fixed', // Fixed or percent type.
'total' => '',
'total_tax' => '',
'taxes' => array(
'total' => array(),
),
);
/**
* Get item costs grouped by tax class.
*
* @since 3.2.0
* @param WC_Order $order Order object.
* @return array
*/
protected function get_tax_class_costs( $order ) {
$tax_classes = array_fill_keys( $order->get_items_tax_classes(), 0 );
$tax_classes['non-taxable'] = 0;
foreach ( $order->get_items( array( 'line_item', 'fee' ) ) as $item ) {
if ( 'taxable' === $item->get_tax_status() ) {
$tax_classes[ $item->get_tax_class() ] += $item->get_total();
} else {
$tax_classes['non-taxable'] += $item->get_total();
}
}
foreach ( $order->get_items( array( 'shipping' ) ) as $item ) {
if ( 'taxable' === $item->get_tax_status() ) {
$class = 'inherit' === $item->get_tax_class() ? current( $order->get_items_tax_classes() ): $item->get_tax_class();
$tax_classes[ $class ] += $item->get_total();
} else {
$tax_classes['non-taxable'] += $item->get_total();
}
}
return $tax_classes;
}
/**
* Calculate item taxes.
*
* @since 3.2.0
* @param array $calculate_tax_for Location data to get taxes for. Required.
* @return bool True if taxes were calculated.
*/
public function calculate_taxes( $calculate_tax_for = array() ) {
if ( ! isset( $calculate_tax_for['country'], $calculate_tax_for['state'], $calculate_tax_for['postcode'], $calculate_tax_for['city'] ) ) {
return false;
}
if ( wc_tax_enabled() && ( $order = $this->get_order() ) ) {
// Apportion taxes to order items, shipping, and fees.
$order = $this->get_order();
$tax_class_costs = $this->get_tax_class_costs( $order );
$total_costs = array_sum( $tax_class_costs );
$discount_taxes = array();
if ( $total_costs ) {
foreach ( $tax_class_costs as $tax_class => $tax_class_cost ) {
if ( 'non-taxable' === $tax_class ) {
continue;
}
$proportion = $tax_class_cost / $total_costs;
$cart_discount_proportion = $this->get_total() * $proportion;
$discount_taxes = wc_array_merge_recursive_numeric( $discount_taxes, WC_Tax::calc_tax( $cart_discount_proportion, WC_Tax::get_rates( $tax_class ) ) );
}
}
$this->set_taxes( array( 'total' => $discount_taxes ) );
} else {
$this->set_taxes( false );
}
return true;
}
/*
|--------------------------------------------------------------------------
| Setters
|--------------------------------------------------------------------------
*/
/**
* Set amount.
*
* @param string $value Value to set.
*/
public function set_amount( $value ) {
$this->set_prop( 'amount', $value );
}
/**
* Set discount_type.
*
* @param string $value Value to set.
*/
public function set_discount_type( $value ) {
$this->set_prop( 'discount_type', $value );
}
/**
* Set total.
*
* @param string $value Value to set.
*/
public function set_total( $value ) {
$this->set_prop( 'total', wc_format_decimal( $value ) );
}
/**
* Set total tax.
*
* @param string $value Value to set.
*/
public function set_total_tax( $value ) {
$this->set_prop( 'total_tax', wc_format_decimal( $value ) );
}
/**
* Set taxes.
*
* This is an array of tax ID keys with total amount values.
*
* @param array $raw_tax_data Array of taxes.
*/
public function set_taxes( $raw_tax_data ) {
$raw_tax_data = maybe_unserialize( $raw_tax_data );
$tax_data = array(
'total' => array(),
);
if ( ! empty( $raw_tax_data['total'] ) ) {
$tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] );
}
$this->set_prop( 'taxes', $tax_data );
$this->set_total_tax( array_sum( $tax_data['total'] ) );
}
/*
|--------------------------------------------------------------------------
| Getters
|--------------------------------------------------------------------------
*/
/**
* Get order item type.
*
* @return string
*/
public function get_type() {
return 'discount';
}
/**
* Get amount.
*
* @param string $context View or edit context.
* @return string
*/
public function get_amount( $context = 'view' ) {
return $this->get_prop( 'amount', $context );
}
/**
* Get discount_type.
*
* @param string $context View or edit context.
* @return string
*/
public function get_discount_type( $context = 'view' ) {
return $this->get_prop( 'discount_type', $context );
}
/**
* Get total fee.
*
* @param string $context
* @return string
*/
public function get_total( $context = 'view' ) {
return $this->get_prop( 'total', $context );
}
/**
* Get total tax.
*
* @param string $context
* @return string
*/
public function get_total_tax( $context = 'view' ) {
return $this->get_prop( 'total_tax', $context );
}
/**
* Get fee taxes.
*
* @param string $context
* @return array
*/
public function get_taxes( $context = 'view' ) {
return $this->get_prop( 'taxes', $context );
}
}

View File

@ -355,7 +355,6 @@ class WC_Order extends WC_Abstract_Order {
'shipping_lines' => $this->get_items( 'shipping' ),
'fee_lines' => $this->get_items( 'fee' ),
'coupon_lines' => $this->get_items( 'coupon' ),
'discount_lines' => $this->get_items( 'discount' ),
)
);
}
@ -1841,7 +1840,6 @@ class WC_Order extends WC_Abstract_Order {
$this->add_order_item_totals_subtotal_row( $total_rows, $tax_display );
$this->add_order_item_totals_shipping_row( $total_rows, $tax_display );
$this->add_order_item_totals_fee_rows( $total_rows, $tax_display );
$this->add_order_item_totals_discount_row( $total_rows, $tax_display );
$this->add_order_item_totals_tax_rows( $total_rows, $tax_display );
$this->add_order_item_totals_payment_method_row( $total_rows, $tax_display );
$this->add_order_item_totals_refund_rows( $total_rows, $tax_display );

View File

@ -351,7 +351,6 @@ final class WooCommerce {
include_once( WC_ABSPATH . 'includes/data-stores/abstract-wc-order-item-type-data-store.php' );
include_once( WC_ABSPATH . 'includes/data-stores/class-wc-order-item-data-store.php' );
include_once( WC_ABSPATH . 'includes/data-stores/class-wc-order-item-coupon-data-store.php' );
include_once( WC_ABSPATH . 'includes/data-stores/class-wc-order-item-discount-data-store.php' );
include_once( WC_ABSPATH . 'includes/data-stores/class-wc-order-item-fee-data-store.php' );
include_once( WC_ABSPATH . 'includes/data-stores/class-wc-order-item-product-store.php' );
include_once( WC_ABSPATH . 'includes/data-stores/class-wc-order-item-shipping-data-store.php' );

View File

@ -1,61 +0,0 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC Order Item Discount Data Store
*
* @version 3.2.0
* @category Class
* @author WooCommerce
*/
class WC_Order_Item_Discount_Data_Store extends Abstract_WC_Order_Item_Type_Data_Store implements WC_Object_Data_Store_Interface, WC_Order_Item_Type_Data_Store_Interface {
/**
* Data stored in meta keys.
*
* @since 3.0.0
* @var array
*/
protected $internal_meta_keys = array( 'discount_type', 'amount', '_line_total', '_line_tax', '_line_tax_data' );
/**
* Read/populate data properties specific to this order item.
*
* @since 3.0.0
* @param WC_Order_Item_Discount $item
*/
public function read( &$item ) {
parent::read( $item );
$id = $item->get_id();
$item->set_props( array(
'discount_type' => get_metadata( 'order_item', $id, 'discount_type', true ),
'total' => get_metadata( 'order_item', $id, '_line_total', true ),
'taxes' => get_metadata( 'order_item', $id, '_line_tax_data', true ),
'amount' => get_metadata( 'order_item', $id, 'amount', true ),
) );
$item->set_object_read( true );
}
/**
* Saves an item's data to the database / item meta.
* Ran after both create and update, so $item->get_id() will be set.
*
* @since 3.0.0
* @param WC_Order_Item_Discount $item
*/
public function save_item_data( &$item ) {
$id = $item->get_id();
$save_values = array(
'discount_type' => $item->get_discount_type( 'edit' ),
'_line_total' => $item->get_total( 'edit' ),
'_line_tax' => $item->get_total_tax( 'edit' ),
'_line_tax_data' => $item->get_taxes( 'edit' ),
'amount' => $item->get_amount( 'edit' ),
);
foreach ( $save_values as $key => $value ) {
update_metadata( 'order_item', $id, $key, $value );
}
}
}

View File

@ -1,36 +0,0 @@
<?php
/**
* Test for the discount class.
* @package WooCommerce\Tests\Discounts
*/
class WC_Tests_Discount extends WC_Unit_Test_Case {
/**
* Test get and set ID.
*/
public function test_get_set_amount() {
$discount = new WC_Discount;
$discount->set_amount( '10' );
$this->assertEquals( '10', $discount->get_amount() );
}
public function test_get_set_type() {
$discount = new WC_Discount;
$discount->set_discount_type( 'fixed' );
$this->assertEquals( 'fixed', $discount->get_discount_type() );
$discount->set_discount_type( 'percent' );
$this->assertEquals( 'percent', $discount->get_discount_type() );
}
/**
* Test get and set discount total.
*/
public function test_get_set_discount_total() {
$discount = new WC_Discount;
$discount->set_discount_total( 1000 );
$this->assertEquals( 1000, $discount->get_discount_total() );
}
}

View File

@ -66,19 +66,19 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
// Apply a percent discount.
$coupon->set_discount_type( 'percent' );
$discounts->set_items_from_cart( WC()->cart );
$discounts->apply_discount( $coupon );
$discounts->apply_coupon( $coupon );
$this->assertEquals( 9, $discounts->get_discounted_price( current( $discounts->get_items() ) ), print_r( $discounts->get_discounts(), true ) );
// Apply a fixed cart coupon.
$coupon->set_discount_type( 'fixed_cart' );
$discounts->set_items_from_cart( WC()->cart );
$discounts->apply_discount( $coupon );
$discounts->apply_coupon( $coupon );
$this->assertEquals( 0, $discounts->get_discounted_price( current( $discounts->get_items() ) ), print_r( $discounts->get_discounts(), true ) );
// Apply a fixed product coupon.
$coupon->set_discount_type( 'fixed_product' );
$discounts->set_items_from_cart( WC()->cart );
$discounts->apply_discount( $coupon );
$discounts->apply_coupon( $coupon );
$this->assertEquals( 0, $discounts->get_discounted_price( current( $discounts->get_items() ) ), print_r( $discounts->get_discounts(), true ) );
// Cleanup.
@ -354,7 +354,7 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
foreach ( $test['coupons'] as $coupon_props ) {
$coupon->set_props( $coupon_props );
$discounts->apply_discount( $coupon );
$discounts->apply_coupon( $coupon );
}
$all_discounts = $discounts->get_discounts();
@ -372,88 +372,4 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
update_option( 'woocommerce_calc_taxes', 'no' );
$coupon->delete( true );
}
/**
* Test apply_discount method.
*/
public function test_apply_discount() {
$tax_rate = array(
'tax_rate_country' => '',
'tax_rate_state' => '',
'tax_rate' => '20.0000',
'tax_rate_name' => 'VAT',
'tax_rate_priority' => '1',
'tax_rate_compound' => '0',
'tax_rate_shipping' => '1',
'tax_rate_order' => '1',
'tax_rate_class' => '',
);
$tax_rate2 = array(
'tax_rate_country' => '',
'tax_rate_state' => '',
'tax_rate' => '20.0000',
'tax_rate_name' => 'VAT',
'tax_rate_priority' => '1',
'tax_rate_compound' => '0',
'tax_rate_shipping' => '1',
'tax_rate_order' => '1',
'tax_rate_class' => 'reduced-rate',
);
$tax_rate_id = WC_Tax::_insert_tax_rate( $tax_rate );
$tax_rate_id2 = WC_Tax::_insert_tax_rate( $tax_rate2 );
update_option( 'woocommerce_calc_taxes', 'yes' );
$product = WC_Helper_Product::create_simple_product();
$product2 = WC_Helper_Product::create_simple_product();
$product->set_tax_class( '' );
$product2->set_tax_class( 'reduced-rate' );
$product->save();
$product2->save();
// Add product to the cart.
WC()->cart->empty_cart();
WC()->cart->add_to_cart( $product->get_id(), 1 );
WC()->cart->add_to_cart( $product2->get_id(), 1 );
$discounts = new WC_Discounts();
$discounts->set_items_from_cart( WC()->cart );
$discounts->apply_discount( '50%' );
$all_discounts = $discounts->get_discounts();
$this->assertEquals( 10, $all_discounts['discount-50%'], print_r( $all_discounts, true ) );
$discounts->apply_discount( '50%' );
$all_discounts = $discounts->get_discounts();
$this->assertEquals( 10, $all_discounts['discount-50%'], print_r( $all_discounts, true ) );
$this->assertEquals( 5, $all_discounts['discount-50%-2'], print_r( $all_discounts, true ) );
// Test fixed discounts.
$discounts = new WC_Discounts();
$discounts->set_items_from_cart( WC()->cart );
$discounts->apply_discount( '5' );
$all_discounts = $discounts->get_discounts();
$this->assertEquals( 5, $all_discounts['discount-500'] );
$discounts->apply_discount( '5' );
$all_discounts = $discounts->get_discounts();
$this->assertEquals( 5, $all_discounts['discount-500'], print_r( $all_discounts, true ) );
$this->assertEquals( 5, $all_discounts['discount-500-2'], print_r( $all_discounts, true ) );
$discounts->apply_discount( '15' );
$all_discounts = $discounts->get_discounts();
$this->assertEquals( 5, $all_discounts['discount-500'], print_r( $all_discounts, true ) );
$this->assertEquals( 5, $all_discounts['discount-500-2'], print_r( $all_discounts, true ) );
$this->assertEquals( 10, $all_discounts['discount-1500'], print_r( $all_discounts, true ) );
// Cleanup.
WC()->cart->empty_cart();
$product->delete( true );
$product2->delete( true );
WC_Tax::_delete_tax_rate( $tax_rate_id );
WC_Tax::_delete_tax_rate( $tax_rate_id2 );
update_option( 'woocommerce_calc_taxes', 'no' );
}
}

View File

@ -1566,7 +1566,7 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
}
/**
* Test add_discount and remove_coupon with a fixed discount coupon.
* Test apply_coupon and remove_coupon with a fixed discount coupon.
* @since 3.2.0
*/
function test_add_remove_coupon_fixed() {
@ -1578,7 +1578,7 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
$coupon->set_amount( 10 );
$coupon->save();
$order->add_discount( 'test' );
$order->apply_coupon( 'test' );
$this->assertEquals( 40, $order->get_total() );
$order->remove_coupon( 'test' );
@ -1589,7 +1589,7 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
}
/**
* Test add_discount and remove_coupon with a percent discount coupon.
* Test apply_coupon and remove_coupon with a percent discount coupon.
* @since 3.2.0
*/
function test_add_remove_coupon_percent() {
@ -1601,7 +1601,7 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
$coupon->set_amount( 50 );
$coupon->save();
$order->add_discount( 'test' );
$order->apply_coupon( 'test' );
$this->assertEquals( 30, $order->get_total() );
$order->remove_coupon( 'test' );
@ -1610,36 +1610,4 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
$coupon->delete( true );
$order->delete( true );
}
/**
* Test add_discount and removing the discount with a fixed discount.
* @since 3.2.0
*/
function test_add_remove_discount_fixed() {
$order = WC_Helper_Order::create_order();
$order->add_discount( 10 );
$this->assertEquals( 40, $order->get_total() );
$discount = current( $order->get_items( 'discount' ) );
$order->remove_item( $discount->get_id() );
$order->calculate_totals( true );
$this->assertEquals( 50, $order->get_total() );
}
/**
* Test add_discount and removing the discount with a percent discount.
* @since 3.2.0
*/
function test_add_remove_discount_percent() {
$order = WC_Helper_Order::create_order();
$order->add_discount( '50%' );
$this->assertEquals( 25, $order->get_total() );
$discount = current( $order->get_items( 'discount' ) );
$order->remove_item( $discount->get_id() );
$order->calculate_totals( true );
$this->assertEquals( 50, $order->get_total() );
}
}