[WooCommerce] 結帳時取得顧客於購物車與結帳頁面當下狀態資訊的方法

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


這需求真的幾乎可以說是每次進行客製化過程一定會遇到的開發技巧!!

比方說最基本的從顧客下單填寫的郵遞區號來判斷是台灣離島還是本島,用來計算運費。

又或是有條件的(比方說客戶消費等級)來開放某些金物流選項等。

過去有寫過不少這樣案例的筆記:

甚至是一些行銷外掛 [WooCommerce] 還差多少就免運的促銷外掛:Amount Left for Free Shipping 也都會要知道當前顧客選擇的資訊來做更近一步的服務。

下面就是列出取得顧客於購物車到結帳頁面能取得的資訊方法:

  • 取得顧客於結帳頁面填寫的欄位資料(預設)
WC()->cart->get_customer()->get_billing_first_name();
WC()->cart->get_customer()->get_billing_last_name();
WC()->cart->get_customer()->get_billing_company();
WC()->cart->get_customer()->get_billing_email();
WC()->cart->get_customer()->get_billing_phone();
WC()->cart->get_customer()->get_billing_country();
WC()->cart->get_customer()->get_billing_state();
WC()->cart->get_customer()->get_billing_postcode();
WC()->cart->get_customer()->get_billing_city();
WC()->cart->get_customer()->get_billing_address();
WC()->cart->get_customer()->get_billing_address_2();
WC()->cart->get_customer()->get_shipping_first_name();
WC()->cart->get_customer()->get_shipping_last_name();
WC()->cart->get_customer()->get_shipping_company();
WC()->cart->get_customer()->get_shipping_country();
WC()->cart->get_customer()->get_shipping_state();
WC()->cart->get_customer()->get_shipping_postcode();
WC()->cart->get_customer()->get_shipping_city();
WC()->cart->get_customer()->get_shipping_address();
WC()->cart->get_customer()->get_shipping_address_2();
  • 取得購物車中當前加入的 Line item 項目
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
   $product = $cart_item['data'];
   $product_id = $cart_item['product_id'];
   $variation_id = $cart_item['variation_id'];
   $quantity = $cart_item['quantity'];
   $price = WC()->cart->get_product_price( $product );
   $subtotal = WC()->cart->get_product_subtotal( $product, $cart_item['quantity'] );
   $link = $product->get_permalink( $cart_item );
   // 取得到商品物件後,可以參考商品類別的設計方法,拿更多關於商品的細節資訊。Ref: https://woocommerce.github.io/code-reference/classes/WC-Product.html
}
  • 購物車中當前計算的各項金額數字
WC()->cart->get_cart_contents_count();
WC()->cart->get_cart_subtotal();
WC()->cart->subtotal_ex_tax;
WC()->cart->subtotal;
WC()->cart->get_displayed_subtotal();
WC()->cart->get_taxes_total();
WC()->cart->get_shipping_total();
WC()->cart->get_coupons();
WC()->cart->get_coupon_discount_amount( 'coupon_code' );
WC()->cart->get_fees();
WC()->cart->get_discount_total();
WC()->cart->get_total();
WC()->cart->total;
WC()->cart->get_tax_totals();
WC()->cart->get_cart_contents_tax();
WC()->cart->get_fee_tax();
WC()->cart->get_discount_tax();
WC()->cart->get_shipping_total();
WC()->cart->get_shipping_taxes();
  • 可以運用的條件判斷方法
WC()->cart->is_empty()
WC()->cart->needs_payment()
WC()->cart->show_shipping()
WC()->cart->needs_shipping()
WC()->cart->needs_shipping_address()
WC()->cart->display_prices_including_tax()
  • 取得客戶運送區域的方法
// Get cart shipping packages
$shipping_packages = WC()->cart->get_shipping_packages();
// Get the WC_Shipping_Zones instance object for the first package
$shipping_zone = wc_get_shipping_zone(reset($shipping_packages));
$zone_id   = $shipping_zone->get_id(); // Get the zone ID
$zone_name = $shipping_zone->get_zone_name(); // Get the zone name

不過經過測試,後台設定本島與離島的郵遞區號,不知道為啥「連江縣」的運送區域他還是會判斷成「本島」。這塊我後來是直接整理一個郵遞區號資料陣列來做判斷。

  • 常用勾點 Hook

woocommerce_cart_calculate_fees - 針對其他客製化費用很常用這個。哪怕是免運條件符合,這邊都可以帶入負數金額去抵扣。

還有片段(Fragments)的程式設計互動,透過這個機制來更新結帳頁面顯示的資訊。

再來是透過 cart_item 資料來傳遞參數去動態改變購物車資料的做法,參考: [WooCommerce] 動態商品價格加入購物車結帳的方法(以贊助、捐款功能為例)

常用的勾點這邊根據我個人經驗排下來,大多是結帳頁面時互動比較多,剩下來的一些特殊需求就是看情況來找對應的勾點處理了!


Share:

作者: Chun

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

發佈留言

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


文章
Filter
Apply Filters
Mastodon