2017-07-18 13:04:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for the discounts class.
|
|
|
|
* @package WooCommerce\Tests\Discounts
|
|
|
|
*/
|
|
|
|
class WC_Tests_Discounts extends WC_Unit_Test_Case {
|
|
|
|
|
2017-07-18 14:42:46 +00:00
|
|
|
/**
|
|
|
|
* Test get and set items.
|
|
|
|
*/
|
2017-07-28 12:02:39 +00:00
|
|
|
public function test_get_set_items_from_cart() {
|
2017-07-18 13:04:56 +00:00
|
|
|
// Create dummy product - price will be 10
|
|
|
|
$product = WC_Helper_Product::create_simple_product();
|
|
|
|
|
2017-07-18 14:42:46 +00:00
|
|
|
// Add product to the cart.
|
2017-07-18 13:04:56 +00:00
|
|
|
WC()->cart->add_to_cart( $product->get_id(), 1 );
|
|
|
|
|
2017-07-18 14:42:46 +00:00
|
|
|
// Add product to a dummy order.
|
|
|
|
$order = new WC_Order();
|
2017-07-18 17:07:46 +00:00
|
|
|
$order->add_product( $product, 1 );
|
2017-07-18 14:42:46 +00:00
|
|
|
$order->calculate_totals();
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
// Test setting items to the cart.
|
2017-07-18 13:04:56 +00:00
|
|
|
$discounts = new WC_Discounts();
|
2017-07-28 12:02:39 +00:00
|
|
|
$discounts->set_items_from_cart( WC()->cart );
|
2017-07-18 19:42:47 +00:00
|
|
|
$this->assertEquals( 1, count( $discounts->get_items() ) );
|
2017-07-18 14:42:46 +00:00
|
|
|
|
|
|
|
// Test setting items to an order.
|
|
|
|
$discounts = new WC_Discounts();
|
2017-07-28 12:02:39 +00:00
|
|
|
$discounts->set_items_from_cart( WC()->cart );
|
2017-07-18 19:42:47 +00:00
|
|
|
$this->assertEquals( 1, count( $discounts->get_items() ) );
|
2017-07-18 13:04:56 +00:00
|
|
|
|
2017-07-18 14:42:46 +00:00
|
|
|
// Empty array of items.
|
|
|
|
$discounts = new WC_Discounts();
|
2017-07-28 12:02:39 +00:00
|
|
|
$discounts->set_items_from_cart( array() );
|
2017-07-18 14:42:46 +00:00
|
|
|
$this->assertEquals( array(), $discounts->get_items() );
|
|
|
|
|
|
|
|
// Invalid items.
|
|
|
|
$discounts = new WC_Discounts();
|
2017-07-28 12:02:39 +00:00
|
|
|
$discounts->set_items_from_cart( false );
|
2017-07-18 14:42:46 +00:00
|
|
|
$this->assertEquals( array(), $discounts->get_items() );
|
2017-07-18 13:04:56 +00:00
|
|
|
|
|
|
|
// Cleanup.
|
|
|
|
WC()->cart->empty_cart();
|
2017-07-18 17:07:46 +00:00
|
|
|
$product->delete( true );
|
|
|
|
$order->delete( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-19 11:26:01 +00:00
|
|
|
* Test applying a coupon (make sure it changes prices).
|
2017-07-18 17:07:46 +00:00
|
|
|
*/
|
|
|
|
public function test_apply_coupon() {
|
|
|
|
$discounts = new WC_Discounts();
|
|
|
|
|
|
|
|
// Create dummy content.
|
|
|
|
$product = WC_Helper_Product::create_simple_product();
|
2017-07-18 19:42:47 +00:00
|
|
|
$product->set_tax_status( 'taxable' );
|
|
|
|
$product->save();
|
2017-07-18 17:47:05 +00:00
|
|
|
WC()->cart->empty_cart();
|
2017-07-18 17:07:46 +00:00
|
|
|
WC()->cart->add_to_cart( $product->get_id(), 1 );
|
2017-07-26 01:36:41 +00:00
|
|
|
$coupon = WC_Helper_Coupon::create_coupon( 'test' );
|
2017-07-19 11:26:01 +00:00
|
|
|
$coupon->set_amount( 10 );
|
2017-07-18 17:07:46 +00:00
|
|
|
|
|
|
|
// Apply a percent discount.
|
|
|
|
$coupon->set_discount_type( 'percent' );
|
2017-07-28 12:02:39 +00:00
|
|
|
$discounts->set_items_from_cart( WC()->cart );
|
2017-08-18 09:36:10 +00:00
|
|
|
$discounts->apply_coupon( $coupon );
|
2017-07-28 15:31:11 +00:00
|
|
|
$this->assertEquals( 9, $discounts->get_discounted_price( current( $discounts->get_items() ) ), print_r( $discounts->get_discounts(), true ) );
|
2017-07-18 17:07:46 +00:00
|
|
|
|
2017-07-18 17:47:05 +00:00
|
|
|
// Apply a fixed cart coupon.
|
2017-07-18 17:07:46 +00:00
|
|
|
$coupon->set_discount_type( 'fixed_cart' );
|
2017-07-28 12:02:39 +00:00
|
|
|
$discounts->set_items_from_cart( WC()->cart );
|
2017-08-18 09:36:10 +00:00
|
|
|
$discounts->apply_coupon( $coupon );
|
2017-07-28 15:31:11 +00:00
|
|
|
$this->assertEquals( 0, $discounts->get_discounted_price( current( $discounts->get_items() ) ), print_r( $discounts->get_discounts(), true ) );
|
2017-07-18 17:07:46 +00:00
|
|
|
|
2017-07-18 17:47:05 +00:00
|
|
|
// Apply a fixed product coupon.
|
|
|
|
$coupon->set_discount_type( 'fixed_product' );
|
2017-07-28 12:02:39 +00:00
|
|
|
$discounts->set_items_from_cart( WC()->cart );
|
2017-08-18 09:36:10 +00:00
|
|
|
$discounts->apply_coupon( $coupon );
|
2017-07-28 15:31:11 +00:00
|
|
|
$this->assertEquals( 0, $discounts->get_discounted_price( current( $discounts->get_items() ) ), print_r( $discounts->get_discounts(), true ) );
|
2017-07-18 17:47:05 +00:00
|
|
|
|
2017-07-18 17:07:46 +00:00
|
|
|
// Cleanup.
|
|
|
|
WC()->cart->empty_cart();
|
|
|
|
$product->delete( true );
|
2017-07-26 01:36:41 +00:00
|
|
|
$coupon->delete( true );
|
2017-07-19 11:26:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test various discount calculations are working correctly and produding expected results.
|
|
|
|
*/
|
|
|
|
public function test_calculations() {
|
|
|
|
$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_rate_id = WC_Tax::_insert_tax_rate( $tax_rate );
|
|
|
|
update_option( 'woocommerce_calc_taxes', 'yes' );
|
|
|
|
|
|
|
|
$tests = array(
|
|
|
|
array(
|
|
|
|
'prices_include_tax' => false,
|
|
|
|
'cart' => array(
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 11:26:01 +00:00
|
|
|
),
|
|
|
|
'coupons' => array(
|
|
|
|
array(
|
|
|
|
'code' => 'test',
|
|
|
|
'discount_type' => 'percent',
|
|
|
|
'amount' => '20',
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 11:26:01 +00:00
|
|
|
),
|
|
|
|
'expected_total_discount' => 2,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'prices_include_tax' => false,
|
|
|
|
'cart' => array(
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 2,
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 11:26:01 +00:00
|
|
|
),
|
|
|
|
'coupons' => array(
|
|
|
|
array(
|
|
|
|
'code' => 'test',
|
|
|
|
'discount_type' => 'fixed_cart',
|
|
|
|
'amount' => '10',
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 11:26:01 +00:00
|
|
|
),
|
|
|
|
'expected_total_discount' => 10,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'prices_include_tax' => false,
|
|
|
|
'cart' => array(
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 11:26:01 +00:00
|
|
|
),
|
|
|
|
'coupons' => array(
|
|
|
|
array(
|
|
|
|
'code' => 'test',
|
|
|
|
'discount_type' => 'fixed_cart',
|
|
|
|
'amount' => '10',
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 11:26:01 +00:00
|
|
|
),
|
|
|
|
'expected_total_discount' => 10,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'prices_include_tax' => false,
|
|
|
|
'cart' => array(
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 11:26:01 +00:00
|
|
|
),
|
|
|
|
'coupons' => array(
|
|
|
|
array(
|
|
|
|
'code' => 'test',
|
|
|
|
'discount_type' => 'fixed_cart',
|
|
|
|
'amount' => '10',
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 11:26:01 +00:00
|
|
|
),
|
|
|
|
'expected_total_discount' => 10,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'prices_include_tax' => false,
|
|
|
|
'cart' => array(
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 2,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 3,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 2,
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 11:26:01 +00:00
|
|
|
),
|
|
|
|
'coupons' => array(
|
|
|
|
array(
|
|
|
|
'code' => 'test',
|
|
|
|
'discount_type' => 'fixed_cart',
|
|
|
|
'amount' => '10',
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 11:26:01 +00:00
|
|
|
),
|
|
|
|
'expected_total_discount' => 10,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'prices_include_tax' => false,
|
|
|
|
'cart' => array(
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 1,
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 11:26:01 +00:00
|
|
|
),
|
|
|
|
'coupons' => array(
|
|
|
|
array(
|
|
|
|
'code' => 'test',
|
|
|
|
'discount_type' => 'fixed_cart',
|
|
|
|
'amount' => '10',
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 11:26:01 +00:00
|
|
|
),
|
|
|
|
'expected_total_discount' => 10,
|
2017-07-19 11:27:50 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'prices_include_tax' => false,
|
|
|
|
'cart' => array(
|
|
|
|
array(
|
|
|
|
'price' => 1,
|
|
|
|
'qty' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 1,
|
|
|
|
'qty' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 1,
|
|
|
|
'qty' => 1,
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 11:27:50 +00:00
|
|
|
),
|
|
|
|
'coupons' => array(
|
|
|
|
array(
|
|
|
|
'code' => 'test',
|
|
|
|
'discount_type' => 'fixed_cart',
|
|
|
|
'amount' => '1',
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 11:27:50 +00:00
|
|
|
),
|
|
|
|
'expected_total_discount' => 1,
|
|
|
|
),
|
2017-07-19 12:49:22 +00:00
|
|
|
array(
|
|
|
|
'prices_include_tax' => false,
|
|
|
|
'cart' => array(
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 2,
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 12:49:22 +00:00
|
|
|
),
|
|
|
|
'coupons' => array(
|
|
|
|
array(
|
|
|
|
'code' => 'test',
|
|
|
|
'discount_type' => 'percent',
|
|
|
|
'amount' => '10',
|
|
|
|
'limit_usage_to_x_items' => 1,
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 12:49:22 +00:00
|
|
|
),
|
|
|
|
'expected_total_discount' => 1,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'prices_include_tax' => false,
|
|
|
|
'cart' => array(
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 2,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'price' => 10,
|
|
|
|
'qty' => 2,
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 12:49:22 +00:00
|
|
|
),
|
|
|
|
'coupons' => array(
|
|
|
|
array(
|
|
|
|
'code' => 'test',
|
|
|
|
'discount_type' => 'percent',
|
|
|
|
'amount' => '10',
|
|
|
|
'limit_usage_to_x_items' => 1,
|
2017-07-26 01:36:41 +00:00
|
|
|
),
|
2017-07-19 12:49:22 +00:00
|
|
|
),
|
|
|
|
'expected_total_discount' => 1,
|
|
|
|
),
|
2017-07-19 11:26:01 +00:00
|
|
|
);
|
|
|
|
|
2017-07-26 01:36:41 +00:00
|
|
|
$coupon = WC_Helper_Coupon::create_coupon( 'test' );
|
|
|
|
|
2017-07-19 11:26:01 +00:00
|
|
|
foreach ( $tests as $test_index => $test ) {
|
|
|
|
$discounts = new WC_Discounts();
|
|
|
|
$products = array();
|
|
|
|
|
|
|
|
foreach ( $test['cart'] as $item ) {
|
|
|
|
$product = WC_Helper_Product::create_simple_product();
|
|
|
|
$product->set_regular_price( $item['price'] );
|
|
|
|
$product->set_tax_status( 'taxable' );
|
|
|
|
$product->save();
|
|
|
|
WC()->cart->add_to_cart( $product->get_id(), $item['qty'] );
|
|
|
|
$products[] = $product;
|
|
|
|
}
|
|
|
|
|
2017-07-28 12:02:39 +00:00
|
|
|
$discounts->set_items_from_cart( WC()->cart );
|
2017-07-19 11:26:01 +00:00
|
|
|
|
|
|
|
foreach ( $test['coupons'] as $coupon_props ) {
|
|
|
|
$coupon->set_props( $coupon_props );
|
2017-08-18 09:36:10 +00:00
|
|
|
$discounts->apply_coupon( $coupon );
|
2017-07-19 11:26:01 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 15:53:23 +00:00
|
|
|
$all_discounts = $discounts->get_discounts();
|
|
|
|
$this->assertEquals( $test['expected_total_discount'], array_sum( $all_discounts['test'] ), 'Test case ' . $test_index . ' failed (' . print_r( $test, true ) . ' - ' . print_r( $discounts->get_discounts(), true ) . ')' );
|
2017-07-19 11:26:01 +00:00
|
|
|
|
|
|
|
// Clean.
|
|
|
|
WC()->cart->empty_cart();
|
|
|
|
|
|
|
|
foreach ( $products as $product ) {
|
|
|
|
$product->delete( true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 19:42:47 +00:00
|
|
|
WC_Tax::_delete_tax_rate( $tax_rate_id );
|
|
|
|
update_option( 'woocommerce_calc_taxes', 'no' );
|
2017-07-26 01:36:41 +00:00
|
|
|
$coupon->delete( true );
|
2017-07-18 13:04:56 +00:00
|
|
|
}
|
|
|
|
}
|