3

I am using Woocommerce plugin with WPML Multilingual plugin, and I simply can't get to work like I would expect.

My products have serveral Custom Fields that I need to display in the cart, Checkout, Order views and emails notifications.

The Fields are displayed correctly on frontend but when I switch the language the data from sessions are not updated.

How does WPML handle the extra data?
Is there a way to get this to work, that the data update when i switch the language?

Here is my code:

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

Thanks

mbg
  • 448
  • 4
  • 7
  • Hello LoicTheAztec, i know that WPML the main plugin is and Woocmmerce Multilingual does not work without WPML. And i also have a lifetime-license for it :) For me it makes total sense: visit a page, choose your prefered language, and then continue doin stuff only in this language. So your last comment was the answer i wanted to hear or expected. I just wanted confidence on it from other more experienced users to have a confirmation on my thoughts. Now i'm happy, thank you! – mbg Dec 13 '16 at 10:58
  • So I have post it as an answer… :) thanks – LoicTheAztec Dec 13 '16 at 11:27

1 Answers1

1

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:

As it is NOT really a customer behavior, adding items in cart (for some language) and then switch to another language before checkout for example.

So this should not be really a problem. This is just the behavior of a developer that is testing an e-commmerce in all possible ways, isn't it?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399