| Conditions | 3 |
| Paths | 4 |
| Total Lines | 76 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 |