在 WooCommerce 中添加到购物车之前尽早设置运输邮政编码 [英] Set shipping postcode early before add to cart in WooCommerce

查看:21
本文介绍了在 WooCommerce 中添加到购物车之前尽早设置运输邮政编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望客户能够在将产品添加到购物车之前设置他们的邮政编码.
然后保存此邮政编码并用于定义可用的交付方式.

I would like the customers to be able to set their post code before adding a product to cart.
This post code is then saved and used to define available delivery methods.

我已经实现了以下功能,但它并不总是有效,我不确定应该使用哪些 Woocommerce 方法以及它们之间的区别:

I've made the following functions but it's not always working and I'm not sure about which Woocommerce methods should be used and what's the difference between them:

  • 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)
  • WC()->customer->set_shipping_postcode(...) and WC()->customer->get_shipping_postcode()
  • WC()->session->set('shipping_postcode', ...) and WC()->session->get('shipping_postcode')
  • update_user_meta(get_current_user_id(), 'shipping_postcode', ...) and get_user_meta(get_current_user_id(), 'shipping_postcode', true)

此外,我正在保存帐单和送货的邮政编码,因为我不知道用户之前是否已下订单并选择将其运送到与帐单地址不同的送货地址.

Also, I'm saving post code for billing and shipping cause I don't know if user has previously made an order and chosen to delivered it to a shipping address different than billing address.

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));
}

谢谢

推荐答案

您的代码大部分是正确的,但缺少一些东西,以避免出现任何问题:

Your code is mostly correct, but there is something missing, to avoid any issues:

// 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 文件中.经测试有效.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

以下是关于用户数据的 WC_SessionWC_CustomerWordPress 之间的差异解释:

Here are the differences explained between WC_Session, WC_Customer and WordPress related to user data:

  • WC()->customerWC_Customer 对象,它访问来自定义的登录用户的注册用户数据(所以存储在数据库 wp_userswp_usermeta 表中的数据)或它将读取来宾的会话数据.
  • WC()->session 是存储在 WooCommerce session 中的任何客户或访客的数据,链接到浏览器 cookie 和通过 wp_woocommerce_sessions 表到数据库.但请注意,客户"WC 会话在第一次添加到购物车时启用.
  • WordPress 函数get_user_meta()set_user_meta()update_user_meta() 允许从wp_usermeta 注册用户的表.
  • WC()->customer is the WC_Customer Object that access the registered user-data from a defined logged in user (so the data that is stored on database wp_users and wp_usermeta tables) or it will read the session data for guests.
  • WC()->session is the data stored in WooCommerce session for any customers or guests, linked to a browser cookie and to database through wp_woocommerce_sessions table. But note that "customer" WC session is enabled on the first add to cart.
  • The WordPress functions get_user_meta(), set_user_meta() and update_user_meta() allow to read / write / update user metadata from wp_usermeta table for a registered user.

注意:WooCommerce 中不存在以下内容:

Note: The following doesn't exist in WooCommerce:

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

可以使用以下方式读取客户会话数据:

The customer session data can be read using:

// 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'];

客户会话数据可以使用例如:

The customer session data can be set for example using:

// 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 方法,但有些方法不适用于来宾.

For WC()->customer, you can use any of the WC_Customer available getter and setter methods, but some few methods will not work for guests.

这篇关于在 WooCommerce 中添加到购物车之前尽早设置运输邮政编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆