Small tidyup

This commit is contained in:
Mike Jolley 2017-07-18 18:52:50 +01:00
parent 2b9e3aafdd
commit 37fbd96de8
2 changed files with 19 additions and 17 deletions

View File

@ -135,9 +135,7 @@ class WC_Discounts {
*/
private function apply_percentage_discount( $amount ) {
foreach ( $this->items as $item ) {
$discount = (float) $amount * ( $item->discounted_price / 100 );
$item->discounted_price -= $discount;
$item->discounted_price = max( 0, $item->discounted_price );
$this->apply_discount_to_item( $item, (float) $amount * ( $item->discounted_price / 100 ) );
}
}
@ -148,19 +146,10 @@ class WC_Discounts {
*/
private function apply_fixed_product_discount( $discount ) {
foreach ( $this->items as $item ) {
$item->discounted_price -= $discount;
$item->discounted_price = max( 0, $item->discounted_price );
$this->apply_discount_to_item( $item, $discount );
}
}
private function apply_discount_to_item( &$item, $discount ) {
if ( $discount > $item->discounted_price ) {
$discount = $item->discounted_price;
}
$item->discounted_price = $item->discounted_price - $discount;
return $discount;
}
/**
* Apply fixed cart discount to items.
*
@ -190,9 +179,8 @@ class WC_Discounts {
$did_discount = false;
foreach ( $this->items as $item ) {
if ( $item->discounted_price ) {
$item->discounted_price --;
$discount --;
if ( $this->apply_discount_to_item( $item, 0.1 ) ) {
$discount -= 0.1;
$did_discount = true;
}
}
@ -203,4 +191,19 @@ class WC_Discounts {
}
}
}
/**
* Apply a discount amount to an item and ensure it does not go negative.
*
* @param object $item
* @param float $discount
* @return float
*/
private function apply_discount_to_item( &$item, $discount ) {
if ( $discount > $item->discounted_price ) {
$discount = $item->discounted_price;
}
$item->discounted_price = $item->discounted_price - $discount;
return $discount;
}
}

View File

@ -73,7 +73,6 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
$coupon->set_discount_type( 'fixed_cart' );
$discounts->set_items( WC()->cart->get_cart() );
$discounts->apply_coupon( $coupon );
var_dump($discounts->get_items());
$this->assertEquals( array( (object) array( 'price' => '10', 'discounted_price' => '0', 'quantity' => 1 ) ), $discounts->get_items() );
// Apply a fixed cart coupon.