为了账号安全,请及时绑定邮箱和手机立即绑定

免费送货 基于 WooCommerce 中的最小购物车商品数量

免费送货 基于 WooCommerce 中的最小购物车商品数量

PHP
手掌心 2023-06-24 17:07:29
在 Woocommerce 中,我想根据独特的购物车商品数量提供免费送货服务。首先,我开始查看可用的插件,但找不到任何简单的解决方案。我想要的是:如果访问者将 4 个不同的商品添加到购物车,则运费将是免费的,但如果用户添加相同的产品 4 次,则不会。所以基本上它只适用于 4 个不同的商品(具有 4 个不同的 SKU 编号)。有什么建议吗?
查看完整描述

1 回答

?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

使用WooCommerce - 当免费送货可用时隐藏其他送货方式现有答案代码,您只需使用以下方式计算不同的订单项目:

$items_count = count(WC()->cart->get_cart());

现在,您需要将免费送货方式设置设置为N/A (第一个选项)

然后您将能够轻松地更改代码以允许免费送货,如下所示:

add_filter( 'woocommerce_package_rates', 'free_shipping_on_items_count_threshold', 100, 2 );

function free_shipping_on_items_count_threshold( $rates, $package ) {

    $items_count     = count(WC()->cart->get_cart()); // Different item count

    $items_threshold = 4; // Minimal number of items to get free shipping

    $free            = array(); // Initializing


    // Loop through shipping rates

    foreach ( $rates as $rate_id => $rate ) {

        // Find the free shipping method

        if ( 'free_shipping' === $rate->method_id ) {

            if( $items_count >= $items_threshold ) {

                $free[ $rate_id ] = $rate; // Keep only "free shipping"

            } elseif ( $items_count < $items_threshold ) {

                unset($rates[$rate_id]); // Remove "Free shipping"

            }

            break;// stop the loop

        }

    }

    return ! empty( $free ) ? $free : $rates;

}

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。


如果您也想允许优惠券设置免费送货,则必须将免费送货设置更改为“最低订单金额或优惠券”并设置0最低订单金额,请改为使用以下内容:


add_filter( 'woocommerce_package_rates', 'free_shipping_on_items_count_threshold', 100, 2 );

function free_shipping_on_items_count_threshold( $rates, $package ) {

    $items_count      = count(WC()->cart->get_cart()); // Different item count

    $items_threshold  = 4; // Minimal number of items to get free shipping


    $coupon_free_ship = false; // Initializing

    $free             = array(); // Initializing


    // Loop through applied coupons

    foreach( WC()->cart->get_applied_coupons() as $coupon_code ) {

        $coupon = new WC_Coupon( $coupon_code ); // Get the WC_Coupon Object


        if ( $coupon->get_free_shipping() ) {

            $coupon_free_ship = true;

            break;

        }

    }


    // Loop through shipping rates

    foreach ( $rates as $rate_id => $rate ) {

        // Find the free shipping method

        if ( 'free_shipping' === $rate->method_id ) {

            if( $items_count >= $items_threshold || $coupon_free_ship ) {

                $free[ $rate_id ] = $rate; // Keep only "free shipping"

            } elseif ( $items_count < $items_threshold ) {

                unset($rates[$rate_id]); // Remove "Free shipping"

            }

            break;// stop the loop

        }

    }

    return ! empty( $free ) ? $free : $rates;

}

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。

刷新运输缓存:

  1. 此代码已保存在您的 function.php 文件中。

  2. 在运输区域设置中,禁用/保存任何运输方式,然后启用返回/保存。
    你已经完成了,你可以测试它。


查看完整回答
反对 回复 2023-06-24
  • 1 回答
  • 0 关注
  • 104 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信