亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

免費送貨 基于 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 關注
  • 166 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號