在Woocommerce 3中以编程方式向订单添加费用 [英] Add a fee to an order programmatically in Woocommerce 3

查看:139
本文介绍了在Woocommerce 3中以编程方式向订单添加费用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我的购物车商品是从另一个CMS导入的,因此我正在即时"创建Woocommerce总计.

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.

推荐答案

WC_Abstract_Legacy_Order方法 add_fee()是对于WC_Order(仅对WC_CartWC_API_Orders类存在),不存在不推荐使用的set_fee()方法.

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).

从Woocommerce 3开始,

要以编程方式向订单添加费用,这要复杂一些.有一些参数可以设置为费用名称,税收状态,税收类别(如果需要)和费用金额(不含税).

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)

在下面的代码中,费用金额变量名称为$imported_total_fee:

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.

这篇关于在Woocommerce 3中以编程方式向订单添加费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆