1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Order helpers. |
4
|
|
|
* |
5
|
|
|
* @package WooCommerce/Tests |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class WC_Helper_Order. |
10
|
|
|
* |
11
|
|
|
* This helper class should ONLY be used for unit tests!. |
12
|
|
|
*/ |
13
|
|
|
class WC_Helper_Order { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Delete an order. |
17
|
|
|
* |
18
|
|
|
* @param int $order_id ID of the order to delete. |
19
|
|
|
*/ |
20
|
|
|
public static function delete_order( $order_id ) { |
21
|
|
|
|
22
|
|
|
$order = wc_get_order( $order_id ); |
23
|
|
|
|
24
|
|
|
// Delete all products in the order. |
25
|
|
|
foreach ( $order->get_items() as $item ) { |
26
|
|
|
WC_Helper_Product::delete_product( $item['product_id'] ); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
WC_Helper_Shipping::delete_simple_flat_rate(); |
30
|
|
|
|
31
|
|
|
// Delete the order post. |
32
|
|
|
$order->delete( true ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create a order. |
37
|
|
|
* |
38
|
|
|
* @since 2.4 |
39
|
|
|
* @version 3.0 New parameter $product. |
40
|
|
|
* |
41
|
|
|
* @param int $customer_id The ID of the customer the order is for. |
42
|
|
|
* @param int $total Total cost of the order. Defaults to 50 (4 x $10 simple helper product + $10 shipping) |
43
|
|
|
* and can be modified to test $0 orders. |
44
|
|
|
* @param WC_Product $product The product to add to the order. |
45
|
|
|
* |
46
|
|
|
* @return WC_Order |
47
|
|
|
*/ |
48
|
|
|
public static function create_order( $customer_id = 1, $total = 50, $product = null ) { |
49
|
|
|
|
50
|
|
|
if ( ! is_a( $product, 'WC_Product' ) ) { |
51
|
|
|
$product = WC_Helper_Product::create_simple_product(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
WC_Helper_Shipping::create_simple_flat_rate(); |
55
|
|
|
|
56
|
|
|
$order_data = [ |
57
|
|
|
'status' => 'pending', |
58
|
|
|
'customer_id' => $customer_id, |
59
|
|
|
'customer_note' => '', |
60
|
|
|
'total' => '', |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Required, else wc_create_order throws an exception. |
64
|
|
|
$order = wc_create_order( $order_data ); |
65
|
|
|
|
66
|
|
|
// Add order products. |
67
|
|
|
$item = new WC_Order_Item_Product(); |
68
|
|
|
$item->set_props( |
69
|
|
|
[ |
70
|
|
|
'product' => $product, |
71
|
|
|
'quantity' => 4, |
72
|
|
|
'subtotal' => wc_get_price_excluding_tax( $product, [ 'qty' => 4 ] ), |
73
|
|
|
'total' => wc_get_price_excluding_tax( $product, [ 'qty' => 4 ] ), |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
$item->save(); |
77
|
|
|
$order->add_item( $item ); |
78
|
|
|
|
79
|
|
|
// Set billing address. |
80
|
|
|
$order->set_billing_first_name( 'Jeroen' ); |
81
|
|
|
$order->set_billing_last_name( 'Sormani' ); |
82
|
|
|
$order->set_billing_company( 'WooCompany' ); |
83
|
|
|
$order->set_billing_address_1( 'WooAddress' ); |
84
|
|
|
$order->set_billing_address_2( '' ); |
85
|
|
|
$order->set_billing_city( 'WooCity' ); |
86
|
|
|
$order->set_billing_state( 'NY' ); |
87
|
|
|
$order->set_billing_postcode( '12345' ); |
88
|
|
|
$order->set_billing_country( 'US' ); |
89
|
|
|
$order->set_billing_email( '[email protected]' ); |
90
|
|
|
$order->set_billing_phone( '555-32123' ); |
91
|
|
|
|
92
|
|
|
// Add shipping costs. |
93
|
|
|
$shipping_taxes = WC_Tax::calc_shipping_tax( '10', WC_Tax::get_shipping_tax_rates() ); |
94
|
|
|
$rate = new WC_Shipping_Rate( 'flat_rate_shipping', 'Flat rate shipping', '10', $shipping_taxes, 'flat_rate' ); |
95
|
|
|
$item = new WC_Order_Item_Shipping(); |
96
|
|
|
$item->set_props( |
97
|
|
|
[ |
98
|
|
|
'method_title' => $rate->label, |
99
|
|
|
'method_id' => $rate->id, |
100
|
|
|
'total' => wc_format_decimal( $rate->cost ), |
101
|
|
|
'taxes' => $rate->taxes, |
102
|
|
|
] |
103
|
|
|
); |
104
|
|
|
foreach ( $rate->get_meta_data() as $key => $value ) { |
105
|
|
|
$item->add_meta_data( $key, $value, true ); |
106
|
|
|
} |
107
|
|
|
$order->add_item( $item ); |
108
|
|
|
|
109
|
|
|
// Set payment gateway. |
110
|
|
|
$payment_gateways = WC()->payment_gateways->payment_gateways(); |
111
|
|
|
$order->set_payment_method( $payment_gateways['bacs'] ); |
112
|
|
|
|
113
|
|
|
// Set totals. |
114
|
|
|
$order->set_shipping_total( 10 ); |
115
|
|
|
$order->set_discount_total( 0 ); |
116
|
|
|
$order->set_discount_tax( 0 ); |
117
|
|
|
$order->set_cart_tax( 0 ); |
118
|
|
|
$order->set_shipping_tax( 0 ); |
119
|
|
|
$order->set_total( $total ); |
120
|
|
|
$order->save(); |
121
|
|
|
|
122
|
|
|
return $order; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|