【问题标题】:Add shipping cost on per product based on category and country根据类别和国家/地区添加每种产品的运费
【发布时间】:2016-12-16 09:04:14
【问题描述】:

在我的 WooCommerce 网站上,我有一些可以正常工作但效果不佳的代码。

国家/地区的计算工作正常,但是当我添加类别时价格不正确。

这是我的代码:

add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );

function calculate_discounted_price( $price, $values ) {
    global $woocommerce, $product;

    $countryArray = array( 'AL', 'AD', 'AT', 'BY', 'BE', 'BA', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FO', 'FI', 'FR', 'DE', 'GI', 'GR', 'HU', 'IS', 'IE', 'IM', 'IT', 'RS', 'LV', 'LI', 'LT', 'LU', 'MK', 'MT', 'MD', 'MC', 'ME', 'NL', 'NO', 'PL', 'PT', 'RO', 'RU', 'SM', 'RS', 'SK', 'SI', 'ES', 'SE', 'CH', 'UA', 'VA', 'RS' );
    $catArray = array('handbags','kids','hats');


                if( $woocommerce->customer->get_shipping_country() == 'GB' ) {

                    foreach ( $woocommerce->cart->cart_contents as $product ) :
                    if ( has_term( $catArray, 'product_cat', $product['product_id'] ) ) {
                        $price += 4.50;
                    } else {
                        $price += 8.50; 
                    }
                    endforeach;             

                } elseif( in_array($woocommerce->customer->get_shipping_country(), $countryArray) ) {

                    foreach ( $woocommerce->cart->cart_contents as $product ) :
                    if ( has_term( $catArray, 'product_cat', $product['product_id'] ) ) {
                        $price += 4.50;
                    } else {
                        $price += 12.50;    
                    }
                    endforeach;

                } else {

                    foreach ( $woocommerce->cart->cart_contents as $product ) :
                    if ( has_term( $catArray, 'product_cat', $product['product_id'] ) ) {
                        $price += 8.50;
                    } else {
                        $price += 18.50;    
                    }
                    endforeach;
                }

    return $price;
}

function display_discounted_price( $values, $item ) {
    return wc_price( $item[ 'line_total' ] ).'<br>incl. shipping';
}

当我删除所有 foreach 语句并仅保留国家/地区条件时,它工作正常,foreach 循环以某种方式导致了问题。

对此提供一些帮助,将不胜感激。

谢谢。

【问题讨论】:

  • 有人帮忙,谢谢

标签: php wordpress woocommerce categories shipping


【解决方案1】:

您不需要通过购物车 foreach 循环来获取产品 ID,如下所示。 我还没有解决您的奇怪显示价格问题,因为它似乎来自产品类别条件。我必须进一步测试,但这段未完成的代码将帮助您了解第一个挂钩函数中的参数是什么:

add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 3 );
function calculate_discounted_price( $price, $cart_item, $cart_object ) {

    $country_arr = array( 'AL', 'AD', 'AT', 'BY', 'BE', 'BA', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FO', 'FI', 'FR', 'DE', 'GI', 'GR', 'HU', 'IS', 'IE', 'IM', 'IT', 'RS', 'LV', 'LI', 'LT', 'LU', 'MK', 'MT', 'MD', 'MC', 'ME', 'NL', 'NO', 'PL', 'PT', 'RO', 'RU', 'SM', 'RS', 'SK', 'SI', 'ES', 'SE', 'CH', 'UA', 'VA', 'RS' );

    $cats_arr = array('handbags','kids','hats');

    // ONLY for logged users (I think)
    $user_ship_country = WC()->customer->get_shipping_country();


    $product_id = $cart_item['product_id'];

    if( $user_ship_country == 'GB' ) {

        if ( has_term( $cats_arr, 'product_cat', $product_id ) )
            $price += 4.50;
        else
            $price += 8.50;

    } elseif( in_array($user_ship_country, $country_arr) ) {

        if ( has_term( $cats_arr, 'product_cat', $product_id ) )
            $price += 4.50;
        else
            $price += 12.50;

    } else {

        if ( has_term( $cats_arr, 'product_cat', $product_id ) )
            $price += 8.50;
        else
            $price += 18.50;

    }

    return $price;
}

add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );
function display_discounted_price( $values, $item ) {
    return wc_price( $item[ 'line_total' ] ).'<br>incl. shipping';
}

另外 WC()-&gt;customer-&gt;get_shipping_country(); 仅适用于登录金客户(我认为)......

希望对你有所帮助。

但是对于运输额外费用,我认为您没有使用正确的挂钩......

【讨论】:

  • 嗨,谢谢你的帮助,我认为它可以工作,但需要关闭查看计算,我使用的正确钩子应该是什么?
  • 我知道这并不容易,但感谢您的帮助...目前这将是一个临时解决方案,直到那时我会为您提供正确的答案
  • @FrancisAlvinTan 如果我得到正确答案,我会发表评论并更新我的答案……我正在同时工作……所以现在对我来说并不容易。谢谢:)
  • 这里的计算仍然有问题
  • 实际上我发现问题 catArray 在最后一个条件下应该是 $cats_arr ......终于工作了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-04
  • 2014-03-15
  • 2018-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多