如何根据WooCommerce中购物车商品的货币和属性值计算手续费



我有一个在Wooccommerce网站上运行的代码片段,它计算并将手续费添加到购物车总额中。

计算有两个部分:

  1. 如果运费大于0,则加运费的18%
  2. 如果购物车中的产品具有特定属性,则根据购物车的货币添加固定费用

该代码段可以完成所有这些工作,但如果购物车包含混合产品,则会失败。

也就是说,如果一个产品具有特定属性,而另一个产品不具有该属性,则不添加设定费用。

如果购物车中至少有一个产品具有此属性,我需要添加设置费用。这是代码:

<?php
// Hook to add a handling fee
add_action( 'woocommerce_cart_calculate_fees','handling_fee' );
function handling_fee($cart_object) {
global $woocommerce;
$specialfeeattr = 71; // attribute id for the special fee
$spfee = 0.00; // initialize special fee
$percentage = 0.18; // percentage to add to shipping total
$orderFeeUSD = 3.20; //fee per transaction USD
$orderFeeCAD = 4.00; //fee per transaction CAD
$orderFeeEUR = 2.62; //fee per transaction EUR
$orderFeeGBP = 2.26; //fee per transaction GBP
$currentCurrency = get_woocommerce_currency();
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( $cart_object->cart_contents as $key => $value ) {
$proid = $value['product_id']; //get the product id from cart
$product = wc_get_product( $proid );
$attr =  $product->get_attributes();
//check if attributes array has fulfillment attribute included
if (array_key_exists ('pa_fulfillment', $attr )) {
$attrFulfillment = $attr['pa_fulfillment'];
}
if (isset($attrFulfillment)) {
$attrFulfillmentOptions = $attrFulfillment['options'];
}
if ($woocommerce->cart->shipping_total > 0) {
$spfee = (($woocommerce->cart->shipping_total) * $percentage);
}
//only check if fullfillment option is set to warehouse if fulfillment is not null
if (isset($attrFulfillmentOptions)) {
//if the product in cart contains attr id 71
if (in_array($specialfeeattr, $attrFulfillmentOptions )) {
if($currentCurrency == 'USD'){
$spfee += $orderFeeUSD;
} elseif($currentCurrency == 'CAD') {
$spfee += $orderFeeCAD;
} elseif($currentCurrency == 'EUR') {
$spfee += $orderFeeEUR;
} elseif($currentCurrency == 'GBP') {
$spfee += $orderFeeGBP;
}
}
}
}
$woocommerce->cart->add_fee( 'Handling', $spfee, true, 'standard' );
}
?>

非常感谢你对如何解决这个问题的任何建议。

您在每次迭代中都使用以下行初始化$spfee变量:

if ( $woocommerce->cart->shipping_total > 0 ) {
$spfee = $woocommerce->cart->shipping_total * $percentage;
}

通过这种方式,您只能获得运输的百分比(如果大于零(和最后一个迭代购物车项目的费用之和。

然后尝试在循环开始之前插入前几行代码。

如果要应用标准税率,请为add_fee方法的第四个参数指定一个空白值,如文档中所述。

还有其他对代码的优化,请尝试以下操作:

// Hook to add a handling fee
add_action( 'woocommerce_cart_calculate_fees','handling_fee' );
function handling_fee( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
global $woocommerce;
$specialfeeattr  = 71; // attribute id for the special fee
$spfee           = 0.00; // initialize special fee
$percentage      = 0.18; // percentage to add to shipping total
$orderFeeUSD     = 3.20; //fee per transaction USD
$orderFeeCAD     = 4.00; //fee per transaction CAD
$orderFeeEUR     = 2.62; //fee per transaction EUR
$orderFeeGBP     = 2.26; //fee per transaction GBP
$currentCurrency = get_woocommerce_currency();
if ( $woocommerce->cart->shipping_total > 0 ) {
$spfee = $woocommerce->cart->shipping_total * $percentage;
}
foreach ( $cart_object->cart_contents as $key => $value ) {
$proid   = $value['product_id']; //get the product id from cart
$product = wc_get_product( $proid );
$attr    = $product->get_attributes();
//check if attributes array has fulfillment attribute included
if ( array_key_exists ( 'pa_fulfillment', $attr ) ) {
$attrFulfillment = $attr['pa_fulfillment'];
}
if ( isset($attrFulfillment) ) {
$attrFulfillmentOptions = $attrFulfillment['options'];
}
// only check if fullfillment option is set to warehouse if fulfillment is not null
if ( isset($attrFulfillmentOptions) ) {
// if the product in cart contains attr id 71
if ( in_array( $specialfeeattr, $attrFulfillmentOptions ) ) {
switch ( $currentCurrency ) {
case 'USD':
$spfee += $orderFeeUSD;
break;
case 'CAD':
$spfee += $orderFeeCAD;
break;
case 'EUR':
$spfee += $orderFeeEUR;
break;
case 'GBP':
$spfee += $orderFeeGBP;
break;
}
}
}
}
$woocommerce->cart->add_fee( 'Handling', $spfee, true, 'standard' );

}

代码应该可以工作。将其添加到活动主题的函数.php中。

最新更新