WPML - 购物车项目数据自定义字段在切换语言时不更新
WPML - Cart item data custom Fields don't update when switching language
我正在将 Woocommerce 插件与 WPML 多语言插件一起使用,但我无法像预期的那样开始工作。
我的产品有几个自定义字段,我需要在购物车、结帐、订单查看和电子邮件通知中显示这些字段。
字段在前端正确显示,但当我切换语言时,会话中的数据未更新。
WPML 如何处理额外数据?
有没有办法让它工作,当我切换语言时数据更新?
这是我的代码:
add_filter( 'woocommerce_product_data_tabs', 'mbextra_product_data_tab' , 99 , 1 );
function mbextra_product_data_tab( $product_data_tabs ) {
$product_data_tabs['mbextraproducttab'] = array(
'label' => __( 'EXTRA', 'mbg' ),
'target' => 'mbextraproductdata',
'class' => array();
);
return $product_data_tabs;
}
add_action( 'woocommerce_product_data_panels', 'mbextra_product_data_fields' );
function mbextra_product_data_fields() {
?>
<div id="mbextraproductdata" class="panel woocommerce_options_panel">
<div class="options_group">
<?php
woocommerce_wp_textarea_input(
array(
'id' => 'company',
'label' => __( 'Company', 'mbg' ),
'placeholder' => 'Company Adress here',
'desc_tip' => 'true',
'description' => __( 'Enter Company Adress here', 'mbg' )
)
);
?>
</div>
<div class="options_group">
<?php
woocommerce_wp_textarea_input(
array(
'id' => 'shortdescription',
'label' => __( 'Short-Description', 'mbg' ),
'placeholder' => 'Enter some short info',
'desc_tip' => 'true',
'description' => __( 'Enter Short description here', 'mbg' )
)
);
?>
</div>
</div><!-- #extraproductdata Tab -->
<?php
}
add_action( 'woocommerce_process_product_meta', 'mbprocess_product_meta_fields_save', 99 );
function mbprocess_product_meta_fields_save( $post_id ){
// if set > save the fields
$company = $_POST['company'];
if( isset( $company ) )
update_post_meta( $post_id, 'company', esc_attr( $company ) );
// if set > save data to post_meta
$shortdescription = $_POST['shortdescription'];
if( isset( $shortdescription ) )
update_post_meta( $post_id, 'shortdescription', esc_attr( $shortdescription ) );
}
add_filter( 'woocommerce_add_cart_item_data', 'custom_product_field', 10, 3 );
function custom_product_field( $cart_item_data, $product_id, $variation_id ) {
$shortdesciption = get_post_meta( $product_id , 'shortdesciption' , true );
$company = get_post_meta( $product_id , 'company' , true );
if( !empty( $shortdesciption ) )
{
$cart_item_data['shortdesciption'] = $shortdesciption;
}
if( !empty( $company ) )
{
$cart_item_data['company'] = $company;
}
return $cart_item_data;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'mbget_cart_item_from_session', 10, 3);
function mbget_cart_item_from_session( $cart_item_data, $cart_item_session_data, $cart_item_key ) {
if ( isset( $cart_item_session_data['shortdesciption'] ) ) {
$cart_item_data['shortdesciption'] = $cart_item_session_data['shortdesciption'];
}
if ( isset( $cart_item_session_data['company'] ) ) {
$cart_item_data['company'] = $cart_item_session_data['company'];
}
return $cart_item_data;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
function render_meta_on_cart_and_checkout( $cart_data, $cart_item ) {
$data = array();
if( !empty( $data ) ) {
$data = $cart_data;
}
if( isset( $cart_item['shortdesciption'] ) ) {
$data[] = array(
'name' => __( 'Stuff', 'mbg' ),
'value' => $cart_item['shortdesciption'] );
}
if( isset( $cart_item['company'] ) ) {
$data[] = array(
'name' => __( 'Company', 'mbg' ),
'value' => $cart_item['company'] );
}
return $data;
}
谢谢
YES you should need to destroy the cart session or to remove all cart items when switching language. But this kind of case never really happen:
因为这不是真正的客户行为,例如在购物车中添加商品(针对某种语言)然后在结账前切换到另一种语言。
所以这应该不是真正的问题。这只是一个开发者在以各种可能的方式测试电子商务的行为,不是吗?
我正在将 Woocommerce 插件与 WPML 多语言插件一起使用,但我无法像预期的那样开始工作。
我的产品有几个自定义字段,我需要在购物车、结帐、订单查看和电子邮件通知中显示这些字段。
字段在前端正确显示,但当我切换语言时,会话中的数据未更新。
WPML 如何处理额外数据?
有没有办法让它工作,当我切换语言时数据更新?
这是我的代码:
add_filter( 'woocommerce_product_data_tabs', 'mbextra_product_data_tab' , 99 , 1 );
function mbextra_product_data_tab( $product_data_tabs ) {
$product_data_tabs['mbextraproducttab'] = array(
'label' => __( 'EXTRA', 'mbg' ),
'target' => 'mbextraproductdata',
'class' => array();
);
return $product_data_tabs;
}
add_action( 'woocommerce_product_data_panels', 'mbextra_product_data_fields' );
function mbextra_product_data_fields() {
?>
<div id="mbextraproductdata" class="panel woocommerce_options_panel">
<div class="options_group">
<?php
woocommerce_wp_textarea_input(
array(
'id' => 'company',
'label' => __( 'Company', 'mbg' ),
'placeholder' => 'Company Adress here',
'desc_tip' => 'true',
'description' => __( 'Enter Company Adress here', 'mbg' )
)
);
?>
</div>
<div class="options_group">
<?php
woocommerce_wp_textarea_input(
array(
'id' => 'shortdescription',
'label' => __( 'Short-Description', 'mbg' ),
'placeholder' => 'Enter some short info',
'desc_tip' => 'true',
'description' => __( 'Enter Short description here', 'mbg' )
)
);
?>
</div>
</div><!-- #extraproductdata Tab -->
<?php
}
add_action( 'woocommerce_process_product_meta', 'mbprocess_product_meta_fields_save', 99 );
function mbprocess_product_meta_fields_save( $post_id ){
// if set > save the fields
$company = $_POST['company'];
if( isset( $company ) )
update_post_meta( $post_id, 'company', esc_attr( $company ) );
// if set > save data to post_meta
$shortdescription = $_POST['shortdescription'];
if( isset( $shortdescription ) )
update_post_meta( $post_id, 'shortdescription', esc_attr( $shortdescription ) );
}
add_filter( 'woocommerce_add_cart_item_data', 'custom_product_field', 10, 3 );
function custom_product_field( $cart_item_data, $product_id, $variation_id ) {
$shortdesciption = get_post_meta( $product_id , 'shortdesciption' , true );
$company = get_post_meta( $product_id , 'company' , true );
if( !empty( $shortdesciption ) )
{
$cart_item_data['shortdesciption'] = $shortdesciption;
}
if( !empty( $company ) )
{
$cart_item_data['company'] = $company;
}
return $cart_item_data;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'mbget_cart_item_from_session', 10, 3);
function mbget_cart_item_from_session( $cart_item_data, $cart_item_session_data, $cart_item_key ) {
if ( isset( $cart_item_session_data['shortdesciption'] ) ) {
$cart_item_data['shortdesciption'] = $cart_item_session_data['shortdesciption'];
}
if ( isset( $cart_item_session_data['company'] ) ) {
$cart_item_data['company'] = $cart_item_session_data['company'];
}
return $cart_item_data;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
function render_meta_on_cart_and_checkout( $cart_data, $cart_item ) {
$data = array();
if( !empty( $data ) ) {
$data = $cart_data;
}
if( isset( $cart_item['shortdesciption'] ) ) {
$data[] = array(
'name' => __( 'Stuff', 'mbg' ),
'value' => $cart_item['shortdesciption'] );
}
if( isset( $cart_item['company'] ) ) {
$data[] = array(
'name' => __( 'Company', 'mbg' ),
'value' => $cart_item['company'] );
}
return $data;
}
谢谢
YES you should need to destroy the cart session or to remove all cart items when switching language. But this kind of case never really happen:
因为这不是真正的客户行为,例如在购物车中添加商品(针对某种语言)然后在结账前切换到另一种语言。
所以这应该不是真正的问题。这只是一个开发者在以各种可能的方式测试电子商务的行为,不是吗?