Set decimal quantity step at product level - php

I am trying to combine the two responses from [https://stackoverflow.com/questions/62943477/set-quantity-minimum-maximum-and-step-at-product-level-in-woocommerce ] and Decimal quantity step for specific product categories in WooCommerce but I can't figure out how to edit the first lot of code so that it allows decimal quantity steps.
// Displaying quantity setting fields on admin product pages
add_action( 'woocommerce_product_options_pricing', 'wc_qty_add_product_field' );
function wc_qty_add_product_field() {
global $product_object;
$values = $product_object->get_meta('_qty_args');
woocommerce_wp_text_input( array(
'id' => 'qty_step',
'type' => 'number',
'label' => __( 'Quantity step', 'woocommerce-quantity-step' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Optional. Set quantity step (a number greater than 0)', 'woocommerce' ),
'custom_attributes' => array( 'step' => 'any', 'min' => '1'),
'value' => isset($values['qty_step']) && $values['qty_step'] > 1 ? (int) $values['qty_step'] : 1,
) );
echo '</div>';
}
// Save quantity setting fields values
add_action( 'woocommerce_admin_process_product_object', 'wc_save_product_quantity_settings' );
function wc_save_product_quantity_settings( $product ) {
if ( isset($_POST['qty_args']) ) {
$values = $product->get_meta('_qty_args');
'qty_step' => isset($_POST['qty_step']) && $_POST['qty_step'] > 1 ? (int) wc_clean($_POST['qty_step']) : 1,
) );
} else {
$product->update_meta_data( '_qty_args', array() );
}
}
// The quantity settings in action on front end
add_filter( 'woocommerce_quantity_input_args', 'filter_wc_quantity_input_args', 99, 2 );
function filter_wc_quantity_input_args( $args, $product ) {
if ( $product->is_type('variation') ) {
$parent_product = wc_get_product( $product->get_parent_id() );
$values = $parent_product->get_meta( '_qty_args' );
} else {
$values = $product->get_meta( '_qty_args' );
}
if ( ! empty( $values ) ) {
// Step value
if ( isset( $values['qty_step'] ) && $values['qty_step'] > 1 ) {
$args['step'] = $values['qty_step'];
}
}
return $args;
}
// Ajax add to cart, set "min quantity" as quantity on shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_args', 'filter_loop_add_to_cart_quantity_arg', 10, 2 );
function filter_loop_add_to_cart_quantity_arg( $args, $product ) {
$values = $product->get_meta( '_qty_args' );
if ( ! empty( $values ) ) {
// Min value
if ( isset( $values['qty_min'] ) && $values['qty_min'] > 1 ) {
$args['quantity'] = $values['qty_min'];
}
}
return $args;
}
I've tried editing the values to a decimal unit but it's not stepping up by that value and it's not saving the value.

Related

Add a select field on WooCommerce single product pages that allows an additional product to be added to cart

I am looking to add a selection of products to some of our single product pages.
I have added a select field that is grabbing all of the products of a specific category and adding them as options.
Based on the users selection, I would like to add the selected product to the cart along with the main product. This only if the additional product is not yet in the cart.
Here is my code attempt:
function comp_bracket_selection( $selection ) {
echo '<div><br></div>';
global $product;
$domain = 'woocommerce';
$args = array(
'category' => 'compressor-brackets',
'limit' => -1,
);
$product_array = wc_get_products( $args );
foreach ($product_array as $comp_product) {
$comp_id = $comp_product->get_id();
echo 'Product id: ' . $comp_id . ' | ';
$options[$comp_id] = $comp_product->get_name();
}
array_unshift($options, 'Choose an option');
woocommerce_form_field('compressor-options', array(
'id' => 'compressor-selection',
'type' => 'select',
'label' => __('Choose Compressor & Bracket', $domain),
'required' => false,
'options' => $options,
),''
);
echo '<p>Number of Compressor & Bracket options: ' . sizeof( $product_array ) . '</p>';
}
add_action( 'woocommerce_before_add_to_cart_button', 'comp_bracket_selection' );
function custom_add_to_cart() {
if (isset($_POST['compressor-options'])) {
$product_id = $_POST['compressor-options'];
$found = false;
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
WC()->cart->add_to_cart( $product_id );
}
}
}
add_action( 'woocommerce_add_to_cart', 'custom_add_to_cart' );
But I have issues getting the selected products added to the cart. Any advice would be much appreciated.
Some notes on your code attempt/question:
The mistake you make is using array_unshift() causing all numerical array keys will be modified to start counting from zero
You use $_POST['compressor-options'] and $_POST['compressor-selection'] while they should be equal
To check whether a product is already in cart you can use WC_Cart::find_product_in_cart()
So you get:
function action_woocommerce_before_add_to_cart_button() {
global $product;
$domain = 'woocommerce';
$args = array(
'category' => 'compressor-bracket',
'limit' => -1,
);
// Retrieving products
$product_array = wc_get_products( $args );
// NOT empty
if ( ! empty( $product_array ) ) {
foreach ( $product_array as $product ) {
$product_id = $product->get_id();
echo 'Product id: ' . $product_id . ' | ';
$options[$product_id] = $product->get_name();
}
$options = array( 0 => __( 'Choose an option', $domain ) ) + $options;
// Add select field
woocommerce_form_field( 'compressor-options', array(
'id' => 'compressor-selection',
'type' => 'select',
'label' => __( 'Choose Compressor & Bracket', $domain ),
'required' => false,
'options' => $options,
),'' );
echo '<p>Number of Compressor & Bracket options: ' . sizeof( $product_array ) . '</p>';
}
}
add_action( 'woocommerce_before_add_to_cart_button', 'action_woocommerce_before_add_to_cart_button' );
function filter_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
if ( isset( $_POST['compressor-options'] ) ) {
// Get product ID
$the_product_id = sanitize_text_field( $_POST['compressor-options'] );
// WC Cart
if ( WC()->cart ) {
// Get cart
$cart = WC()->cart;
// If cart is NOT empty
if ( ! $cart->is_empty() ) {
// Cart id
$product_cart_id = $cart->generate_cart_id( $the_product_id );
// Find product in cart
$in_cart = $cart->find_product_in_cart( $product_cart_id );
// NOT in cart
if ( ! $in_cart ) {
$cart->add_to_cart( $the_product_id );
}
} else {
$cart->add_to_cart( $the_product_id );
}
}
}
}
add_action( 'woocommerce_add_to_cart', 'filter_woocommerce_add_to_cart', 10, 6 );
Note: if you want to exclude the current product from the select option list
Change:
$args = array(
'category' => 'compressor-bracket',
'limit' => -1,
);
To:
$args = array(
'category' => 'compressor-bracket',
'limit' => -1,
'exclude' => array( $product->get_id() ),
);

How to calculate and update sales price in woocommerce product page

I inserted two radio buttons as product add ons by using the following to calculate extra packaging cost. But I have two problems.
First instead of getting the value of my custom field eg 4.35 it gets a value of 4.00.
Second how can I update the sales price in my product page when I choose an option
function add_custom_fees_before_add_to_cart() {
global $product;
$myfee = floatval(get_post_meta(get_the_ID(), '_skrprom', TRUE)) + floatval(0.90);
$args = array(
'type' => 'radio',
'class' => array( 'form-row-wide' ),
'options' => array(
'' => 'Τηλεφωνική Παραγγελία',
$myfee => 'Έξοδα Συσκευασίας '.$myfee.'€',
),
'default' => ''
);
?>
<div class="custom-fees-wrap">
<label for="iconic-engraving"><?php _e( 'Customize Your Order!', 'textdomain' ); ?></label>
<?php woocommerce_form_field( 'custom_fees', $args, '' ); ?>
</div>
<?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_fees_before_add_to_cart', 99 );
function save_value_add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
$custom_fees = filter_input( INPUT_POST, 'custom_fees' );
if ( empty( $custom_fees ) ) {
return $cart_item_data;
}
$cart_item_data['custom_fees'] = $custom_fees;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_value_add_cart_item_data', 99, 3 );
function calculate_add_cart_fee() {
global $woocommerce;
$cart_items = $woocommerce->cart->get_cart();
foreach( $cart_items as $key => $item ) {
if( !isset( $item['custom_fees'] ) && empty( $item['custom_fees'] ) ) continue;
$woocommerce->cart->add_fee( __('Έξοδα Συσκευασίας', 'textdomain'), $item['custom_fees'] );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'calculate_add_cart_fee', 99 );

Set minimum, maximum and step quantity programmatically for specific variable products in WooCommerce

I use WooCommerce cart quantity won't change after cart update answer code to customize some product quantity arguments (as min, max and step arguments).
Now I have a variable product with product id 27525, and I want to apply another rule to this product only, Quantity rule details: min(default) qty 1, max qty 24 and step 1.
I tried to use the code below, the problem is,
if no variations are selected, the default/min quantity will be 25, the rule is the same as the general quantity settings,
If I choose an available variation, the default quantity will be 24.
add_filter( 'woocommerce_available_variation', 'variation_quantity_input_args', 10, 2 );
function variation_quantity_input_args( $args, $product ) {
$sample_product_id = array(27525);
if (in_array( $product->get_id(), $sample_product_id)) {
$args['min_qty'] = 1;
$args['max_qty'] = 24;
return $args;
}
}
How to change the code and apply the rules to variable product 27525?
$args['min_qty'] = 1; //default and min qty
$args['max_qty'] = 24; // max qty
$args['step'] = 1; // Seems won't work use woocommerce_available_variation, but I want to change the step to 1
The woocommerce_available_variation hook has the WC_Product_Variation product object as its third parameter and not the variable product.
With the woocommerce_available_variation hook you cannot set the
product step.
The available arguments are the following (source code of the WooCommerce /includes/class-wc-product-variable.php file #version 3.0.0):
return apply_filters(
'woocommerce_available_variation',
array(
'attributes' => $variation->get_variation_attributes(),
'availability_html' => wc_get_stock_html( $variation ),
'backorders_allowed' => $variation->backorders_allowed(),
'dimensions' => $variation->get_dimensions( false ),
'dimensions_html' => wc_format_dimensions( $variation->get_dimensions( false ) ),
'display_price' => wc_get_price_to_display( $variation ),
'display_regular_price' => wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) ),
'image' => wc_get_product_attachment_props( $variation->get_image_id() ),
'image_id' => $variation->get_image_id(),
'is_downloadable' => $variation->is_downloadable(),
'is_in_stock' => $variation->is_in_stock(),
'is_purchasable' => $variation->is_purchasable(),
'is_sold_individually' => $variation->is_sold_individually() ? 'yes' : 'no',
'is_virtual' => $variation->is_virtual(),
'max_qty' => 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : '',
'min_qty' => $variation->get_min_purchase_quantity(),
'price_html' => $show_variation_price ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',
'sku' => $variation->get_sku(),
'variation_description' => wc_format_content( $variation->get_description() ),
'variation_id' => $variation->get_id(),
'variation_is_active' => $variation->variation_is_active(),
'variation_is_visible' => $variation->variation_is_visible(),
'weight' => $variation->get_weight(),
'weight_html' => wc_format_weight( $variation->get_weight() ),
),
$this,
$variation
);
To set the step to product variations you will need to use the woocommerce_quantity_input_args hook.
If you want to set the same step to all product variations you can use this hook.
The available arguments are the following (source code of the WooCommerce /includes/wc-template-functions.php file #version 2.5.0):
$defaults = array(
'input_id' => uniqid( 'quantity_' ),
'input_name' => 'quantity',
'input_value' => '1',
'classes' => apply_filters( 'woocommerce_quantity_input_classes', array( 'input-text', 'qty', 'text' ), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', -1, $product ),
'min_value' => apply_filters( 'woocommerce_quantity_input_min', 0, $product ),
'step' => apply_filters( 'woocommerce_quantity_input_step', 1, $product ),
'pattern' => apply_filters( 'woocommerce_quantity_input_pattern', has_filter( 'woocommerce_stock_amount', 'intval' ) ? '[0-9]*' : '' ),
'inputmode' => apply_filters( 'woocommerce_quantity_input_inputmode', has_filter( 'woocommerce_stock_amount', 'intval' ) ? 'numeric' : '' ),
'product_name' => $product ? $product->get_title() : '',
'placeholder' => apply_filters( 'woocommerce_quantity_input_placeholder', '', $product ),
);
If you want to manage the product step, the minimum quantity and the maximum quantity as a custom meta of the product, see #LoicTheAztec's answer:
Set quantity minimum, maximum and step at product level in Woocommerce
ANSWER
To answer your question, if you want all product variations that have the parent variable product id equal to 27525 to have the following values:
Min quantity: 1
Max quantity: 24
Step: 1
You can use the following functions:
It works in the product page, in the cart and in the loop.
// set the min and max quantity for each product variation (based on the variable product id)
add_filter( 'woocommerce_available_variation', 'variation_quantity_input_args', 10, 3 );
function variation_quantity_input_args( $args, $product, $variation ) {
// set variable product ids
$sample_product_id = array( 27525 );
// if the selected variation belongs to one of the variable product ids
// set the min and max quantity
if ( in_array( $variation->get_parent_id(), $sample_product_id ) ) {
$args['min_qty'] = 1;
$args['max_qty'] = 24;
return $args;
}
return $args;
}
// set the step for specific variable products (and for each product variation)
add_filter( 'woocommerce_quantity_input_args', 'set_step_for_specific_variable_products', 10, 2 );
function set_step_for_specific_variable_products( $args, $product ) {
// set variable product ids
$sample_product_id = array( 27525 );
// on the product page
if ( ! is_cart() ) {
// if the id of the variable product is in the array
if ( in_array( $product->get_id(), $sample_product_id ) ) {
$args['input_value'] = 1;
$args['min_value'] = 1;
$args['max_value'] = 24;
$args['step'] = 1;
}
// in the cart
} else {
// if the id of the product variation belongs to a variable product present in the array
if ( in_array( $product->get_parent_id(), $sample_product_id ) ) {
$args['min_value'] = 1;
$args['step'] = 1;
}
}
return $args;
}
The code has been tested and works. Add it to your active theme's functions.php.

Save and display order item custom meta data in Woocommerce

I have some custom code was working perfectly and since I have updated Woocommerce to version 3.5.2 it is not working anymore, not sure if it is because I changed the wordpress theme or because because plugin updates.
My problem is that the value of the custom fields doesn't appear on the order page from woocommerce or even on the order email.
Here is the related code:
// Display Fields using WooCommerce Action Hook
add_action('woocommerce_product_options_general_product_data', 'woocom_general_product_data_custom_field');
function woocom_general_product_data_custom_field()
{
// FieldName1
woocommerce_wp_text_input(array('id' => 'FieldName1', 'label' => __('FieldName1', 'woocommerce'), 'placeholder' => '', 'desc_tip' => 'false', 'description' => __('', 'woocommerce')));
// FieldType1
woocommerce_wp_text_input(array('id' => 'FieldType1', 'label' => __('FieldType1', 'woocommerce'), 'placeholder' => '', 'desc_tip' => 'false', 'description' => __('', 'woocommerce')));
// FieldLenght1
woocommerce_wp_text_input(array('id' => 'FieldLenght1', 'label' => __('FieldLenght1', 'woocommerce'), 'placeholder' => '', 'desc_tip' => 'false', 'description' => __('', 'woocommerce')));
// Dropdown1
woocommerce_wp_text_input(array('id' => 'Dropdown1', 'label' => __('Dropdown1', 'woocommerce'), 'placeholder' => '', 'desc_tip' => 'false', 'description' => __('', 'woocommerce')));
}
// Hook to save the data value from the custom fields
add_action('woocommerce_process_product_meta', 'woocom_save_general_proddata_custom_field');
function woocom_save_general_proddata_custom_field($post_id)
{
// Save Label Option 1
update_post_meta($post_id, 'FieldName1', esc_attr($_POST['FieldName1']));
// Save Label Option 1
update_post_meta($post_id, 'FieldType1', esc_attr($_POST['FieldType1']));
// Save Label Option 1
update_post_meta($post_id, 'FieldLenght1', esc_attr($_POST['FieldLenght1']));
// Save Dropdown1
update_post_meta($post_id, 'Dropdown1', esc_attr($_POST['Dropdown1']));
}
/**
* Register the 'Custom Column' column in the importer.
*
* #param array $options
* #return array $options
*/
function add_column_to_importer($options)
{
// column slug => column name
$options['FieldName1'] = 'FieldName1';
$options['FieldType1'] = 'FieldType1';
$options['FieldLenght1'] = 'FieldLenght1';
$options['Dropdown1'] = 'Dropdown1';
return $options;
}
add_filter('woocommerce_csv_product_import_mapping_options', 'add_column_to_importer');
/**
* Process the data read from the CSV file.
* This just saves the value in meta data, but you can do anything you want here with the data.
*
* #param WC_Product $object - Product being imported or updated.
* #param array $data - CSV data read for the product.
* #return WC_Product $object
*/
function process_import( $object, $data ) {
if ( ! empty( $data['FieldName1'] ) ) {
$object->update_meta_data( 'FieldName1', $data['FieldName1'] );
}
if ( ! empty( $data['FieldType1'] ) ) {
$object->update_meta_data( 'FieldType1', $data['FieldType1'] );
}
if ( ! empty( $data['FieldLenght1'] ) ) {
$object->update_meta_data( 'FieldLenght1', $data['FieldLenght1'] );
}
if ( ! empty( $data['Dropdown1'] ) ) {
$object->update_meta_data( 'Dropdown1', $data['Dropdown1'] );
}
return $object;
}
add_filter( 'woocommerce_product_import_pre_insert_product_object', 'process_import', 10, 2 );
// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');
function my_custom_checkout_field() {
global $product;
$product_id = $product->get_id();
// Get the field name of InputText1
$FieldType1 = get_post_meta($product_id, 'FieldType1', true);
$FieldName1 = get_post_meta($product_id, 'FieldName1', true);
$FieldLenght1 = get_post_meta($product_id, 'FieldLenght1', true);
$Dropdown1 = get_post_meta($product_id, 'Dropdown1', true);
$Dropdown1Content = explode(", ", $Dropdown1);
echo '<table class="extravariations" cellspacing="0">
<tbody>';
// Field 1
if( ! empty( $FieldType1 ) ){
if( $FieldType1 == "TEXT AREA"){
echo '
<tr>
<td class="label">
<label for="'.$FieldName1.'" id="label1">'.$FieldName1.':</label><br>
</td>
<td class="value">
<textarea id="'.$FieldName1.'" class="inputfield1" name="FieldTypeValue1" maxlength="'.$FieldLenght1.'" rows="2" cols="80" placeholder="" required></textarea>
</td>
</tr>';
}
if( $FieldType1 == "TEXT BOX"){
echo '<tr>
<td class="label">
<label for="'.$FieldName1.'" id="label1">'.$FieldName1.':</label>
</td>
<td class="value">
<input id="'.$FieldName1.'" class="inputfield1" type="text" maxlength="'.$FieldLenght1.'" name="FieldTypeValue1" value="" required>
</td>
</tr>';
}
if( $FieldType1 == "DROP DOWN"){
echo ' <tr>
<td class="label">
<label for="'.$FieldName1.'" id="label1">'.$FieldName1.':</label>
</td>
<td class="value">';
echo'<select id="'.$FieldName1.'" class="inputfield1" name="FieldTypeValue1" >';
foreach ($Dropdown1Content as $Dropdown1IndividualContent) {
echo '<option value="'.$Dropdown1IndividualContent.'">';
echo $Dropdown1IndividualContent;
echo '</option>';
}
echo'</td></tr>';
}
}
echo' </tbody>
</table>';
}
// Store custom field label and value in cart item data
add_action( 'woocommerce_add_cart_item_data','save_my_custom_checkout_field', 20, 2 );
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
$label1 = get_post_meta( $product_id, 'FieldName1', true );
if( isset( $_REQUEST['FieldTypeValue1'] ) && ! empty( $label1 ) )
$cart_item_data['custom_data']['1'] = array(
'label' => $label1,
'value' => sanitize_text_field( $_REQUEST['FieldTypeValue1'] ),
);
if( count($cart_item_data['custom_data']) > 0 )
$cart_item_data['custom_data']['key'] = md5( microtime().rand() );
return $cart_item_data;
}
// Display items custom fields label and value in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 20, 2 );
function render_meta_on_cart_and_checkout( $cart_data, $cart_item ){
$custom_items = array();
if( !empty( $cart_data ) )
$custom_items = $cart_data;
if( isset( $cart_item['custom_data'] ) ) {
foreach( $cart_item['custom_data'] as $key => $custom_data ){
if( $key != 'key' ){
$custom_items[] = array(
'name' => $custom_data['label'],
'value' => $custom_data['value'],
);
}
}
}
return $custom_items;
}
// Save item custom fields label and value as order item meta data
add_action('woocommerce_add_order_item_meta','save_in_order_item_meta', 10, 3 );
function save_in_order_item_meta( $item_id, $values, $cart_item_key ) {
if( isset( $values['custom_data'] ) ) {
wc_add_order_item_meta( $item_id, $values['custom_data']['label'], $values['custom_data']['value'] );
}
}
I have been looking for hours and i have no idea how to solve it or what the problem is. Any help or tips will be really helpful. Thanks a lot.
The first main problem is $cart_item_data['custom_data']['1'] = array( that should be instead:
$cart_item_data['custom_data'] = array(
'label' => $label1,
'value' => sanitize_text_field( $_REQUEST['FieldTypeValue1'] ),
);
Then the 2nd main problem is the last function where woocommerce_get_item_data is obsolete and replaced by woocommerce_checkout_create_order_line_item already answered in your last question.
So here below I have revisited your 3 last functions:
// Store custom field label and value in cart item data
add_action( 'woocommerce_add_cart_item_data','add_custom_data_as_custom_cart_item_data', 10, 3 );
function add_custom_data_as_custom_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
if( isset( $_REQUEST['FieldTypeValue1'] ) ) {
// Get an instance of the WC_Product object
$product = wc_get_product( $variation_id > 0 ? $variation_id : $product_id );
if( $label = $product->get_meta('FieldName1') ){
$cart_item_data['custom_data'] = array(
'label' => $product->get_meta('FieldName1'),
'value' => sanitize_text_field( $_REQUEST['FieldTypeValue1'] ),
'unique_key' => md5( microtime().rand() ),
);
}
}
return $cart_item_data;
}
// Display cart item custom data in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_cart_item_custom_on_cart_and_checkout', 10, 2 );
function display_cart_item_custom_on_cart_and_checkout( $cart_item_data, $cart_item ){
if( isset($cart_item['custom_data']['label']) && isset($cart_item['custom_data']['value']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label'],
'value' => $cart_item['custom_data']['value'],
);
}
return $cart_item_data;
}
// Save cart item custom data as order item meta data and display it everywhere in Orders and email notifications
add_action('woocommerce_checkout_create_order_line_item', 'save_custom_order_item_meta_data', 10, 4 );
function save_custom_order_item_meta_data( $item, $cart_item_key, $values, $order ) {
if( isset( $values['custom_data']['label'] ) && isset( $values['custom_data']['value'] ) ) {
$item->update_meta_data( $values['custom_data']['label'], $values['custom_data']['value'] );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.

WPML - Cart item data custom Fields don't update when switching language

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
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?

Categories