如果
$customer->save()
$customer->set_billing_first_name('Test')
$customer->get_id()
是
0
.
因此,当用户未注册/登录时,WooCommerce不会启动其会话,直到用户登录,或者他/她将产品添加到购物车中。
但是您可以手动启动会话,如下所示:(将代码添加到活动主题的
functions.php
add_action( 'woocommerce_init', function(){
if ( ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
} );
然后,对客户数据的更改将被转到其他页面,只要
曲奇
是在浏览器上启用的,因为就像WordPress一样,WooCommerce将其会话ID(用户ID或自动生成的ID/哈希)存储在Cookie中—会话ID用于设置/检索数据库中的会话数据—表名是
woocommerce_sessions
如果没有任何表前缀。
在启动会话后尝试以下操作:
$customer = WC()->customer;
// Change 'Test' if necessary - e.g. 'Something unique 123'
if ( 'Test' !== $customer->get_billing_first_name() ) {
$customer->set_billing_first_name( 'Test' );
echo 'First name added to session<br>';
} else {
echo 'First name read from session<br>';
}
echo WC()->session->get( 'test', 'None set' ) . '<br>';
WC()->session->set( 'test', current_time( 'mysql' ) );
echo WC()->session->get( 'test', 'It can\'t be empty' );