Revised coupon sort order to not rely on IDs which can differ between cart and tests

Also edited test to match
This commit is contained in:
Mike Jolley 2017-09-20 18:41:48 +01:00
parent 29107d24a9
commit 21dfde842a
2 changed files with 19 additions and 7 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

@ -827,17 +827,17 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
),
),
'coupons' => array(
array(
'code' => 'test1',
'discount_type' => 'percent',
'amount' => '20',
),
array(
'code' => 'test',
'discount_type' => 'percent',
'amount' => '30',
'limit_usage_to_x_items' => 5,
),
array(
'code' => 'test1',
'discount_type' => 'percent',
'amount' => '20',
),
),
'expected_total_discount' => 18.30,
),