我有这个密码
/* !Hide Shipping Options Woocommerce */
add_filter( 'woocommerce_available_shipping_methods', 'hide_shipping_based_on_tag' , 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_tag', array( "fields" => "names" ) );
if (in_array("Heavy",$term_list)) { // Replace "Heavy" with what ever tag you want
$found = true;
break;
}
}
return $found;
}
function hide_shipping_based_on_tag( $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['flat_rate'] ); // Replace "flat_rate" with the shipping option that you want to remove.
}
// return the available methods without the one you unset.
return $available_methods;
}
在我的function.php -但由于woocommerce 2.5它不再工作了。其他运输方式需要隐藏时,免费送货可用。
2条答案
按热度按时间iqjalb3h1#
您使用的是WooCommerce版本2.1及以下的钩子。
相反,请使用
woocommerce_package_rates
筛选器。qc6wkl3g2#
此代码工作完美的最新woocommerce:
我已经取消了单位航运,你可以取消其他航运方式,如果你启用。这段代码对我来说很好,我昨天试过了。