WC_Checkout::create_order_shipping_lines()
Add shipping lines to the order.
Method of the class: WC_Checkout{}
Hooks from the method
Return
null
. Nothing (null).
Usage
$WC_Checkout = new WC_Checkout(); $WC_Checkout->create_order_shipping_lines( $order, $chosen_shipping_methods, $packages );
- $order(WC_Order) (required) (passed by reference — &)
- Order Instance.
- $chosen_shipping_methods(array) (required)
- Chosen shipping methods.
- $packages(array) (required)
- Packages.
WC_Checkout::create_order_shipping_lines() WC Checkout::create order shipping lines code WC 9.7.1
public function create_order_shipping_lines( &$order, $chosen_shipping_methods, $packages ) { foreach ( $packages as $package_key => $package ) { if ( isset( $chosen_shipping_methods[ $package_key ], $package['rates'][ $chosen_shipping_methods[ $package_key ] ] ) ) { $shipping_rate = $package['rates'][ $chosen_shipping_methods[ $package_key ] ]; $item = new WC_Order_Item_Shipping(); $item->legacy_package_key = $package_key; // @deprecated 4.4.0 For legacy actions. $item->set_props( array( 'method_title' => $shipping_rate->label, 'method_id' => $shipping_rate->method_id, 'instance_id' => $shipping_rate->instance_id, 'total' => wc_format_decimal( $shipping_rate->cost ), 'taxes' => array( 'total' => $shipping_rate->taxes, ), 'tax_status' => $shipping_rate->tax_status, ) ); foreach ( $shipping_rate->get_meta_data() as $key => $value ) { $item->add_meta_data( $key, $value, true ); } /** * Action hook to adjust item before save. * * @since 3.0.0 */ do_action( 'woocommerce_checkout_create_order_shipping_item', $item, $package_key, $package, $order ); // Add item to order and save. $order->add_item( $item ); } } }