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

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

在 WooCommerce 中添加到購物車之前盡早設置運輸郵政編碼

在 WooCommerce 中添加到購物車之前盡早設置運輸郵政編碼

PHP
三國紛爭 2023-04-28 16:05:04
我希望客戶能夠在將產品添加到購物車之前設置他們的郵政編碼。然后保存此郵政編碼并用于定義可用的交付方式。我已經實現了以下功能,但它并不總是有效,我不確定應該使用哪些 Woocommerce 方法以及它們之間有什么區別:WC()->customer->set_shipping_postcode(...)和WC()->customer->get_shipping_postcode()WC()->session->set('shipping_postcode', ...)和WC()->session->get('shipping_postcode')update_user_meta(get_current_user_id(), 'shipping_postcode', ...)和get_user_meta(get_current_user_id(), 'shipping_postcode', true)此外,我正在保存賬單和送貨的郵政編碼,因為我不知道用戶之前是否下過訂單并選擇將其交付到與賬單地址不同的送貨地址。function getDeliveryZipcode(){  $shipping_postcode = WC()->customer->get_shipping_postcode();  $billing_postcode = WC()->customer->get_billing_postcode();  return $shipping_postcode ? $shipping_postcode : $billing_postcode;}function setDeliveryZipcode(){  $zipcode = $_GET['zipcode'];  // ...  WC()->customer->set_shipping_postcode(wc_clean($zipcode));  WC()->customer->set_billing_postcode(wc_clean($zipcode));}
查看完整描述

1 回答

?
九州編程

TA貢獻1785條經驗 獲得超4個贊

您的代碼大部分是正確的,但缺少一些東西,以避免出現任何問題:


// Important: Early enable customer WC_Session?

add_action( 'init', 'wc_session_enabler' );

function wc_session_enabler() {

? ? if ( ! is_admin() && ! WC()->session->has_session() ) {

? ? ? ? WC()->session->set_customer_session_cookie( true );

? ? }

}


function getDeliveryZipcode()

{

? ? $shipping_postcode = WC()->customer->get_shipping_postcode();

? ? $billing_postcode = WC()->customer->get_billing_postcode();


? ? return ! empty($shipping_postcode) ? $shipping_postcode : $billing_postcode;

}


function setDeliveryZipcode()

{

? ? if ( isset($_GET['zipcode']) ) {

? ? ? ? WC()->customer->set_shipping_postcode(wc_clean($_GET['zipcode']));

? ? ? ? WC()->customer->set_billing_postcode(wc_clean($_GET['zipcode']));

? ? }

}

代碼進入您的活動子主題(或活動主題)的 functions.php 文件。測試和工作。


WC_Session以下是和WC_Customer與用戶數據相關的區別WordPress

  • WC()->customer從定義的登錄用戶WC_Customer訪問注冊用戶數據的對象(因此存儲在數據庫和表中的數據)或者它將讀取訪客的會話數據wp_userswp_usermeta

  • WC()->sessionWooCommerce session是為任何客戶或客人存儲的數據,鏈接到瀏覽器 cookie 并通過wp_woocommerce_sessions表鏈接到數據庫。但請注意,“客戶”WC 會話在第一次添加到購物車時啟用。

  • WordPress 功能get_user_meta(),set_user_meta()update_user_meta()允許從wp_usermeta表中為注冊用戶讀取/寫入/更新用戶元數據。

注意:?WooCommerce 中不存在以下內容:

$postcode?=?WC()->session->get('shipping_postcode');?
WC()->session->set('shipping_postcode',?$postcode);

可以使用以下方式讀取客戶會話數據:

// Get an array of the current customer data stored in WC session

$customer_data = (array) WC()->session->get('customer');?


// Get the billing postcode

if ( isset( $customer_data['postcode'] ) )

? ? $postcode = $customer_data['postcode'];?


// Get the shipping postcode

if ( isset( $customer_data['shipping_postcode'] ) )

? ? $postcode = $customer_data['shipping_postcode'];

可以使用以下方式設置客戶會話數據:


// Get an array of the current customer data stored in WC session

$customer_data = (array) WC()->session->get('customer');?


// Change the billing postcode

$customer_data['postcode'] = '10670';


// Change the shipping postcode

$customer_data['shipping_postcode'] = '10670';


// Save the array of customer WC session data

WC()->session->set('customer', $customer_data);

對于WC()->customer,您可以使用任何WC_Customer可用的 getter 和 setter 方法,但有些方法對來賓不起作用。



查看完整回答
反對 回復 2023-04-28
  • 1 回答
  • 0 關注
  • 175 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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