Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a fee to an order programmatically in Woocommerce 3

I'm creating Woocommerce totals 'on-the-fly' as my cart items are imported from another CMS.

Currently I am having trouble setting a custom 'fee' for each order, then marking the order as 'on-hold':

                $order->set_date_created($creation_tsz);

                $order->set_address( $address, 'billing' );
                $order->set_address( $address, 'shipping' );
                $order->set_currency('GBP');

                $order->add_fee('Imported Total', $imported_total_here);
                $order->set_fee();

                $order->calculate_totals();

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

Any track on this will be appreciated.

like image 580
Callum Avatar asked Dec 03 '18 23:12

Callum


People also ask

How do I add fees in WooCommerce?

Go to: Products > Add Product.Under Product Data, click on the Additional Fees tab to configure fees for the current product. Product fees are applied in addition to the total cart/order fee.

How do I create a custom order in WooCommerce programmatically?

function create_vip_order() { global $woocommerce; $address = array( 'first_name' => '111Joe', 'last_name' => 'Conlin', 'company' => 'Speed Society', 'email' => '[email protected]', 'phone' => '760-555-1212', 'address_1' => '123 Main st.

How do I set shipping method in WooCommerce programmatically?

1/ Go to: WooCommerce > Settings > Shipping. 2/ Hover over the Shipping Zone location that Flat Rate should be added to and select Edit. ... 3/ In the Shipping Methods box, select Add Shipping Method. 4/ Select Flat Rate from the dropdown.


1 Answers

The WC_Abstract_Legacy_Order method add_fee() is deprecated and set_fee() method doesn't exist for the WC_Order Class (exist only for WC_Cart and WC_API_Orders classes).

To add a Fee to an order programmatically since Woocommerce 3, it's a bit more complicated. There are some parameters to set as the Fee name, the tax status, the tax class (if needed) and the fee amount (excl. taxes).

Also to make the tax calculations, depending on the taxes settings, you will need to set an array containing at minima the customer country code (if the taxes are based on the country)

Let say that the fee amount variable name is $imported_total_fee in the code below:

$order->set_date_created($creation_tsz);

$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
$order->set_currency('GBP');

## ------------- ADD FEE 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' => '', 
    'postcode' => '', 
    'city' => ''
);

// Get a new instance of the WC_Order_Item_Fee Object
$item_fee = new WC_Order_Item_Fee();

$item_fee->set_name( "Fee" ); // Generic fee name
$item_fee->set_amount( $imported_total_fee ); // Fee amount
$item_fee->set_tax_class( '' ); // default for ''
$item_fee->set_tax_status( 'taxable' ); // or 'none'
$item_fee->set_total( $imported_total_fee ); // Fee amount

// Calculating Fee taxes
$item_fee->calculate_taxes( $calculate_tax_for );

// Add Fee item to the order
$order->add_item( $item_fee );

## ----------------------------------------------- ##

$order->calculate_totals();

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

$order->save();

Tested and perfectly works.

like image 198
LoicTheAztec Avatar answered Oct 19 '22 05:10

LoicTheAztec