Merge branch 'discounts-class-fixes-and-tests'

This commit is contained in:
Mike Jolley 2017-09-20 18:44:18 +01:00
commit 5151415eec
3 changed files with 1230 additions and 299 deletions

View File

@ -380,7 +380,13 @@ final class WC_Cart_Totals {
}
/**
* Sort coupons so discounts apply consistently.
* Sort coupons so discounts apply consistently across installs.
*
* In order of priority;
* - sort param
* - usage restriction
* - coupon value
* - ID
*
* @param WC_Coupon $a Coupon object.
* @param WC_Coupon $b Coupon object.
@ -388,7 +394,13 @@ final class WC_Cart_Totals {
*/
protected function sort_coupons_callback( $a, $b ) {
if ( $a->sort === $b->sort ) {
return $a->get_id() - $b->get_id();
if ( $a->get_limit_usage_to_x_items() === $b->get_limit_usage_to_x_items() ) {
if ( $a->get_amount() === $b->get_amount() ) {
return $b->get_id() - $a->get_id();
}
return ( $a->get_amount() < $b->get_amount() ) ? -1 : 1;
}
return ( $a->get_limit_usage_to_x_items() < $b->get_limit_usage_to_x_items() ) ? -1 : 1;
}
return ( $a->sort < $b->sort ) ? -1 : 1;
}

View File

@ -300,25 +300,31 @@ class WC_Discounts {
}
foreach ( $this->items as $item ) {
if ( 0 === $this->get_discounted_price_in_cents( $item ) ) {
$item_to_apply = clone $item; // Clone the item so changes to this item do not affect the originals.
if ( 0 === $this->get_discounted_price_in_cents( $item_to_apply ) ) {
continue;
}
if ( ! $coupon->is_valid_for_product( $item->product, $item->object ) && ! $coupon->is_valid_for_cart() ) {
if ( ! $coupon->is_valid_for_product( $item_to_apply->product, $item_to_apply->object ) && ! $coupon->is_valid_for_cart() ) {
continue;
}
if ( $limit_usage_qty && $applied_count > $limit_usage_qty ) {
if ( $limit_usage_qty && $applied_count >= $limit_usage_qty ) {
break;
}
if ( $limit_usage_qty && $item->quantity > ( $limit_usage_qty - $applied_count ) ) {
$limit_to_qty = absint( $limit_usage_qty - $applied_count );
$item->price = ( $item->price / $item->quantity ) * $limit_to_qty;
$item->quantity = $limit_to_qty; // Lower the qty so the discount is applied less.
if ( $limit_usage_qty && $item_to_apply->quantity > ( $limit_usage_qty - $applied_count ) ) {
$limit_to_qty = absint( $limit_usage_qty - $applied_count );
// Lower the qty and cost so the discount is applied less.
$item_to_apply->price = ( $item_to_apply->price / $item_to_apply->quantity ) * $limit_to_qty;
$item_to_apply->quantity = $limit_to_qty;
}
if ( 0 >= $item->quantity ) {
if ( 0 >= $item_to_apply->quantity ) {
continue;
}
$items_to_apply[] = $item;
$applied_count += $item->quantity;
$items_to_apply[] = $item_to_apply;
$applied_count += $item_to_apply->quantity;
}
return $items_to_apply;
}
@ -357,6 +363,7 @@ class WC_Discounts {
}
$discount = min( $discounted_price, $discount );
$total_discount += $discount;
// Store code and discount amount per item.

File diff suppressed because it is too large Load Diff