示例#1
0
 /**
  * Test percent product discount method.
  *
  * @since 2.3
  */
 public function test_percent_product_discount()
 {
     // Create product
     $product = WC_Helper_Product::create_simple_product();
     update_post_meta($product->id, '_price', '10');
     update_post_meta($product->id, '_regular_price', '10');
     // Create coupon
     $coupon = WC_Helper_Coupon::create_coupon();
     update_post_meta($coupon->get_id(), 'discount_type', 'percent_product');
     update_post_meta($coupon->get_id(), 'coupon_amount', '5');
     // Create a flat rate method
     WC_Helper_Shipping::create_simple_flat_rate();
     // We need this to have the calculate_totals() method calculate totals
     if (!defined('WOOCOMMERCE_CHECKOUT')) {
         define('WOOCOMMERCE_CHECKOUT', true);
     }
     // Add fee
     WC_Helper_Fee::add_cart_fee();
     // Add product to cart
     WC()->cart->add_to_cart($product->id, 1);
     // Add coupon
     WC()->cart->add_discount($coupon->get_code());
     // Set the flat_rate shipping method
     WC()->session->set('chosen_shipping_methods', array('flat_rate'));
     WC()->cart->calculate_totals();
     // Test if the cart total amount is equal 29.5
     $this->assertEquals(29.5, WC()->cart->total);
     // Clearing WC notices
     wc_clear_notices();
     // Clean up the cart
     WC()->cart->empty_cart();
     // Remove coupons
     WC()->cart->remove_coupons();
     // Remove fee
     WC_Helper_Fee::remove_cart_fee();
     // Delete the flat rate method
     WC()->session->set('chosen_shipping_methods', array());
     WC_Helper_Shipping::delete_simple_flat_rate();
     // Delete coupon
     WC_Helper_Coupon::delete_coupon($coupon->get_id());
     // Delete product
     WC_Helper_Product::delete_product($product->id);
 }
示例#2
0
 /**
  * Test cart fee
  *
  * @since 2.3
  */
 public function test_cart_fee()
 {
     // Create product
     $product = \WC_Helper_Product::create_simple_product();
     update_post_meta($product->id, '_price', '10');
     update_post_meta($product->id, '_regular_price', '10');
     // We need this to have the calculate_totals() method calculate totals
     if (!defined('WOOCOMMERCE_CHECKOUT')) {
         define('WOOCOMMERCE_CHECKOUT', true);
     }
     // Add fee
     \WC_Helper_Fee::add_cart_fee();
     // Add product to cart
     WC()->cart->add_to_cart($product->id, 1);
     // Test if the cart total amount is equal 20
     $this->assertEquals(20, WC()->cart->total);
     // Clearing WC notices
     wc_clear_notices();
     // Clean up the cart
     WC()->cart->empty_cart();
     // Remove fee
     \WC_Helper_Fee::remove_cart_fee();
     // Delete product
     \WC_Helper_Product::delete_product($product->id);
 }