大家好,我在购物车的每个项目上添加了一个自定义textarea字段。问题是,当我尝试更新购物车时,值没有更新,我丢失了信息。我不知道我做错了什么!如果之前有人这样做,如果你能解释我,我会感谢你!
这一刻我不得不这样!
// 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 );
这是我的购物车。php
<textarea id="comentario_<?php echo $cart_item_key ?>" name="comentario_<?php echo $cart_item_key ?>"></textarea>