Discount rows
This commit is contained in:
parent
06c1a2ad8d
commit
0e96bd8a93
|
@ -157,15 +157,47 @@ class WC_Discounts {
|
|||
|
||||
/**
|
||||
* Allows a discount to be applied to the items programmatically without a coupon.
|
||||
* @return [type] [description]
|
||||
*
|
||||
* @param string $raw_discount Discount amount either fixed or percentage.
|
||||
* @return int discounted amount in cents.
|
||||
*/
|
||||
public function apply_discount( $discount ) {
|
||||
if ( strstr( $discount, '%' ) ) {
|
||||
$discount = absint( rtrim( $discount, '%' ) );
|
||||
$discounted = $this->apply_percentage_discount( $this->items, $discount );
|
||||
public function apply_discount( $raw_discount ) {
|
||||
if ( strstr( $raw_discount, '%' ) ) {
|
||||
$discount = absint( rtrim( $raw_discount, '%' ) );
|
||||
$total_to_discount = 0;
|
||||
|
||||
return $this->remove_precision( $discounted );
|
||||
// Get total item cost right now.
|
||||
foreach ( $this->items as $item ) {
|
||||
$total_to_discount += $this->get_discounted_price_in_cents( $item );
|
||||
}
|
||||
|
||||
// @todo sum other manual discounts too
|
||||
foreach ( $this->discounts as $key => $value ) {
|
||||
if ( strstr( $key, 'discount-' ) ) {
|
||||
$total_to_discount = $total_to_discount - $value;
|
||||
}
|
||||
}
|
||||
|
||||
$discount_total = $discount * ( $total_to_discount / 100 );
|
||||
$discount_id = '';
|
||||
$index = 1;
|
||||
|
||||
while ( ! $discount_id ) {
|
||||
$discount_id = 'discount-' . $raw_discount;
|
||||
|
||||
if ( 1 < $index ) {
|
||||
$discount_id .= '-' . $index;
|
||||
}
|
||||
|
||||
if ( isset( $this->discounts[ $discount_id ] ) ) {
|
||||
$index ++;
|
||||
$discount_id = '';
|
||||
}
|
||||
}
|
||||
|
||||
return $this->discounts[ $discount_id ] = $discount_total;
|
||||
}
|
||||
// @todo fixed discounts
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -91,13 +91,15 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
|
|||
// Test.
|
||||
$discounts = new WC_Discounts();
|
||||
$discounts->set_items( WC()->cart->get_cart() );
|
||||
$result = $discounts->apply_discount( '50%' );
|
||||
|
||||
echo "\n\n\n";
|
||||
print_r( $result );
|
||||
echo "\n\n\n";
|
||||
$discounts->apply_discount( '50%' );
|
||||
$all_discounts = $discounts->get_discounts();
|
||||
$this->assertEquals( $all_discounts['discount-50%'], 10 );
|
||||
|
||||
$this->assertEquals( array( 'test' => 2 ), $result );
|
||||
$discounts->apply_discount( '50%' );
|
||||
$all_discounts = $discounts->get_discounts();
|
||||
$this->assertEquals( $all_discounts['discount-50%'], 10, print_r( $all_discounts, true ) );
|
||||
$this->assertEquals( $all_discounts['discount-50%-2'], 5, print_r( $all_discounts, true ) );
|
||||
|
||||
// Cleanup.
|
||||
WC()->cart->empty_cart();
|
||||
|
|
Loading…
Reference in New Issue