php - Woocommerce: how to wc_add_order_item_meta on woocommerce_add_to_cart hook? -
use case:
- user choose date product list , add product cart button
- woocommerce_add_to_cart trigger and
the date append item meta wc_add_order_item_meta
add_action( 'woocommerce_add_to_cart', 'add_event_date_meta', 10, 3 ); function add_event_date_meta($cart_item_key, $product_id, $quantity) { $event_date = get_cart_item_event_date(); $result = wc_add_order_item_meta( $product_id, '_event_date', $event_date); }
the $result valid id, no '_event_date' meta included in checkout cart item. there not understand properly? meta key should added cart item while recieved id of it, or?
thank helgatheviking , loictheaztec yours comments. solution found in helgatheviking's tutorial. searching for:
/* * add custom data cart item * @param array $cart_item * @param int $product_id * @return array */ function kia_add_cart_item_data( $cart_item, $product_id ){ if( isset( $_post['_custom_option'] ) ) { $cart_item['custom_option'] = sanitize_text_field( $_post['_custom_option'] ); } return $cart_item; } add_filter( 'woocommerce_add_cart_item_data', 'kia_add_cart_item_data', 10, 2 );
Comments
Post a Comment