【问题标题】:Woocommerce 2.5: Hide Shipping Methods when free shipping is available not working anymoreWoocommerce 2.5:当免费送货可用时隐藏送货方式不再有效
【发布时间】:2016-03-08 15:58:23
【问题描述】:

我有这个代码

 /* !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 以来它不再工作了。 当提供免费送货服务时,需要隐藏其他送货方式。 有什么想法吗?

【问题讨论】:

    标签: wordpress woocommerce shipping


    【解决方案1】:

    您正在使用用于 WooCommerce 2.1 及更低版本的挂钩。

    改为使用woocommerce_package_rates 过滤器..

    【讨论】:

      【解决方案2】:

      此代码非常适合最新的 woocommerce:

      add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
      
      function hide_shipping_when_free_is_available( $rates, $package ) {
      
          // Only modify rates if free_shipping is present
          if ( isset( $rates['free_shipping'] ) ) {
      
              // To unset a single rate/method, do the following. This example unsets flat_rate shipping
              unset( $rates['flat_rate'] );
      
              // To unset all methods except for free_shipping, do the following
              $free_shipping          = $rates['free_shipping'];
              $rates                  = array();
              $rates['free_shipping'] = $free_shipping;
          }
      
          return $rates;
      }
      

      我已经取消了固定运费,如果您启用了,您可以取消设置其他运输方式。这段代码对我来说很好用,我昨天试过了。

      【讨论】:

        猜你喜欢
        • 2016-11-11
        • 2021-05-29
        • 2020-06-22
        • 2020-11-05
        • 2015-08-14
        • 2019-05-31
        • 2018-08-09
        • 1970-01-01
        相关资源
        最近更新 更多