所以我试图隐藏基于产品标签中Woocommerce某些船舶的方法。我面对的主要问题是我自己缺乏PHP知识,使我一起frankensteined下面的代码一些非常友好的人的帮助:隐藏运输选项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) {
if (isset($array_item['product_tag']) && $array_item['product_tag'] == "CHOSEN_TAG") { // Replace "CHOSEN_TAG" with what ever tag you want
$found = true;
break;
}
}
return $found;
}
function hide_shipping_based_on_tag($available_methods) {
// use the function abve to check the cart for the tag.
if (check_cart_for_share()) {
// remove the rate you want
unset($available_methods['flat_rate']); // Replace "flar_rate" with the shipping option that yu want to remove.
}
// return the available methods without the one you unset.
return $available_methods;
}
据我所知,这个代码是绝不普遍性,因而变量将是不同的情况下,但也许有人可以告诉我,如果东西在代码中看起来。非常感谢
是代码不工作?或者你是否在寻找更多如何使这个更好的答案?如果是这样,那么这个问题在http://codereview.stackexchange.com/更好。 –