Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Woocommerce 3中以编程方式向订单添加费用_Php_Wordpress_Woocommerce_Orders_Fee - Fatal编程技术网

Php 在Woocommerce 3中以编程方式向订单添加费用

Php 在Woocommerce 3中以编程方式向订单添加费用,php,wordpress,woocommerce,orders,fee,Php,Wordpress,Woocommerce,Orders,Fee,我正在“动态”创建Woocommerce总计,因为我的购物车项目是从另一个CMS导入的 当前我无法为每个订单设置自定义“费用”,然后将订单标记为“暂停”: $order->set_date_created($creation_tsz); $order->set_address( $address, 'billing' ); $order->set_address( $address

我正在“动态”创建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');
任何关于这方面的线索都将不胜感激

WC\u-Abstract\u-Legacy\u-Order
方法和
set\u-fee()
方法不适用于
WC\u-Order
类(仅适用于
WC\u-Cart
WC\u-API\u-Orders
类)

从Woocommerce 3开始,以编程方式向订单中添加费用就有点复杂了。有一些参数可以设置为费用名称、税务状态、税务类别(如果需要)和费用金额(不含税)

此外,要进行税务计算,根据税务设置,您需要设置一个数组,其中至少包含客户国家代码(如果税务基于国家)

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

经过测试,工作正常。

什么问题,请告诉我们您遇到的错误。设置费用功能的代码。啊,设置费用不是标准功能吗?也许这就是我被引入歧途的地方。目前,我添加费用并计算总数,但当我查看订单时,费用和总数均为0.00。那么order=0.00该代码将添加到哪里?我在做一个类似的项目,我需要在手动创建的订单上动态增加处理费。。。