2013-07-01 63 views
2

所以我试图隐藏基于产品标签中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; 

} 

据我所知,这个代码是绝不普遍性,因而变量将是不同的情况下,但也许有人可以告诉我,如果东西在代码中看起来。非常感谢

+0

是代码不工作?或者你是否在寻找更多如何使这个更好的答案?如果是这样,那么这个问题在http://codereview.stackexchange.com/更好。 –

回答

5

没有你现在排序这个疑问,但你的代码是一个良好的开端,我......因为我整理它我在下面刊登出来。你的问题是woocommerce在购物车数组中没有product_tag,所以你必须去弄明白。

/* !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; 

} 
+0

其实我试图做的是去除航运类“flat_rate”当其他航运类存在。该代码使我能够在产品上存在标签时执行此操作。然而,当我今天早上醒来时,我意识到,如果我更改'product_tag'为'product_shipping_class'(更多选项,请参阅http://docs.woothemes.com/wc-apidocs/class-WC_Product.html),我可以做我的不需要添加标签以及发货类......它可以工作......但它也适用于任何产品属性。以为我会分享。伊恩 –

-2

在主题的functions.php中使用它,或者使用此代码创建一个插件。

+0

照亮我们与你downvote的理由。它可能是教育性的。 –

+0

不是谁低估了你,但你有什么没有答案。这更多的是评论。在你要传达给问题提问者的内容上添加链接或小代码示例。以上这些对于那些在这方面没有太多经验的人来说无论如何也没有帮助。 – adamj

+0

是的,当然。这应该是一个评论。该评论被错误地作为答案,旨在帮助那些可能不知道如何使用代码示例或在何处实施它的人。所以我确实认为,我的错位评论确实对像我这样的人并不熟悉框架。 –

0

您可以使用航运类Woocommerce来实现这一点。

这里是分步说明。

  • 创建航运类(woocommerce->设置 - >运费 - >航运类)。
  • 副本航运类产品(你的魔杖隐藏的运输方式)
  • 使用this片段。 (复制并切换到function.php)。

欲了解更多关于该片段的信息可用here