为了账号安全,请及时绑定邮箱和手机立即绑定

在 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 关注
  • 90 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信