【问题标题】:WPML - Cart item data custom Fields don't update when switching languageWPML - 切换语言时购物车商品数据自定义字段不更新
【发布时间】:2016-12-10 22:38:51
【问题描述】:

我正在使用带有 WPML 多语言插件的 Woocommerce 插件,但我根本无法按预期工作。

我的产品有多个自定义字段,我需要在购物车、结帐、订单视图和电子邮件通知中显示这些字段。

字段在前端正确显示,但是当我切换语言时,会话中的数据没有更新。

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

谢谢

【问题讨论】:

  • 你好 LoicTheAztec,我知道 WPML 是主要插件,而 Woocmmerce Multilingual 没有 WPML 就无法工作。而且我也有它的终身许可证 :) 对我来说这完全有意义:访问一个页面,选择你喜欢的语言,然后继续只用这种语言做事。所以你最后的评论是我想听到或期望的答案。我只是希望其他更有经验的用户对此有信心,以确认我的想法。现在我很高兴,谢谢!
  • 所以我把它作为答案发布了...... :) 谢谢

标签: php wordpress woocommerce custom-fields wpml


【解决方案1】:

您应该需要在切换语言时销毁购物车会话或删除所有购物车项目。但这种情况从未真正发生过:

因为这不是真正的客户行为,例如,在购物车中添加商品(对于某些语言),然后在结账前切换到另一种语言。

所以这不应该是一个真正的问题。这只是开发人员以所有可能的方式测试电子商务的行为,不是吗?

【讨论】:

  • 是的。谢谢你让我不再为此头疼:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-07
  • 1970-01-01
  • 1970-01-01
  • 2019-10-08
  • 2019-04-23
  • 2019-12-21
  • 1970-01-01
相关资源
最近更新 更多