Woocommerce,隐藏基于运输类的运输方式

问题描述 投票:5回答:2

我试图隐藏基于货运类的所有货运方法,当选择属于特定类的产品时,基本上强制采用FedEx隔夜方法。

我从this code开始,并按如下所示进行修改:

add_filter( 'woocommerce_available_shipping_methods', 'hide_shipping_based_on_class' ,    10, 1 );

function check_cart_for_share() {

// load the contents of the cart into an array.
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;

$found = false;

// loop through the array looking for the tag you set. Switch to true if the tag is     found.
foreach ($cart as $array_item) {
$term_list = wp_get_post_terms( $array_item['product_id'], 'product_shipping_class', array( "fields" => "names" ) );

if (in_array("Frozen",$term_list)) {

      $found = true;
      break;
    }
}

return $found;

}

function hide_shipping_based_on_class( $available_methods ) {

// use the function above to check the cart for the tag.
if ( check_cart_for_share() ) {

// remove the rate you want
unset( $available_methods['canada_post,purolator,fedex:FEDEX_GROUND,fedex:GOUND_HOME_DELIVERY'] ); 
}

// return the available methods without the one you unset.
return $available_methods;

}

它似乎并没有隐藏任何运输方法。不确定我错过了什么......

这是一个多站点安装,我正在加拿大方面在http://stjeans.harbourcitydevelopment.com进行测试

我正在运行Table Rate运输模块,以及FedEx,Purolator和Canada Post模块。

php wordpress woocommerce shipping fedex
2个回答
9
投票

我遇到了同样的问题并且帮助了你的代码。一个问题是WooCommerce 2.1不推荐使用“woocommerce_available_shipping_methods”过滤器。所以你必须使用新的:“woocommerce_package_rates”。 WooCommerce tutorial也有类似的任务。

因此,我更改了过滤器挂钩,当条件为真时,我迭代所有送货方法/费率,找到我想要显示给客户的那个,从中创建新数组并返回此数组(只有一个项目)。

我认为你的问题是(除了弃用的钩子)主要是错误的未设置($ available_methods [...])行。它不能像那样工作。

所以这是我的代码:

add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_class' ,    10, 2 );
function hide_shipping_based_on_class( $available_methods ) {
    if ( check_cart_for_share() ) {
        foreach($available_methods as $key=>$method) {
            if( strpos($key,'YOUR_METHOD_KEY') !== FALSE ) {
                $new_rates = array();
                $new_rates[$key] = $method;
                return $new_rates;
            }
        }
    }
    return $available_methods;
}

警告!我发现每次都不会触发woocommerce_package_rates钩子,但只有当您更改购物车中的物品或物品数量时才会触发。或者它看起来像我。也许这些可用的费率以某种方式缓存购物车内容。


0
投票

波纹管代码段允许您根据货运类隐藏运输方法。有详细描述here

add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 10, 2);

function wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package)
{
$hide_when_shipping_class_exist = array(
    42 => array(
        'free_shipping'
    )
);

$hide_when_shipping_class_not_exist = array(
    42 => array(
        'wf_shipping_ups:03',
        'wf_shipping_ups:02',
         'wf_shipping_ups:01'
    ),
    43 => array(
        'free_shipping'
    )
);


$shipping_class_in_cart = array();
foreach(WC()->cart->cart_contents as $key => $values) {
   $shipping_class_in_cart[] = $values['data']->get_shipping_class_id();
}

foreach($hide_when_shipping_class_exist as $class_id => $methods) {
    if(in_array($class_id, $shipping_class_in_cart)){
        foreach($methods as & $current_method) {
            unset($available_shipping_methods[$current_method]);
        }
    }
}
foreach($hide_when_shipping_class_not_exist as $class_id => $methods) {
    if(!in_array($class_id, $shipping_class_in_cart)){
        foreach($methods as & $current_method) {
            unset($available_shipping_methods[$current_method]);
        }
    }
}
return $available_shipping_methods;
}
© www.soinside.com 2019 - 2024. All rights reserved.