简体   繁体   English

Woocommerce:如何在woocommerce_add_to_cart钩子上wc_add_order_item_meta?

[英]Woocommerce: how to wc_add_order_item_meta on woocommerce_add_to_cart hook?

Use case: 用例:

  1. user will choose the date for product from the list and add the product to cart by button 用户将从列表中选择产品的日期,然后通过按钮将产品添加到购物车
  2. woocommerce_add_to_cart will trigger and woocommerce_add_to_cart将触发并
  3. the date append to item as meta by wc_add_order_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 is valid id, but no '_event_date' meta is included in a checkout cart item. $ result是有效的ID,但结帐购物车项目中不包含“ _event_date”元数据。 Is there something I do not understand properly? 有我不正确理解的东西吗? Meta key should be added to cart item while I recieved the id of it, or? 当我收到ID时,应该将元密钥添加到购物车项目中吗?

Thank You helgatheviking and LoicTheAztec for yours comments. 谢谢helgathevikingLoicTheAztec的意见。 The solution I found in helgatheviking 's tutorial . 我在helgatheviking教程中找到了解决方案。 This is exactly what I was searching for: 这正是我要搜索的内容:

    /*
     * Add custom data to the 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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM