在 Woocommerce 3 中以编程方式向订单添加费用
Add a fee to an order programmatically in Woocommerce 3
我正在创建 Woocommerce 总计 'on-the-fly',因为我的购物车商品是从另一个 CMS 导入的。
目前我无法为每个订单'fee'设置自定义'fee',然后将订单标记为'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');
我们将不胜感激。
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 以来,它有点复杂。有一些参数可以设置为费用名称、税收状态、税收 class (如果需要) 和费用金额 (不含税) .
还要进行税费计算,根据税费设置,您需要设置一个至少包含客户国家/地区代码的数组(如果税费基于国家/地区)
假设以下代码中的费用金额变量名称是$imported_total_fee
:
$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();
经过测试,完美运行。
我正在创建 Woocommerce 总计 'on-the-fly',因为我的购物车商品是从另一个 CMS 导入的。
目前我无法为每个订单'fee'设置自定义'fee',然后将订单标记为'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');
我们将不胜感激。
The
WC_Abstract_Legacy_Order
methodadd_fee()
is deprecated andset_fee()
method doesn't exist for theWC_Order
Class (exist only forWC_Cart
andWC_API_Orders
classes).
以编程方式向订单添加费用 自 Woocommerce 3 以来,它有点复杂。有一些参数可以设置为费用名称、税收状态、税收 class (如果需要) 和费用金额 (不含税) .
还要进行税费计算,根据税费设置,您需要设置一个至少包含客户国家/地区代码的数组(如果税费基于国家/地区)
假设以下代码中的费用金额变量名称是$imported_total_fee
:
$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();
经过测试,完美运行。