TransWikia.com

Adding customs fields on each product on the cart

WordPress Development Asked on December 6, 2021

Hi people i add a custom textarea field on each item of the cart. The problem is that when i tried to update the cart, the values is not updated, i lose the information. i don’t know what i am doing wrong ! If someone did this before i will thanks if you can explain me !

This i have to this moment !

// ADD THE INFORMATION AS ORDER ITEM META DATA SO THAT IT CAN BE SEEN AS PART OF THE ORDER
add_action('woocommerce_add_order_item_meta','add_product_custom_field_to_order_item_meta', 9, 3 );
function add_product_custom_field_to_order_item_meta( $item_id, $item_values, $item_key ) {
  echo($item_values);
    // the meta-key is 'Date event' because it's going to be the label too
    if( ! empty( $item_values['comment' ] ) )
        wc_update_order_item_meta( $item_id, 'Comentarios', sanitize_text_field( $item_values['.$item_key'] ) );
}

//Store the custom field
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_custom_data_vase', 10, 3 );
function add_cart_item_custom_data_vase( $cart_item_meta, $product_id, $key ) {
  global $woocommerce;
  $cart_item_meta['comment'] = $_POST['comentario_'.$key];
  echo($cart_item_meta);
  return $cart_item_meta; 
}

//Get it from the session and add it to the cart variable
function get_cart_items_from_session( $item, $values, $key ) {
    if ( array_key_exists( 'comentario_'.$key, $values ) )
        $item[ 'comment' ] = $values['comentario_'.$key];
    return $item;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'get_cart_items_from_session', 1, 3 );

And this on mi cart.php

<textarea id="comentario_<?php echo $cart_item_key ?>" name="comentario_<?php echo $cart_item_key ?>"></textarea>

enter image description here

One Answer

Answered by Sahil Doshi on December 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP