How to retrieve cart_item_data with WooCommerce?

后端 未结 2 1293
囚心锁ツ
囚心锁ツ 2020-12-08 02:59

During the add_to_cart function, there is a filter to add \"cart item data\". The filter is woocommerce_add_cart_item_data. I expected to store my custom plugin

2条回答
  •  臣服心动
    2020-12-08 03:34

    I could not get the default cart item data to work, unfortunately. I feel it may not be properly implemented, or may even be deprecated, as there is a lack of support and documentation.

    Instead, I used a cart session variable to accomplish the same thing. It's simply an array where each key is the cart_item_key. The value of each array is yet another array, containing a key-value pair of custom fields. So it's essentially the same thing as the built-in cart item data, except stored as cart session data instead.

    Here is a Gist containing some utility functions to make it easy:

    https://gist.github.com/RadGH/e3444fc661554a0f8c6f


    Or if you want to build it yourself, the magic is in WC()->session. Here are the two key functions for this to work:

    WC()->session->get('_my_cart_item_data');
    WC()->session->set('_my_cart_item_data', $cart_item_data_array);
    

    These are the action hooks you will need:

    <<

提交回复
热议问题