我尝试隐藏所有基于运输类的运输方法,但只保留一种,这实际上是在选择属于特定类的产品时强制使用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测试它
我正在运行表率航运模块,以及联邦快递,Purolator和加拿大邮政模块。
2条答案
按热度按时间irtuqstp1#
我有同样的问题,修改你的代码有帮助。一个问题是“woocommerce_available_shipping_methods”过滤器从WooCommerce 2.1中被弃用。所以你必须使用新的:“woocommerce_package_rates"。类似的任务也有WooCommerce tutorial。
因此,我修改了filter hook,当条件为真时,我迭代所有的运输方式/费率,找到我想向客户显示的方式,从中创建新数组并返回该数组(只有一个项目)。
我认为你的问题(除了不赞成的钩子)主要是错误的unset($available_methods[...])行。它不能像那样工作。
下面是我的代码:
警告!我发现woocommerce_package_rates钩子不是每次都被触发,而是只有当您更改购物车中的项目或项目数量时才被触发。或者对我来说是这样。也许这些可用的价格以某种方式缓存了购物车内容。
nhn9ugyo2#
下面的代码片段允许您隐藏基于航运类的航运方式。详细说明可here