Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a shipping to an order programmatically in Woocommerce 3

I've been trying numerous solutions to programmatically set the price of shipping (per order) via Woocommerce. I'm having no luck

I have tried overwriting the meta value for the item:

            update_post_meta( $woo_order_id, '_order_shipping', $new_ship_price );

Also tried something along the lines of this [Question/Answer][1]

                    $item_ship = new WC_Order_Item_Shipping();

                    $item_ship->set_name( "flat_rate" );
                    $item_ship->set_amount( $new_ship_price );
                    $item_ship->set_tax_class( '' );
                    $item_ship->set_tax_status( 'none' );


                    // Add Shipping item to the order
                    $order->add_item( $item_ship );

But neither seem to work. Any pointers most welcome.


Similar thread: Add a fee to an order programmatically in Woocommerce 3

like image 317
Callum Avatar asked Dec 07 '18 14:12

Callum


People also ask

How do I add shipping per item in WooCommerce?

Go to WooCommerce > Settings > Shipping and select the shipping zone you want to add Per-Product Shipping to. Then, click “Add shipping method” and add Per-Product as a shipping method. This is required for the Standalone Method so that Per-Product Shipping will be displayed as a shipping method at checkout.

How do I add custom shipping charges in WooCommerce?

Head to: WooCommerce > Settings > Shipping. Select the Shipping Zone that Flat Rate should be added to and select Edit. In the Shipping Methods box, select Add Shipping Method. Select Flat Rate shipping from the dropdown and then Add Shipping method.

How do I create an order programmatically in WooCommerce?

php $order->add_product( get_product( '129' ), 1 ); // This is an existing SIMPLE product $order->set_address( $address, 'billing' ); // $order->calculate_totals(); $order->update_status("Completed", 'Imported order', TRUE); } add_action( 'init', 'create_vip_order' );


1 Answers

To handle this in Woocommerce 3+ use the following (from a WC_Order Order object $order):

## ------------- ADD SHIPPING PROCESS ---------------- ##

// Get the customer country code
$country_code = $order->get_shipping_country();

// Set the array for tax calculations
$calculate_tax_for = array(
    'country' => $country_code,
    'state' => '', // Can be set (optional)
    'postcode' => '', // Can be set (optional)
    'city' => '', // Can be set (optional)
);

// Optionally, set a total shipping amount
$new_ship_price = 5.10;

// Get a new instance of the WC_Order_Item_Shipping Object
$item = new WC_Order_Item_Shipping();

$item->set_method_title( "Flat rate" );
$item->set_method_id( "flat_rate:14" ); // set an existing Shipping method rate ID
$item->set_total( $new_ship_price ); // (optional)
$item->calculate_taxes($calculate_tax_for);

$order->add_item( $item );

$order->calculate_totals();

$order->update_status('on-hold');

// $order->save(); // If you don't update the order status

Tested an works.

like image 147
LoicTheAztec Avatar answered Sep 25 '22 00:09

LoicTheAztec