php 在WooCommerce中添加到购物车之前提前设置运输邮政编码

gg58donl  于 12个月前  发布在  PHP
关注(0)|答案(1)|浏览(76)

我想客户能够设置他们的邮政编码前添加一个产品到购物车。
然后保存此邮政编码并用于定义可用的传递方法。
我已经做了以下函数,但它并不总是工作,我不确定应该使用哪些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));
}

谢谢

but5z9lq

but5z9lq1#

你的代码基本上是正确的,但是为了避免任何问题,还缺少了一些东西:

// Important: Early enable customer WC_Session 
add_action( 'woocommerce_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_SessionWC_CustomerWordPress之间与用户数据相关的差异:

  • WC()->customerWC_Customer对象,它从定义的登录用户访问注册用户数据(因此数据存储在数据库wp_userswp_usermeta表中)或它将读取访客的会话数据
  • WC()->session是存储在WooCommerce session中的任何客户或访客的数据,通过wp_woocommerce_sessions表链接到浏览器cookie和数据库。但请注意,“客户”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 available getter and setter methods,但有些方法不适用于访客。

相关问题