【问题标题】:Add a fee to an order programmatically in Woocommerce 3在 Woocommerce 3 中以编程方式向订单添加费用
【发布时间】:2019-05-05 08:05:12
【问题描述】:

我正在“即时”创建 Woocommerce 总计,因为我的购物车商品是从另一个 CMS 导入的。

目前我无法为每个订单设置自定义“费用”,然后将订单标记为“暂停”:

                $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');

对此的任何跟踪将不胜感激。

【问题讨论】:

  • 什么样的问题,给我们你得到的错误。以及set_fee函数的代码。
  • 啊,set_fee 不是标准函数吗?也许这就是我误入歧途的地方。目前我 add_fee 并计算总数,但是当我查看订单时,费用和总数都存在 0.00。所以订单 = 0.00

标签: php wordpress woocommerce orders fee


【解决方案1】:

WC_Abstract_Legacy_Order 方法 add_fee() is deprecatedset_fee() 方法不存在于 WC_Order(仅存在于 WC_CartWC_API_Orders 类)

要以编程方式向订单添加费用,自 Woocommerce 3 以来,它有点复杂。有一些参数可以设置为费用名称、税务状态、税级(如果需要)和费用金额(不含税)

还要进行税收计算,根据税收设置,您需要设置一个数组,其中包含最低限度的客户国家代码(如果税收基于国家/地区)

假设在下面的代码中,费用金额变量名称是$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();

经过测试并且完美运行。

【讨论】:

  • 这段代码会添加到哪里?我正在从事类似的项目,我需要在手动创建的订单上动态添加处理费......
猜你喜欢
  • 2019-05-09
  • 1970-01-01
  • 2021-01-15
  • 2014-12-22
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 2013-03-19
  • 2018-07-17
相关资源
最近更新 更多