Only one currently added product into Woocommerce cart? - php

I would like Woocommerce to only allow 1 product in the cart. If a product is already in the cart and another one is added then it should remove the previous one.
I found this code on net:
/**
* When an item is added to the cart, remove other products
*/
function custom_maybe_empty_cart( $valid, $product_id, $quantity ) {
if( ! empty ( WC()->cart->get_cart() ) && $valid ){
WC()->cart->empty_cart();
wc_add_notice( 'Only allowed 1 item in cart, please remove previous
item.', 'error' ); // here instead popup notice need to remove prevous added product
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'custom_maybe_empty_cart',
10, 3 );
But it seems that is not working as I wanted. It need to autoremove previously added product in cart, and add latest added product in cart. Can someone to give me tip what need to update on class to work in latest woo 3.3.X ?

This one is the more compact and actual code as global $woocommerce is not needed anymore:
add_filter( 'woocommerce_add_to_cart_validation', 'auto_empty_cart', 20, 3 );
function auto_empty_cart( $passed, $product_id, $quantity ) {
if( WC()->cart->is_empty() ) return $passed; // Exit
WC()->cart->empty_cart();
return $passed;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works in all Woocommerce versions since 2.6.x

This working perfecty on Woocommerce 3.3.X
add_filter( 'woocommerce_add_to_cart_validation',
'bbloomer_only_one_in_cart', 99, 2 );
function bbloomer_only_one_in_cart( $passed, $added_product_id ) {
global $woocommerce;
// empty cart: new item will replace previous
$woocommerce->cart->empty_cart();
// display a message if you like
wc_add_notice( 'Product added to cart!', 'notice' );
return $passed;
}

Related

Empty cart before adding to cart specific products in WooCommerce

I have this code to empty the cart before adding new item to the cart.
add_action( 'woocommerce_add_cart_item_data', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
But I want to limit this function to product id 986 and 1146. I mean when ever the customer add product id 986 or 1146, empty the cart first!
please help me i am totally noob
Try the following which will empty cart before whenadding to cart product Ids 986 or **1146**:
add_filter( 'woocommerce_add_to_cart_validation', 'custom_empty_cart', 10, 3 );
function custom_empty_cart( $passed, $product_id, $quantity ) {
if( ( ! WC()->cart->is_empty() ) && in_array( $product_id, [986, 1146] ) )
WC()->cart->empty_cart();
return $passed;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Disable Woocommerce add to cart button if the product is already in cart

I am trying to set the functionality in Woocommerce on the Add to Cart button to only allow a particular product to be added once to the Cart.
Once a particular product has been added to cart the first time, the Add to Cart needs to be hidden.
In the cart I can have any number of products - just a max of quantity 1 for each product.
I was doing research and saw that I can use woocommerce_add_to_cart_validation hook. But have no idea how to start off.
How can I allow a particular product to be added once to the Cart?
Disable add to cart button if product is in cart using woocommerce_is_purchasable hook:
add_filter( 'woocommerce_is_purchasable', 'disable_add_to_cart_if_product_is_in_cart', 10, 2 );
function disable_add_to_cart_if_product_is_in_cart ( $is_purchasable, $product ){
// Loop through cart items checking if the product is already in cart
foreach ( WC()->cart->get_cart() as $cart_item ){
if( $cart_item['data']->get_id() == $product->get_id() ) {
return false;
}
}
return $is_purchasable;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works (even for product variations in variable products).
Original answer:
Here is an example using woocommerce_add_to_cart_validation hook and that will do the trick (preventing add to cart action and displaying a custom notice when needed), and using a custom utility function that will remove quantity field for your specific defined product ID:
add_filter( 'woocommerce_add_to_cart_validation', 'limit_cart_items_from_category', 10, 3 );
function limit_cart_items_from_category ( $passed, $product_id, $quantity ){
// HERE define your product ID
$targeted_product_id = 37;
// Check quantity and display notice
if( $quantity > 1 && $targeted_product_id == $product_id ){
wc_add_notice( __('Only one item quantity allowed for this product', 'woocommerce' ), 'error' );
return false;
}
// Loop through cart items checking if the product is already in cart
foreach ( WC()->cart->get_cart() as $cart_item ){
if( $targeted_product_id == $product_id && $cart_item['data']->get_id() == $targeted_product_id ) {
wc_add_notice( __('This product is already in cart (only one item is allowed).', 'woocommerce' ), 'error' );
return false;
}
}
return $passed;
}
// Checking and removing quantity field for a specific product
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
function custom_quantity_input_args( $args, $product ) {
// HERE define your product ID
$targeted_product_id = 37;
if( $targeted_product_id == $product->get_id() )
$args['min_value'] = $args['max_value'] = $args['input_value'] = 1;
return $args;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Checkout price issue using woocommerce_product_get_price hook

I need to double the price for every product in Woocommerce frontend. For this I used the following code:
add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
return $price*2;
}
But there is an error using this code. checkout page price is not correct.
for example the product orginal price is 10. We double the price by this code . so the product price is 20 now. When i added this product to the cart then cart and checkout page price is 40. That means this multiplication is take place in two times.
Please help to solve this.
Updated To double the price:
1) First you will restrict your code to single product and archives pages only:
add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
if( is_shop() || is_product_category() || is_product_tag() || is_product() )
return $price*2;
return $price;
}
2) Then for cart and checkout pages, you will change the cart item price this way:
add_filter( 'woocommerce_add_cart_item', 'set_custom_cart_item_prices', 20, 2 );
function set_custom_cart_item_prices( $cart_data, $cart_item_key ) {
// Price calculation
$new_price = $cart_data['data']->get_price() * 2;
// Set and register the new calculated price
$cart_data['data']->set_price( $new_price );
$cart_data['new_price'] = $new_price;
return $cart_data;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'set_custom_cart_item_prices_from_session', 20, 3 );
function set_custom_cart_item_prices_from_session( $session_data, $values, $key ) {
if ( ! isset( $session_data['new_price'] ) || empty ( $session_data['new_price'] ) )
return $session_data;
// Get the new calculated price and update cart session item price
$session_data['data']->set_price( $session_data['new_price'] );
return $session_data;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and working. It will change all prices as you expect in cart, checkout and order pages…

WooCommerce Redirect to Cart After Item Added

I have added code to only allow 1 item in the cart at one time. However, when a user adds any item to their cart, it does NOT direct them to the Cart page.
Here is the code that I am using:
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );
function woo_custom_add_to_cart( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
wc_add_notice( 'WARNING MESSAGE - You can only have 1 Item in your Cart. Previous Items have been removed.', 'error' );
return $cart_item_data;
}
So my goal is to keep this function and error message but take the user to the cart. I am sure there is something in this code that is preventing the user from going to the cart and staying on the product page.
Thanks in advance for your help!
If you add an error then WooCommerce won't redirect. See source. You can trying setting the notice type to "notice".
Clear cart when item is added:
add_filter( 'woocommerce_add_to_cart_validation', 'so_41002991_empty_cart' );
function so_41002991_empty_cart( $valid ){
WC()->cart->empty_cart();
wc_add_notice( __( 'WARNING MESSAGE - You can only have 1 Item in your Cart. Previous Items have been removed.', 'your-plugin' ), 'notice' );
return $valid;
}
There's a WooCommerce setting that will enable redirect to cart. But there's also a filter that you can optionally use:
add_filter( 'woocommerce_add_to_cart_redirect', 'so_41002991_redirect_to_cart' );
function so_41002991_redirect_to_cart( $url ){
return wc_get_cart_url();
}

WooCommerce: Only 1 product in cart. Replace if one is added

I am fairly new to WooCommerce so I dont know what could be of use to answer my question, this is why I have not added any codeblocks.
I would like to let customers only add one product to the cart and if they add another product the current product in cart is replaced by the last one.
Do I need to make changes in the code or is it possible with a plugin or WooCommerce setting?
I am looking forward to helpful replies.
Thanks
/**
* When an item is added to the cart, remove other products
*/
function custom_maybe_empty_cart( $valid, $product_id, $quantity ) {
if( ! empty ( WC()->cart->get_cart() ) && $valid ){
WC()->cart->empty_cart();
wc_add_notice( 'Only allowed 1 item in cart.', 'error' );
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'custom_maybe_empty_cart', 10, 3 );
Add this code your theme functions.php file.
Hope It's useful !! Enjoy

Categories