woocommerce /
woocommerce-rest-api
| 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 |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 50 | * |
||||||
| 51 | * @return WC_Order |
||||||
|
0 ignored issues
–
show
|
|||||||
| 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 ); |
||||||
|
0 ignored issues
–
show
The method
add_item() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 83 | |||||||
| 84 | // Set billing address |
||||||
| 85 | $order->set_billing_first_name( 'Jeroen' ); |
||||||
|
0 ignored issues
–
show
The method
set_billing_first_name() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 86 | $order->set_billing_last_name( 'Sormani' ); |
||||||
|
0 ignored issues
–
show
The method
set_billing_last_name() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 87 | $order->set_billing_company( 'WooCompany' ); |
||||||
|
0 ignored issues
–
show
The method
set_billing_company() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 88 | $order->set_billing_address_1( 'WooAddress' ); |
||||||
|
0 ignored issues
–
show
The method
set_billing_address_1() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 89 | $order->set_billing_address_2( '' ); |
||||||
|
0 ignored issues
–
show
The method
set_billing_address_2() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 90 | $order->set_billing_city( 'WooCity' ); |
||||||
|
0 ignored issues
–
show
The method
set_billing_city() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 91 | $order->set_billing_state( 'NY' ); |
||||||
|
0 ignored issues
–
show
The method
set_billing_state() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 92 | $order->set_billing_postcode( '123456' ); |
||||||
|
0 ignored issues
–
show
The method
set_billing_postcode() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 93 | $order->set_billing_country( 'US' ); |
||||||
|
0 ignored issues
–
show
The method
set_billing_country() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 94 | $order->set_billing_email( '[email protected]' ); |
||||||
|
0 ignored issues
–
show
The method
set_billing_email() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 95 | $order->set_billing_phone( '555-32123' ); |
||||||
|
0 ignored issues
–
show
The method
set_billing_phone() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 96 | |||||||
| 97 | // Add shipping costs |
||||||
| 98 | $shipping_taxes = WC_Tax::calc_shipping_tax( '10', WC_Tax::get_shipping_tax_rates() ); |
||||||
|
0 ignored issues
–
show
'10' of type string is incompatible with the type double expected by parameter $price of WC_Tax::calc_shipping_tax().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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(); |
||||||
|
0 ignored issues
–
show
The method
payment_gateways() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 116 | $order->set_payment_method( $payment_gateways['bacs'] ); |
||||||
|
0 ignored issues
–
show
The method
set_payment_method() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 117 | |||||||
| 118 | // Set totals |
||||||
| 119 | $order->set_shipping_total( 10 ); |
||||||
|
0 ignored issues
–
show
The method
set_shipping_total() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 120 | $order->set_discount_total( 0 ); |
||||||
|
0 ignored issues
–
show
The method
set_discount_total() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 121 | $order->set_discount_tax( 0 ); |
||||||
|
0 ignored issues
–
show
The method
set_discount_tax() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 122 | $order->set_cart_tax( 0 ); |
||||||
|
0 ignored issues
–
show
The method
set_cart_tax() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 123 | $order->set_shipping_tax( 0 ); |
||||||
|
0 ignored issues
–
show
The method
set_shipping_tax() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 124 | $order->set_total( 50 ); // 4 x $10 simple helper product |
||||||
|
0 ignored issues
–
show
The method
set_total() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 125 | $order->save(); |
||||||
|
0 ignored issues
–
show
The method
save() does not exist on WP_Error.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 126 | |||||||
| 127 | return $order; |
||||||
| 128 | } |
||||||
| 129 | } |
||||||
| 130 |