WooCommerce 2.6 - 通过达到特定金额触发免费送货时隐藏付费送货

时间:2016-06-15 09:08:46

标签: php wordpress wordpress-plugin woocommerce shipping

我最近在我的商店更新了WooCommerce 2.6,他们更新了他们的运输系统。在达到特定订单价值并触发免费送货之前,我使用此选项隐藏付费送货选项:

/**
 * woocommerce_package_rates is a 2.1+ hook
 */
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );

/**
 * Hide shipping rates when free shipping is available
 *
 * @param array $rates Array of rates found for the package
 * @param array $package The package array/object being shipped
 * @return array of modified rates
 */
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;
}

虽然这不再起作用了。我需要一个新的修复程序,我不是真正的编码。

有没有人有解决方案?

以上解决方案来自本网站:
Hide other shipping methods when FREE SHIPPING is available

我猜测一些参数因为更新了运输方式而发生了变化。

我希望有人知道如何解决这个问题。

3 个答案:

答案 0 :(得分:3)

请尝试使用以下内容替换现有的代码段。 this article中介绍了此代码段的详细信息。如果可以改进,请告诉我。

cd "$(CURRENT_DIRECTORY)"
C:\Python26\python.exe -i "$(FULL_CURRENT_PATH)"

答案 1 :(得分:1)

如果您已删除旧传送方式(必须使用新的送货区域设置送货方式),您可以使用以下代码段在免费送货时删除所有其他送货方式。 (WooCommerce 2.6 +):

  /**
   * Hide shipping rates when free shipping is available.
   * Updated to support WooCommerce 2.6 Shipping Zones.
   *
   * @param array $rates Array of rates found for the package.
   * @return array
   */
  function my_hide_shipping_when_free_is_available( $rates ) {
    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
      if ( 'free_shipping' === $rate->method_id ) {
        $free[ $rate_id ] = $rate;
        break;
      }
    }
    return ! empty( $free ) ? $free : $rates;
  }
  add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 ); 

来自updated docs

答案 2 :(得分:0)

好的以下代码将允许本地提货免费送货:

// ##### WOOCOMMERCE - HIDE OTHER SHIPPING METHODS WHEN FREE SHIPPING IS AVAILABLE #####
add_filter('woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2);
function hide_shipping_when_free_is_available($rates, $package) {
    $free_yn = 0;
    $pickup_yn = 0;
    foreach($rates as $key => $value) {
        $key_part = explode(":", $key);
        $method_title = $key_part[0];
        if ('free_shipping' == $method_title) {
            // check if free shipping rate exists
            $free_yn = 1;
            $free_shipping = $rates[$key];
            $free_key = $key;
        }
        if ('local_pickup' == $method_title) {
            // check if local pickup rate exists
            $pickup_yn = 1;
            $local_pickup = $rates[$key];
            $pickup_key = $key;
        }
    }
    if ($free_yn == 1) {
        // Unset all rates.
        $rates = array();
        // Restore free shipping rate.
        $rates[$free_key] = $free_shipping;
        if ($pickup_yn == 1) {
            // Restore local pickup rate.
            $rates[$pickup_key] = $local_pickup;
        }
        return $rates;
    }
    return $rates;
}