【问题标题】:Hide Shipping Options Woocommerce隐藏运输选项 Woocommerce
【发布时间】:2013-07-01 16:17:58
【问题描述】:

所以我试图根据产品标签在 Woocommerce 中隐藏某些发货方式。我面临的主要问题是我自己缺乏 PHP 知识,所以在一些非常友好的人的帮助下,我将以下代码科学化了:

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;

}

我知道这段代码绝不是通用的,因此变量会因情况而异,但也许有人可以告诉我代码中是否有问题。非常感谢

【问题讨论】:

  • 代码不起作用?或者您是否正在寻找更多如何做出更好的答案?如果是这样,那么这个问题在codereview.stackexchange.com 中会更好。

标签: wordpress woocommerce


【解决方案1】:

毫无疑问,您现在已经对它进行了排序,但您的代码对我来说是一个好的开始......自从我对它进行排序后,我将其发布在下面。您的问题是 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;

}

【讨论】:

  • 实际上,当存在另一个运输类别时,我试图做的是删除运输类别“flat_rate”。此代码使我能够在产品上存在标签时执行此操作。然而,当我今天早上醒来时,我意识到如果我将 'product_tag' 更改为 'product_shipping_class'(更多选项请参阅 docs.woothemes.com/wc-apidocs/class-WC_Product.html),我可以做我想做的事,而无需添加标签和运输类别......它有效......但它也适用于任何产品属性。以为我会分享。伊恩
  • woocommerce_available_shipping_methods 已弃用,无法使用 woocommerce_package_rates。
【解决方案2】:

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

这里是分步说明。

  • 创建一个shipping class(woocommerce->settings->shipping->shipping classes)。
  • 将此配送类别与产品(您希望为其隐藏配送方式)相关联
  • 使用thissn-p。 (复制并粘贴到 function.php)。

有关 sn-p 的更多信息,请联系here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 2016-08-15
    • 1970-01-01
    • 2021-01-08
    • 2014-08-06
    • 2014-07-05
    • 1970-01-01
    相关资源
    最近更新 更多