WooCommerce

How to Add or Change Meta Data in the WooCommerce Cart Items?

In this article I want to talk about how in the process of creating an online store using WooCommerce we can add or change meta data in the WooCommerce cart items and where we can apply it.

The first case, you need to change the product price in the cart.

If you want to change the product price in the cart it’s quite easy to do. You can send the new price using $_GET or $_POST. For example, adding a new data attribute data-product_price_'.esc_attr( $product->get_id() ).'=new_price in the loop/add-to-cart.php template file.

We can also change the new price value via JS after some actions by the buyer.


echo apply_filters( 'woocommerce_loop_add_to_cart_link',
	sprintf( '<a rel="nofollow" data-product_price_'.esc_attr( $product->get_id() ).'="'. $product->get_price().'" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
		esc_url( $product->add_to_cart_url() ),
		esc_attr( isset( $quantity ) ? $quantity : 1 ),
		esc_attr( $product->get_id() ),
		esc_attr( $product->get_sku() ),
		esc_attr( isset( $class ) ? $class : 'button' ),
		esc_html( $product->add_to_cart_text() )
	),
$product );

Insert this code in the functions.php file of your theme.


add_filter( 'woocommerce_add_cart_item_data', function( $cart_item_data, $product_id, $variation_id )
{
    if ( $variation_id ){
        $product_id = $variation_id;
    }

    $posted_new_price_field = 'product_price_' . $product_id;

    // no need to check is_nyp b/c this has already been validated by validate_add_cart_item()
    if( isset( $_REQUEST[ $posted_new_price_field ] ) ) {
        $cart_item_data['new_price'] = ( double ) warp_standardize_number( $_REQUEST[ $posted_new_price_field ] );
    }
    
    return $cart_item_data;
    
}, 5, 3 );

add_filter( 'woocommerce_get_cart_item_from_session', function( $cart_item, $values )
{
    if ( isset( $values['new_price'] ) ) 
    {
        $cart_item['new_price'] = $values['new_price'];
        
        $product = $cart_item['data'];
        $product->set_price( $cart_item['new_price'] );
        $product->set_regular_price( $cart_item['new_price'] );
        $product->set_sale_price( $cart_item['new_price'] );
    }

    return $cart_item;
}, 11, 2 );

add_filter( 'woocommerce_add_cart_item', function( $cart_item )
{
    if ( isset( $cart_item['new_price'] ) ) 
    {
        $product = $cart_item['data'];
        $product->set_price( $cart_item['new_price'] );
        $product->set_regular_price( $cart_item['new_price'] );
        $product->set_sale_price( $cart_item['new_price'] );
    }    
}, 11, 1 );

The second case, you need to add a few custom meta data in the cart items.

Do you want to optimize your site, and increase the speed load of the site, you don’t need to create many variable products, you can use product attributes.

I preferred to send an array of custom meta fields as a string: data-product_meta_'.esc_attr( $product->get_id() ).'="Diameter:16|Stretching:4×100|Width:5.5|Disc Type:A-disk"


add_filter( 'woocommerce_add_cart_item_data', function( $cart_item_data, $product_id, $variation_id )
{
    if ( $variation_id ){
        $product_id = $variation_id;
    }

	$posted_meta_field = 'product_meta_' . $product_id;
    
	if( isset( $_REQUEST[ $posted_meta_field ] ) )
	{
		$meta_list_arr = explode('|', $_REQUEST[ $posted_meta_field ]);
		foreach($meta_list_arr as $meta)
		{
			$meta_arr = explode(':', $meta);

			if(isset($meta_arr[0]) && isset($meta_arr[1]))
			{
				$cart_item_data['meta'][$meta_arr[0]] = $meta_arr[1];
			}
		}
	}

	//$cart_item_data['meta']['field_name'] = $field_value;
    
    return $cart_item_data;
    
}, 5, 3 );

add_filter( 'woocommerce_get_cart_item_from_session', function( $cart_item, $values )
{
	if ( isset( $values['meta'] ) ) {
		$cart_item['meta'] = $values['meta'];
	}

    return $cart_item;
}, 11, 2 );
    
// Adding to the cart information about additional fields
add_filter( 'woocommerce_get_item_data', function( $item_data, $cart_item )
{
    if(isset($cart_item['meta'])) 
    {
        foreach( $cart_item['meta'] as $label => $value ) 
        {
            $item_data[] = array(
                'key'   => $label,
                'value' => $value,
            );    
        }
    }
    
    return $item_data;
}, 11, 2 );

// Adding additional product information to the order
add_action( 'woocommerce_checkout_create_order_line_item', function( $item, $cart_item_key, $values, $order )
{
	if ( isset( $values['meta'] ) )
	{
		foreach( $values['meta'] as $label => $value )
		{
			$item->add_meta_data( $label, $value, true );
		}
	}
}, 10, 4 );

Leave a comment