Woocommerce在购物车之外添加费用
我想在商业订单上加一笔费用。我发现了许多这样的例子: $woocommerce->cart->add_fee() 但我需要从一个函数中向现有(处理或预定)订单添加费用 我可以打电话给add_fee()吗 例如,如果我想增加一笔15美元的费用,称为“期权调整”,但不征税,我可以简单地做一些类似的事情吗 增加费用(“期权调整”,15,$TAXED=false,$TAXE\U class=”) 当然,问题是,在购物车之外添加费用无法告诉我要将费用添加到哪个订单 我一直在看这个: 这让我想知道我是否可以在WC_Abstract_order中调用add_fee,方法如下: $order=新WC\U订单($order\U id) 但我不确定具体细节和语法是什么Woocommerce在购物车之外添加费用,woocommerce,orders,Woocommerce,Orders,我想在商业订单上加一笔费用。我发现了许多这样的例子: $woocommerce->cart->add_fee() 但我需要从一个函数中向现有(处理或预定)订单添加费用 我可以打电话给add_fee()吗 例如,如果我想增加一笔15美元的费用,称为“期权调整”,但不征税,我可以简单地做一些类似的事情吗 增加费用(“期权调整”,15,$TAXED=false,$TAXE\U class=”) 当然,问题是,在购物车之外添加费用无法告诉我要将费用添加到哪个订单 我一直在看这个: 这让我想知道我是否可以
任何帮助都将不胜感激 我能够使用以下方法成功地从我的functions.php添加woocommerce费用。具体来说,如果用户使用重力表单编辑其价格相关选项,我从“提交后的gform_”操作中调用它来调整计划付款 注意:对于要添加费用的woocommerce订单,需要订单ID($Order\u ID) 1:设置商业费用对象
$feename = 'Option Adjustment';
$feeamount = 40;
//The following sets up the fee object in a format accepted by woocommerce
$fee = array('name' => $feename, 'amount' => $feeamount, 'taxable' => false, 'tax_class' => '');
二,。调用添加费用的函数(#3)
三,。添加费用的功能(改编自)
四,。最后,您可能希望重新计算您的总数(上面使用“blb_calculate_totals”调用并根据改编)
这对我来说是行不通的,因为我需要费用来适应我正在编写的一段代码。我最终解决了一些问题。我将在下面分享。
blb_add_fee($fee, $order_id);
function blb_add_fee( $fee, $order_id ) {
$item_id = wc_add_order_item( $order_id, array(
'order_item_name' => $fee['name'],
'order_item_type' => 'fee'
) );
if ( ! $item_id ) {
return false;
}
if ( $fee['taxable'] ) {
wc_add_order_item_meta( $item_id, '_tax_class', $fee['tax_class'] );
} else {
wc_add_order_item_meta( $item_id, '_tax_class', '0' );
}
wc_add_order_item_meta( $item_id, '_line_total', wc_format_decimal( $fee['amount'] ) );
wc_add_order_item_meta( $item_id, '_line_tax', wc_format_decimal( $fee['tax'] ) );
// Save tax data - Since 2.2
$tax_data = array_map( 'wc_format_decimal', $fee['tax_data'] );
wc_add_order_item_meta( $item_id, '_line_tax_data', array( 'total' => $tax_data ) );
do_action( 'woocommerce_order_add_fee', $order_id, $item_id, $fee );
//Remove the following line (blb_calculate_totals) if you dont need to recalculate your totals
blb_calculate_totals( $and_taxes = false, $order_id );
return $item_id;
}
function blb_calculate_totals( $and_taxes = false, $order_id ) {
$cart_subtotal = 0;
$cart_total = 0;
$fee_total = 0;
$cart_subtotal_tax = 0;
$cart_total_tax = 0;
/*
if ( $and_taxes && wc_tax_enabled() ) {
$this->calculate_taxes();
}
*/
// line items
$order = new WC_Order( $order_id );
foreach ( $order->get_items() as $item ) {
$cart_subtotal += wc_format_decimal( isset( $item['line_subtotal'] ) ? $item['line_subtotal'] : 0 );
$cart_total += wc_format_decimal( isset( $item['line_total'] ) ? $item['line_total'] : 0 );
$cart_subtotal_tax += wc_format_decimal( isset( $item['line_subtotal_tax'] ) ? $item['line_subtotal_tax'] : 0 );
$cart_total_tax += wc_format_decimal( isset( $item['line_tax'] ) ? $item['line_tax'] : 0 );
}
//$this->calculate_shipping();
foreach ( $order->get_fees() as $item ) {
$fee_total += $item['line_total'];
}
$order->set_total( $cart_subtotal - $cart_total, 'cart_discount' );
$order->set_total( $cart_subtotal_tax - $cart_total_tax, 'cart_discount_tax' );
$grand_total = round( $cart_total + $fee_total + $order->get_total_shipping() + $order->get_cart_tax() + $order->get_shipping_tax(), wc_get_price_decimals() );
$order->set_total( $grand_total, 'total' );
return $grand_total;
}