[WooCommerce] 取得購物車裡商品的最終折扣價並以運送類別計算免運

本篇文章更新時間:2021/06/07
如有資訊過時或語誤之處,歡迎使用 Contact 功能通知。
一介資男的 LINE 社群開站囉!歡迎入群聊聊~
如果本站內容對你有幫助,歡迎使用 BFX Pay 加密貨幣新台幣 贊助支持。


先前筆記過這篇 [WooCommerce] 程式化處理購物車運費折扣 – 滿額免運為例[WordPress] WooCommerce 客製化運費規則的方法 ,文中舉例「低溫運送」和「常溫運送」用來動態計算購物車裡商品與運費之關係。

但文章裡的寫法有兩個延伸的問題:

  1. 商品金額是取當時產品的售價(price)
  2. 邏輯條件是「任一商品滿足購買價格條件才免運」

關於 1. 的問題是,如果商品還有其他折扣,那這免運條件就應該要符合當下折扣的累積,而不是用原價。

關於 2. 的問題是,如果今天希望是以「整個購物車」來看的話,單一商品要滿足的條件變的很困難,不能是 A+B+C > 結帳金額滿足免運條件。

第一點解法

把原本取得商品售價的方法改寫

$price = $_product->get_price(); -> $price = wc_get_price_including_tax($lineitem);

關鍵是這個 wc_get_price_including_tax() 方法,看命名還可以猜到有另一個 wc_get_price_excluding_tax() 取得不含稅售價的方法。

透過這兩個方法來抓到最終售價(包含套用折扣),這樣計算也比較能貼近使用情境。

第二點解法

如果要拆開原本商品與數量關係的滿足免運條件,那還有一個參數是「這些商品對應的運送類別」,也就是對整個購物車統計個別運送類別的總結帳金額,這樣才能判斷某一個運送類別是否已經滿足該類別條件而免運。

要做到這樣就是直接寫法改寫如下:

function mxp_add_shipping_fee_via_shipping_class() {
    if (is_admin() && !defined('DOING_AJAX')) {
        // 避免在管理介面下被觸發
        return;
    }
    // 設定裡的運送類別 Slug 關鍵字:常溫、低溫
    $shipping_slug        = 'shipping';
    $cooler_shipping_slug = 'cooler-shipping';
    $shipping_class_arr   = array();
    foreach (WC()->cart->get_cart() as $line => $item) {
        $cooler_lineitem_total_price   = 0;
        $shipping_lineitem_total_price = 0;
        // 撈取購物車中的每一個商品資訊
        $_product = wc_get_product($item['data']->get_id());
        // 商品名稱
        $product_name = $_product->get_title();
        // 商品下單時數量
        $qty = $item['quantity'];
        // 商品下單時購物車當前價格
        $price = wc_get_price_including_tax($item['data']);
        // 商品設定的運送類別
        $shipping_class = $_product->get_shipping_class();
        // 判斷低溫運送類別
        if ($cooler_shipping_slug == $shipping_class) {
            $cooler_lineitem_total_price = ($price * $qty);
            if (!isset($shipping_class_arr[$cooler_shipping_slug])) {
                $shipping_class_arr[$cooler_shipping_slug] = 0;
            }
            // 加總低溫類別的累計消費
            $shipping_class_arr[$cooler_shipping_slug] += $cooler_lineitem_total_price;
        }
        // 判斷常溫運送類別
        if ($shipping_slug == $shipping_class) {
            $shipping_lineitem_total_price = ($price * $qty);
            if (!isset($shipping_class_arr[$shipping_slug])) {
                $shipping_class_arr[$shipping_slug] = 0;
            }
            // 加總常溫類別的累計消費
            $shipping_class_arr[$shipping_slug] += $shipping_lineitem_total_price;
        }
    }
    // 分別來判斷低溫與常溫運送類別是否滿足條件
    foreach ($shipping_class_arr as $key => $count_fee) {
        if ($key == $shipping_slug) {
            $shipping_total_fee = $count_fee;
            if ($shipping_total_fee > 800) {
                // 常溫滿 800 元,免運
                $shipping_total_fee = 0;
            }
            if ($shipping_total_fee > 0) {
                WC()->cart->add_fee('常溫宅配運費', $shipping_total_fee, false);
            }
        }
        if ($key == $cooler_shipping_slug) {
            $cooler_total_fee = $count_fee;
            if ($cooler_total_fee > 1800) {
                // 低溫滿 1800 元,免運
                $cooler_total_fee = 0;
            }
            if ($cooler_total_fee > 0) {
                WC()->cart->add_fee('低溫宅配運費', $cooler_total_fee, false);
            }
        }
    }
}
add_action('woocommerce_cart_calculate_fees', 'mxp_add_shipping_fee_via_shipping_class');

如果你的運送類別不止這些,就是依樣畫葫蘆,再多補判斷即可囉~


Share:

作者: Chun

資訊愛好人士。主張「人人都該為了偷懶而進步」。期許自己成為斜槓到變進度條 100% 的年輕人。[///////////____36%_________]

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *


文章
Filter
Mastodon