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_users
wp_usermeta
WC()->session
WooCommerce 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 方法,但有些方法對來賓不起作用。
- 1 回答
- 0 關注
- 175 瀏覽
添加回答
舉報