2015-05-23 08:52:06 +00:00
|
|
|
<?php
|
2019-12-06 00:16:44 +00:00
|
|
|
/**
|
|
|
|
* Order helpers.
|
|
|
|
*
|
2020-08-05 16:36:24 +00:00
|
|
|
* @package WooCommerce\Tests
|
2019-12-06 00:16:44 +00:00
|
|
|
*/
|
2015-05-23 08:52:06 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Class WC_Helper_Order.
|
2015-05-23 08:52:06 +00:00
|
|
|
*
|
2015-11-03 13:31:20 +00:00
|
|
|
* This helper class should ONLY be used for unit tests!.
|
2015-05-23 08:52:06 +00:00
|
|
|
*/
|
|
|
|
class WC_Helper_Order {
|
|
|
|
|
|
|
|
/**
|
2020-03-04 11:30:17 +00:00
|
|
|
* Delete an order.
|
2015-05-23 08:52:06 +00:00
|
|
|
*
|
|
|
|
* @param int $order_id ID of the order to delete.
|
|
|
|
*/
|
|
|
|
public static function delete_order( $order_id ) {
|
|
|
|
|
2015-07-30 18:07:34 +00:00
|
|
|
$order = wc_get_order( $order_id );
|
2015-05-23 08:52:06 +00:00
|
|
|
|
2017-11-21 17:04:45 +00:00
|
|
|
// Delete all products in the order.
|
|
|
|
foreach ( $order->get_items() as $item ) {
|
2015-05-23 08:52:06 +00:00
|
|
|
WC_Helper_Product::delete_product( $item['product_id'] );
|
2017-11-21 17:04:45 +00:00
|
|
|
}
|
2015-05-23 08:52:06 +00:00
|
|
|
|
|
|
|
WC_Helper_Shipping::delete_simple_flat_rate();
|
|
|
|
|
2017-11-21 17:04:45 +00:00
|
|
|
// Delete the order post.
|
|
|
|
$order->delete( true );
|
2015-05-23 08:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a order.
|
|
|
|
*
|
2017-01-05 09:23:22 +00:00
|
|
|
* @since 2.4
|
2017-03-15 16:36:53 +00:00
|
|
|
* @version 3.0 New parameter $product.
|
2015-05-23 08:52:06 +00:00
|
|
|
*
|
2019-12-06 00:16:44 +00:00
|
|
|
* @param int $customer_id The ID of the customer the order is for.
|
|
|
|
* @param WC_Product $product The product to add to the order.
|
2017-01-05 09:23:22 +00:00
|
|
|
*
|
|
|
|
* @return WC_Order
|
2015-05-23 08:52:06 +00:00
|
|
|
*/
|
2017-01-05 09:23:22 +00:00
|
|
|
public static function create_order( $customer_id = 1, $product = null ) {
|
|
|
|
|
|
|
|
if ( ! is_a( $product, 'WC_Product' ) ) {
|
|
|
|
$product = WC_Helper_Product::create_simple_product();
|
|
|
|
}
|
2015-05-23 08:52:06 +00:00
|
|
|
|
|
|
|
WC_Helper_Shipping::create_simple_flat_rate();
|
|
|
|
|
|
|
|
$order_data = array(
|
|
|
|
'status' => 'pending',
|
2016-03-15 18:37:42 +00:00
|
|
|
'customer_id' => $customer_id,
|
2015-05-23 08:52:06 +00:00
|
|
|
'customer_note' => '',
|
2015-05-23 08:57:06 +00:00
|
|
|
'total' => '',
|
2015-05-23 08:52:06 +00:00
|
|
|
);
|
|
|
|
|
2019-12-06 00:16:44 +00:00
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Required, else wc_create_order throws an exception.
|
2019-05-01 22:05:00 +00:00
|
|
|
$order = wc_create_order( $order_data );
|
2015-05-23 08:52:06 +00:00
|
|
|
|
2019-12-06 00:16:44 +00:00
|
|
|
// Add order products.
|
2017-04-06 10:02:06 +00:00
|
|
|
$item = new WC_Order_Item_Product();
|
2019-05-01 22:05:00 +00:00
|
|
|
$item->set_props(
|
|
|
|
array(
|
|
|
|
'product' => $product,
|
|
|
|
'quantity' => 4,
|
|
|
|
'subtotal' => wc_get_price_excluding_tax( $product, array( 'qty' => 4 ) ),
|
|
|
|
'total' => wc_get_price_excluding_tax( $product, array( 'qty' => 4 ) ),
|
|
|
|
)
|
|
|
|
);
|
2017-04-06 10:02:06 +00:00
|
|
|
$item->save();
|
|
|
|
$order->add_item( $item );
|
2015-05-23 08:52:06 +00:00
|
|
|
|
2019-12-06 00:16:44 +00:00
|
|
|
// Set billing address.
|
2016-08-22 16:03:59 +00:00
|
|
|
$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' );
|
2019-12-06 00:16:44 +00:00
|
|
|
$order->set_billing_postcode( '12345' );
|
2016-08-22 16:03:59 +00:00
|
|
|
$order->set_billing_country( 'US' );
|
|
|
|
$order->set_billing_email( 'admin@example.org' );
|
|
|
|
$order->set_billing_phone( '555-32123' );
|
2015-05-23 08:52:06 +00:00
|
|
|
|
2019-12-06 00:16:44 +00:00
|
|
|
// Add shipping costs.
|
2015-05-23 08:52:06 +00:00
|
|
|
$shipping_taxes = WC_Tax::calc_shipping_tax( '10', WC_Tax::get_shipping_tax_rates() );
|
2019-05-01 22:05:00 +00:00
|
|
|
$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,
|
|
|
|
)
|
|
|
|
);
|
2017-01-23 11:30:53 +00:00
|
|
|
foreach ( $rate->get_meta_data() as $key => $value ) {
|
|
|
|
$item->add_meta_data( $key, $value, true );
|
|
|
|
}
|
2016-08-22 15:48:19 +00:00
|
|
|
$order->add_item( $item );
|
2015-05-23 08:52:06 +00:00
|
|
|
|
2019-12-06 00:16:44 +00:00
|
|
|
// Set payment gateway.
|
2015-05-23 08:52:06 +00:00
|
|
|
$payment_gateways = WC()->payment_gateways->payment_gateways();
|
|
|
|
$order->set_payment_method( $payment_gateways['bacs'] );
|
|
|
|
|
2019-12-06 00:16:44 +00:00
|
|
|
// Set totals.
|
2016-06-22 11:29:23 +00:00
|
|
|
$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 );
|
2017-08-10 10:38:09 +00:00
|
|
|
$order->set_total( 50 ); // 4 x $10 simple helper product
|
2016-08-15 10:15:03 +00:00
|
|
|
$order->save();
|
2016-06-22 11:29:23 +00:00
|
|
|
|
2016-08-15 10:15:03 +00:00
|
|
|
return $order;
|
2015-05-23 08:52:06 +00:00
|
|
|
}
|
2020-11-11 13:17:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to create order with fees and shipping objects.
|
|
|
|
*
|
|
|
|
* @param int $customer_id The ID of the customer the order is for.
|
|
|
|
* @param WC_Product $product The product to add to the order.
|
|
|
|
*
|
|
|
|
* @return WC_Order
|
|
|
|
*/
|
|
|
|
public static function create_order_with_fees_and_shipping( $customer_id = 1, $product = null ) {
|
|
|
|
$order = self::create_order( $customer_id, $product );
|
|
|
|
|
|
|
|
$fee_item = new WC_Order_Item_Fee();
|
|
|
|
$fee_item->set_order_id( $order->get_id() );
|
|
|
|
$fee_item->set_name( 'Testing fees' );
|
|
|
|
$fee_item->set_total( 100 );
|
|
|
|
|
|
|
|
$shipping_item = new WC_Order_Item_Shipping();
|
|
|
|
$shipping_item->set_order_id( $order->get_id() );
|
|
|
|
$shipping_item->set_name( 'Flat shipping' );
|
|
|
|
$shipping_item->set_total( 25 );
|
|
|
|
|
|
|
|
$order->add_item( $fee_item );
|
|
|
|
$order->add_item( $shipping_item );
|
|
|
|
$order->save();
|
|
|
|
$order->calculate_totals( true );
|
|
|
|
|
|
|
|
return $order;
|
|
|
|
}
|
2015-05-23 08:52:06 +00:00
|
|
|
}
|