Pawan Kumawat

Add same product to cart twice instead of changing quantity in Cart Page.

How to add the same product twice to cart instead of changing quantity in WooCommerce

 

In normal case when we increase quantity of product in WooCommerce cart it simply increase quantity of same product that’s one product with incremented quantity.
But In some of cases, We like to have product in WooCommerce added in the cart as separate items and not as the same item with the changed quantity.
Basically we need to display separate cart items for product quantity > 1.
We can do that with pasting the code To the functions.php of theme.

 

This Is Done Basically In Two Steps:

 

Step 1: Split product quantities into multiple cart items.

 

In This Hooks Basically, when we are adding a new product to cart ,we have added a unique key to each cart item meta. that’s why the same product become distinct product each time.

For Adding unique meta key we will use ‘woocommerce_add_cart_item_data’ filter.

 

function AddNewProductInsteadChangeQuantity( $cart_item_data, $product_id ) {
    $distinctive_cart_item_key = md5( microtime() . rand() );
    $cart_item_data['distinctive_key'] = $distinctive_cart_item_key;
    return $cart_item_data;
}
add_filter('woocommerce_add_cart_item_data','AddNewProductInsteadChangeQuantity',11,2);

 

Step 2: Force add to cart quantity to 1 and disable +- quantity input .Product can still be added multiple times to cart

 

add_filter('woocommerce_is_sold_individually','__return_true' );

 

Categories

Related Blogs

Disable plugin update in WordPress

While it’s generally not recommended to disable plugin updates in WordPress due to security and functionality improvements, there might be specific cases where you need to prevent certain plugins from updating. Keep in mind that doing so may leave your site vulnerable to security issues and may cause compatibility problems with future WordPress versions.

How to Remove Product Tabs on Product Page

To remove product tabs on the WooCommerce product page, you can use the `woocommerce_product_tabs` filter. This filter allows you to modify or remove the default product tabs provided by WooCommerce. By removing the tabs you don’t want, you can customize the product page layout to suit your needs.

Hide Products Based On Custom Field / ACF Value : WooCommerce

To hide products in WooCommerce based on a custom field or Advanced Custom Fields (ACF) value, you’ll need to use some custom code. Specifically, you’ll have to hook into the WooCommerce product query and modify it to include your custom field or ACF value as a condition for hiding products.

Add same product to cart twice instead of changing quantity in Cart

By default, WooCommerce allows customers to change the quantity of a product in the cart by updating the quantity input field on the cart page. If the quantity is increased, the product will be added to the cart only once with the updated quantity. However, if you want to allow customers to add the same product multiple times as separate items in the cart, you’ll need to customize the cart behavior.