From 06c1a2ad8daa5062e53267ab249ef292b6e2b744 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 20 Jul 2017 14:33:38 +0100 Subject: [PATCH] apply discount --- includes/class-wc-discounts.php | 13 +++++ tests/unit-tests/discounts/discounts.php | 60 ++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/includes/class-wc-discounts.php b/includes/class-wc-discounts.php index 26b9b56b6d1..3593d45d59b 100644 --- a/includes/class-wc-discounts.php +++ b/includes/class-wc-discounts.php @@ -155,6 +155,19 @@ class WC_Discounts { } } + /** + * Allows a discount to be applied to the items programmatically without a coupon. + * @return [type] [description] + */ + public function apply_discount( $discount ) { + if ( strstr( $discount, '%' ) ) { + $discount = absint( rtrim( $discount, '%' ) ); + $discounted = $this->apply_percentage_discount( $this->items, $discount ); + + return $this->remove_precision( $discounted ); + } + } + /** * Apply a discount to all items using a coupon. * diff --git a/tests/unit-tests/discounts/discounts.php b/tests/unit-tests/discounts/discounts.php index 247f5257441..3d591bb7b76 100644 --- a/tests/unit-tests/discounts/discounts.php +++ b/tests/unit-tests/discounts/discounts.php @@ -48,6 +48,66 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case { $order->delete( true ); } + 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->add_to_cart( $product->get_id(), 1 ); + WC()->cart->add_to_cart( $product2->get_id(), 1 ); + + // 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"; + + $this->assertEquals( array( 'test' => 2 ), $result ); + + // 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' ); + } + /** * test get_applied_coupons */