Search code examples
phpwordpresswoocommercecartproduct-quantity

Woocommerce add custom data to products that are already in the cart in custom file


I have file custom.php where do I add products with custom data (field_wrap_name and field_wrap_img) WC()->cart->add_to_cart

custom.php

foreach($result as $res) {
    if( isset($_POST['bookwrap_type_name']) && ! empty($_POST['bookwrap_type_name']) )
    $custom_data['custom_data']['field_wrap_name']  = $res[1];
    if( isset($_POST['bookwrap_type_img']) && ! empty($_POST['bookwrap_type_img']) )
    $custom_data['custom_data']['field_wrap_img']  = $res[2];
    WC()->cart->add_to_cart($res[0], '1', '0', array(), $custom_data );
}

and show it in the cart and order function.php

function display_custom_data_in_cart($item_data, $cart_item)
{
    if (isset($cart_item['custom_data'])) {
        if (isset($cart_item['custom_data']['field_wrap_name']) && !empty($cart_item['custom_data']['field_wrap_name'])) {
            $item_data[] = array(
                'key' => __('Type of wrapper', 'theme-child'),
                'value' => $cart_item['custom_data']['field_wrap_name'],
            );
        }
        if (isset($cart_item['custom_data']['field_wrap_img']) && !empty($cart_item['custom_data']['field_wrap_img'])) {
            $item_data[] = array(
                'key' => __('An example of a cover', 'theme-child'),
                'value' => '<img src="' . $cart_item['custom_data']['field_wrap_img'] . '" class="bwrap-cart-img">',
            );
        }
    }
    return $item_data;
}
add_filter('woocommerce_get_item_data', 'display_custom_data_in_cart', 10, 2);

Its works then i add new product, how add this $custom_data then products already in cart in custom.php?

if( in_array( $res[0], array_column( WC()->cart->get_cart(), 'product_id' ) ) ) {
    // product in cart
    ???
} else {
   WC()->cart->add_to_cart($res[0], '1', '0', array(), $custom_data );
}

Solution

  • If the product is already in cart, and if the "custom_data" is the same, you can use WC_Cart set_quantity() method like:

    if( in_array( $res[0], array_column( WC()->cart->get_cart(), 'product_id' ) ) ) {
        // Loop through cart items to get the cart item key
        foreach ( WC()->cart->get_cart() as $item_key => $item ) {
            if ( $item['product_id'] == $res[0] ) {
                WC()->cart->set_quantity( $item_key, $item['quantity']+1 );
                break;
            }
        }
    } else {
       WC()->cart->add_to_cart($res[0], '1', '0', array(), $custom_data );
    }
    

    Or you can use WC_Cart remove_cart_item() method, to remove the existing product from cart and add the new product to cart increasing its quantity:

    if( in_array( $res[0], array_column( WC()->cart->get_cart(), 'product_id' ) ) ) {
        // product in cart
        foreach ( WC()->cart->get_cart() as $item_key => $item ) {
            if ( $item['product_id'] == $res[0] ) {
                $quantity = $item['quantity'];
                WC()->cart->remove_cart_item( $item_key );
                WC()->cart->add_to_cart($res[0], $quantity+1, '0', array(), $custom_data );
                break;
            }
        }
    } else {
       WC()->cart->add_to_cart($res[0], '1', '0', array(), $custom_data );
    }
    

    Both ways should work.