如果超过 5 磅,WooCommerce 免费送货 [英] WooCommerce Free Shipping if over 5 pounds

查看:53
本文介绍了如果超过 5 磅,WooCommerce 免费送货的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数,如果订单超过 5 磅(80 盎司),该函数将仅提供免费送货(删除所有其他选项),但尽管代码似乎正确,但它不起作用.

I'm trying to write a function that will offer free shipping only (remove all other options) if the order is over 5 pounds (80 ounces), but it doesn't work although the code seems correct.

这就是我所拥有的:

  // Hide ALL shipping options but FREe when over 80 ounches (5 pounds)
add_filter( 'woocommerce_available_shipping_methods', 'ship_free_if_over_five_pounds' , 10, 1 );

/**
* Hide ALL Shipping option but free if over 5 pounds
*
* @param array $available_methods
*/
function ship_free_if_over_five_pounds( $available_methods ) {
    global $woocommerce;
    $whats_the_weight = $woocommerce->cart->cart_contents_weight;   

    if($whats_the_weight != 80) :

        // Get Free Shipping array into a new array
        $freeshipping = array();
        $freeshipping = $available_methods['free_shipping'];

        // Empty the $available_methods array
        unset( $available_methods );

        // Add Free Shipping back into $avaialble_methods
        $available_methods = array();
        $available_methods[] = $freeshipping;

    endif;

    return $available_methods;
}

有什么想法吗?

代码基于此站点上的示例 #19:
我的 25 个 WordPress 最佳 WooCommerce 片段第 2 部分

推荐答案

我知道这是一个老问题,但我最近有这个替代方案......

I know that this is an old question, but I have this recent alternative…

首先,在您的代码中如果订单超过 5 磅(80 盎司)",您的 if 语句应该是 if($whats_the_weight > 80) 而不是 !=...使用 WooCommerce 2.6+.

First, in your code "if the order is over 5 pounds (80 ounces)" your if statement should be if($whats_the_weight > 80) instead of !=… But i think that your code is a little outdated if you use WooCommerce 2.6+.

在使用 global $woocommerce;$woocommerce->cart->cart_contents_weight; 后,您可以使用:WC()->cart->cart_contents_weight;

After instead using global $woocommerce; with $woocommerce->cart->cart_contents_weight; you could use: WC()->cart->cart_contents_weight;

我有这个更新的代码片段 基于 WooCommerce 2.6+ 的官方主题.你应该试试:

I have this much more recent code snippet based on this official thread for WooCommerce 2.6+. You should try it:

add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );

function my_hide_shipping_when_free_is_available( $rates ) {

    $cart_weight = WC()->cart->cart_contents_weight; // Cart total weight

    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id && $cart_weight > 80 ) { // <= your weight condition
            $free[ $rate_id ] = $rate;
            break;
        }
    }
    return ! empty( $free ) ? $free : $rates;
}

对于 WooCommerce 2.5,你应该试试这个:

For WooCommerce 2.5, You should try this:

add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );

function hide_shipping_when_free_is_available( $rates, $package ) {

    $cart_weight = WC()->cart->cart_contents_weight; // Cart total weight

    // Only modify rates if free_shipping is present
    if ( isset( $rates['free_shipping'] ) && $cart_weight > 80 ) { // Here your weight condition

        // 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;
}

这篇关于如果超过 5 磅,WooCommerce 免费送货的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆