/** * Test the is_customer_outside_base method. */ public function test_is_customer_outside_base() { // Get the original settings for the session and the WooCommerce options $original_chosen_shipping_methods = WC_Helper_Customer::get_chosen_shipping_methods(); $original_tax_based_on = WC_Helper_Customer::get_tax_based_on(); $original_customer_details = WC_Helper_Customer::get_customer_details(); $customer = WC_Helper_Customer::create_mock_customer(); // Create dummy product, and add the product to the cart. $product = WC_Helper_Product::create_simple_product(); WC()->cart->add_to_cart($product->get_id(), 1); // Customer is going with the Local Pickup option, and the store calculates tax based on the store location. WC_Helper_Customer::set_chosen_shipping_methods(array('local_pickup')); WC_Helper_Customer::set_tax_based_on('base'); $this->assertEquals($customer->is_customer_outside_base(), false); // Customer is going with the Local Pickup option, and the store calculates tax based on the customer's billing address. WC_Helper_Customer::set_chosen_shipping_methods(array('local_pickup')); WC_Helper_Customer::set_tax_based_on('billing'); $this->assertEquals($customer->is_customer_outside_base(), false); // Customer is going with the Free Shipping option, and the store calculates tax based on the customer's billing address. WC_Helper_Customer::set_chosen_shipping_methods(array('free_shipping')); WC_Helper_Customer::set_tax_based_on('billing'); $this->assertEquals($customer->is_customer_outside_base(), true); // Customer is going with the Free Shipping option, and the store calculates tax based on the store base location. WC_Helper_Customer::set_chosen_shipping_methods(array('free_shipping')); WC_Helper_Customer::set_tax_based_on('base'); $this->assertEquals($customer->is_customer_outside_base(), false); // Now reset the settings back to the way they were before this test WC_Helper_Customer::set_chosen_shipping_methods($original_chosen_shipping_methods); WC_Helper_Customer::set_tax_based_on($original_tax_based_on); WC_Helper_Customer::set_customer_details($original_customer_details); // Clean up the cart WC()->cart->empty_cart(); // Clean up product WC_Helper_Product::delete_product($product->get_id()); }
/** * Test wc_cart_totals_subtotal_html() * * @todo test with taxes incl./excl. * @since 2.4 */ public function test_wc_cart_totals_subtotal_html() { $product = \WC_Helper_Product::create_simple_product(); WC()->cart->add_to_cart($product->id, 1); $this->expectOutputString(wc_price($product->price), wc_cart_totals_subtotal_html()); \WC_Helper_Product::delete_product($product->id); }
/** * Delete a product. * * @param int $order_id ID of the order to delete. */ public static function delete_order($order_id) { $order = wc_get_order($order_id); // Delete all products in the order foreach ($order->get_items() as $item) { WC_Helper_Product::delete_product($item['product_id']); } WC_Helper_Shipping::delete_simple_flat_rate(); // Delete the order post wp_delete_post($order_id, true); }
/** * Test wc_empty_cart() * * @since 2.3.0 */ public function test_wc_empty_cart() { // Create dummy product $product = \WC_Helper_Product::create_simple_product(); // Add the product to the cart WC()->cart->add_to_cart($product->id, 1); // Empty the cart wc_empty_cart(); // Check if the cart is empty $this->assertEquals(0, WC()->cart->get_cart_contents_count()); // Delete the previously created product \WC_Helper_Product::delete_product($product->id); }
/** * 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); }
/** * Test backorders_require_notification(). * * @since 2.3 */ public function test_backorders_require_notification() { // Create product $product = WC_Helper_Product::create_simple_product(); $product->backorders = 'notify'; $product->manage_stock = 'yes'; $this->assertTrue($product->backorders_require_notification()); $product->backorders = 'yes'; $this->assertFalse($product->backorders_require_notification()); $product->backorders = 'no'; $this->assertFalse($product->backorders_require_notification()); $product->backorders = 'yes'; $product->manage_stock = 'no'; $this->assertFalse($product->backorders_require_notification()); // Delete product WC_Helper_Product::delete_product($product->id); }
/** * 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); }
/** * Test wc_get_product_id_by_sku() * * @since 2.3 */ public function test_wc_get_product_id_by_sku() { // Create product $product = \WC_Helper_Product::create_simple_product(); $this->assertEquals($product->id, wc_get_product_id_by_sku($product->sku)); // Delete Product \WC_Helper_Product::delete_product($product->id); }