我想客户能够设置他们的邮政编码前添加一个产品到购物车。
然后保存此邮政编码并用于定义可用的传递方法。
我已经做了以下函数,但它并不总是工作,我不确定应该使用哪些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条答案
按热度按时间but5z9lq1#
你的代码基本上是正确的,但是为了避免任何问题,还缺少了一些东西:
代码放在活动子主题(或活动主题)的functions.php文件中。测试和作品。
以下是
WC_Session
、WC_Customer
和WordPress
之间与用户数据相关的差异:WC()->customer
是WC_Customer
对象,它从定义的登录用户访问注册用户数据(因此数据存储在数据库wp_users
和wp_usermeta
表中)或它将读取访客的会话数据。WC()->session
是存储在WooCommerce session
中的任何客户或访客的数据,通过wp_woocommerce_sessions
表链接到浏览器cookie和数据库。但请注意,“客户”WC会话在第一次添加到购物车时启用。get_user_meta()
、set_user_meta()
和update_user_meta()
允许从注册用户的wp_usermeta
表中读取/写入/更新用户元数据。**注意:**以下内容在WooCommerce中不存在:
客户会话数据可以使用以下命令读取:
客户会话数据可以例如使用以下设置:
对于
WC()->customer
,您可以使用任何WC_Customer
available getter and setter methods,但有些方法不适用于访客。