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

How to calculate handling fee based on currency and attribute value of cart items in WooCommerce

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

计算分为两部分:

  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;
}

通过这种方式,您只会得到运费百分比 (如果它大于零) 和最后一个迭代购物车商品的费用之和。

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

If you want to apply the standard tax rate, assign a blank value to the fourth parameter of the add_fee method, as reported in the documentation.

代码还有其他优化,试试这个:

// 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' );
    
}

代码应该有效。将它添加到您的活动主题 functions.php.