Skip to content
Imaginate Solutions Logo
  • Home
  • Plugins
    • Custom Payment Gateways for WooCommerce
    • File Uploads Addon for WooCommerce
    • Custom Shipping Methods for WooCommerce
    • Payment Gateways by User Roles for WooCommerce
    • Variations Radio Buttons for WooCommerce
    • WooCommerce Variations Reports
    • WooCommerce Export Orders
  • Checkout
  • Blog
  • Contact Us
  • My Account
Update Existing WooCommerce Cart Meta Data

Update Existing WooCommerce Cart Meta Data

/ How To's / By Dhruvin

There may be instances where you want to add additional data whenever a product is added to the cart. This additional data could be the file selected while adding a product to the cart as we do in the case of WooCommerce Addon Uploads Pro. In such cases, the best place to store the data for further processing would be Cart Item Meta.

Adding cart item meta

To add cart item meta the best filter which WooCommerce provides is woocommerce_add_cart_item_data. This filter is triggered when an add to cart action is triggered. Let’s understand the parameters available with this filter. The filter description is:

apply_filters( 'woocommerce_add_cart_item_data',  $cart_item_data,  $product_id,  $variation_id );

The cart item data is the object that will hold the custom data that is being passed during an add to cart operation.

Example usage of the filter is as below:

function img_sol_add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
    $cart_item_data['meta1'] = $_POST['meta1'];
    $cart_item_data['meta2'] = $_POST['meta2'];
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'img_sol_add_cart_item_data', 99, 3 );

Updating Existing Cart Meta Data

Now that you have added custom meta data to your items added to the cart, what if you want to update the value of this meta data?

A typical use case for this could be allowing users to edit some of the options while on the cart page. In that case you can use the below function to alter your WooCommerce Cart Item Meta data values:

function prefix_update_existing_cart_item_meta() {
    $cart = WC()->cart->cart_contents;
    foreach( $cart as $cart_item_id=>$cart_item ) {
        $cart_item['meta1'] = 'New value to be updated';

        WC()->cart->cart_contents[ $cart_item_id ] = $cart_item;
    }
    WC()->cart->set_session();
}

In the above code snippet, we are getting the WooCommerce cart contents using cart_contents property. We then loop through each item and update the cart item meta.

Please note that the entire cart item present in $cart_item object needs to be updated back.

The final save happens using set_session function on the WooCommerce Cart object.

Please let me know in the comments below if the code is functioning fine for you or not or if you are facing any errors.

Post navigation
← Previous Post
Next Post →
  • About
  • Privacy Policy
  • Contact Us
  • Terms and Conditions
  • Affiliates Area
Copyright © 2025 Imaginate Solutions
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
ACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT