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