/** * Create a order. * * @since 2.4 * * @return WC_Order Order object. */ public static function create_order() { // Create product $product = WC_Helper_Product::create_simple_product(); WC_Helper_Shipping::create_simple_flat_rate(); $order_data = array('status' => 'pending', 'customer_id' => 1, 'customer_note' => '', 'total' => ''); $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Required, else wc_create_order throws an exception $order = wc_create_order($order_data); // Add order products $item_id = $order->add_product($product, 4); // Set billing address $billing_address = array('country' => 'US', 'first_name' => 'Jeroen', 'last_name' => 'Sormani', 'company' => 'WooCompany', 'address_1' => 'WooAddress', 'address_2' => '', 'postcode' => '123456', 'city' => 'WooCity', 'state' => 'NY', 'email' => '*****@*****.**', 'phone' => '555-32123'); $order->set_address($billing_address, 'billing'); // Add shipping costs $shipping_taxes = WC_Tax::calc_shipping_tax('10', WC_Tax::get_shipping_tax_rates()); $order->add_shipping(new WC_Shipping_Rate('flat_rate_shipping', 'Flat rate shipping', '10', $shipping_taxes, 'flat_rate')); // Set payment gateway $payment_gateways = WC()->payment_gateways->payment_gateways(); $order->set_payment_method($payment_gateways['bacs']); // Set totals $order->set_total(10, 'shipping'); $order->set_total(0, 'cart_discount'); $order->set_total(0, 'cart_discount_tax'); $order->set_total(0, 'tax'); $order->set_total(0, 'shipping_tax'); $order->set_total(40, 'total'); // 4 x $10 simple helper product return wc_get_order($order->id); }
/** * Create a order. * * @since 2.4 * * @return WC_Order Order object. */ public static function create_order($customer_id = 1) { // Create product $product = WC_Helper_Product::create_simple_product(); WC_Helper_Shipping::create_simple_flat_rate(); $order_data = array('status' => 'pending', 'customer_id' => $customer_id, 'customer_note' => '', 'total' => ''); $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Required, else wc_create_order throws an exception $order = wc_create_order($order_data); // Add order products $order->add_product($product, 4); // Set billing address $order->set_billing_first_name('Jeroen'); $order->set_billing_last_name('Sormani'); $order->set_billing_company('WooCompany'); $order->set_billing_address_1('WooAddress'); $order->set_billing_address_2(''); $order->set_billing_city('WooCity'); $order->set_billing_state('NY'); $order->set_billing_postcode('123456'); $order->set_billing_country('US'); $order->set_billing_email('*****@*****.**'); $order->set_billing_phone('555-32123'); // Add shipping costs $shipping_taxes = WC_Tax::calc_shipping_tax('10', WC_Tax::get_shipping_tax_rates()); $rate = new WC_Shipping_Rate('flat_rate_shipping', 'Flat rate shipping', '10', $shipping_taxes, 'flat_rate'); $item = new WC_Order_Item_Shipping(); $item->set_props(array('method_title' => $rate->label, 'method_id' => $rate->id, 'total' => wc_format_decimal($rate->cost), 'taxes' => $rate->taxes, 'meta_data' => $rate->get_meta_data())); $order->add_item($item); // Set payment gateway $payment_gateways = WC()->payment_gateways->payment_gateways(); $order->set_payment_method($payment_gateways['bacs']); // Set totals $order->set_shipping_total(10); $order->set_discount_total(0); $order->set_discount_tax(0); $order->set_cart_tax(0); $order->set_shipping_tax(0); $order->set_total(40); // 4 x $10 simple helper product $order->save(); return $order; }
/** * 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 shipping total * * @since 2.3 */ public function test_shipping_total() { // 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 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 product to cart WC()->cart->add_to_cart($product->id, 1); // Set the flat_rate shipping method WC()->session->set('chosen_shipping_methods', array('flat_rate')); WC()->cart->calculate_totals(); // Test if the shipping total amount is equal 20 $this->assertEquals(10, WC()->cart->shipping_total); // Test if the cart total amount is equal 20 $this->assertEquals(20, WC()->cart->total); // Clean up the cart WC()->cart->empty_cart(); // Delete the flat rate method WC()->session->set('chosen_shipping_methods', array()); \WC_Helper_Shipping::delete_simple_flat_rate(); // Delete product \WC_Helper_Product::delete_product($product->id); }