[WooCommerce] 程式開發顧客結帳互動時會使用到的方法

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


顧客、消費者在 WooCommerce 結帳的時候其實有很多客製化的機會。像是之前筆記過的可以參考 WooCommerce 標籤。

互動這件事分主動與被動,主動是指顧客點擊畫面上的物件,預期會有怎樣的反應發生,而被動就是操作的過程中,系統因為條件滿足而觸發一些反應。

通常被動的部分就能讓顧客感受加分,不然遇到不理解操作設計時,很難去說是設計的爛還是客戶的笨了是吧(?)

結帳時能夠讓程式端取得一些顧客的資料要仰靠 WooCommerce Fragments 片段互動,參考這篇筆記: [WooCommerce] 片段(fragments)互動程式設計要點

可以簡單地說這樣的互動,會讓後端程式可以拿到多一點使用者結帳前的資訊,進而來判斷,像是:是不是老客戶?買的多寡要不要給折扣?運費邏輯的計算符合銷售成本?... 等

拿到顧客的資訊方法如下:(WooCommerce v2.1 以後適用)

//結帳付款人欄位
$billing = WC()->customer->get_billing(); 
$billing_first_name = WC()->customer->get_billing_first_name(); 
$billing_last_name = WC()->customer->get_billing_last_name(); 
$billing_company = WC()->customer->get_billing_company(); 
$billing_address = WC()->customer->get_billing_address(); 
$billing_address_1 = WC()->customer->get_billing_address_1(); 
$billing_address_2 = WC()->customer->get_billing_address_2(); 
$billing_city = WC()->customer->get_billing_city(); 
$billing_state = WC()->customer->get_billing_state(); 
$billing_postcode = WC()->customer->get_billing_postcode(); 
$billing_country = WC()->customer->get_billing_country(); 

//運送收貨端欄位
$shipping = WC()->customer->get_shipping(); 
$shipping_first_name = WC()->customer->get_shipping_first_name(); 
$shipping_last_name = WC()->customer->get_shipping_last_name(); 
$shipping_company = WC()->customer->get_shipping_company(); 
$shipping_address = WC()->customer->get_shipping_address(); 
$shipping_address_1 = WC()->customer->get_shipping_address_1(); 
$shipping_address_2 = WC()->customer->get_shipping_address_2(); 
$shipping_city = WC()->customer->get_shipping_city(); 
$shipping_state = WC()->customer->get_shipping_state(); 
$shipping_postcode = WC()->customer->get_shipping_postcode(); 
$shipping_country = WC()->customer->get_shipping_country(); 

Ref: WooCommerce set billing and shipping information

除了這些內建方法外,有自己資料想從前端透過片段功能同步後端的話還可以透過 Session 功能的 set & get 來存取,例如: WC()->session->set('變數名稱','變數值')
WC()->session->get('變數名稱')

後記

這標題我也不知道日後找筆記時找不找得到,但有筆記一下應該會有感覺吧XD


Share:

作者: Chun

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

發佈留言

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


文章
Filter
Apply Filters
Mastodon