WooCommerce Redirect to Cart After Item Added - php

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();
}

Related

Change URL of “View Cart” button on WooCommerce Pages other than Cart and Checkout

I am applying the below code to change the URL of “View Cart” button on the Shop page.
// Change View Cart Button URL from /cart to /checkout
add_filter( 'woocommerce_add_to_cart_redirect', 'cart_to_checkout' );
function cart_to_checkout( $url ) {
return wc_get_checkout_url();
}
What is the filter that I need to use to achieve the same for “View Cart” button in the “Woocommerce error message” on the Single Product Page?
Woocommerce error message” appears something like this: “The maximum allowed quantity for ‘Product X’ is 3 (you currently have 3 in your cart). “View Cart”
So, I need to change the URL of the “View Cart” button in the above message.
And also, all the View Cart buttons must point to Checkout Page and not Cart Page.
Thanks!
As this notice is hard coded in WC_Cart add_to_cart() method (from line 1075 to 1083):
throw new Exception(
sprintf(
'%s %s',
wc_get_cart_url(),
__( 'View cart', 'woocommerce' ),
/* translators: 1: quantity in stock 2: current quantity */
sprintf( __( 'You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.', 'woocommerce' ), wc_format_stock_quantity_for_display( $product_data->get_stock_quantity(), $product_data ), wc_format_stock_quantity_for_display( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ], $product_data ) )
)
);
The only way to change the url link is using woocommerce_get_cart_url filter hook located inside the function wc_get_cart_url() (used in this notice) as follow for single product pages only:
add_filter( 'woocommerce_get_cart_url', 'filter_get_cart_url' );
function filter_get_cart_url( $url ) {
// Only on single product pages
if( is_product() )
$url = wc_get_checkout_url();
return $url;
}
To change all url links that are using wc_get_cart_url() to checkout url, you will use instead:
add_filter( 'woocommerce_get_cart_url', 'wc_get_checkout_url' );
Addition: To make it work everywhere except on cart and checkout pages, use the following:
add_filter( 'woocommerce_get_cart_url', 'filter_get_cart_url' );
function filter_get_cart_url( $url ) {
// Except on cart and checkout pages
if( ! ( is_cart() || is_checkout() ) )
$url = wc_get_checkout_url();
return $url;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Related: Change the "view cart" product overlay button on product loops in Woocommerce

Empty cart upon page load allowing add to cart in WooCommerce

In WooCommerce, I'm trying to create a one-page landing page with a product and checkout.
When the page is loaded I want the cart to empty.
But I want to be able to add to cart, and checkout on the same page.
I want to achieve this only on pages with a specific page-template.
I'm working from Clear Woocommerce Cart on Page Load Even for logged in users answer code.
This is what I have:
?>
<?php
//epmty cart
if (! WC()->cart->is_empty() ) {
WC()->cart->empty_cart( true );
}
<?php
// show add to cart button
echo do_shortcode( '[add_to_cart id='22']');
?>
// allow checkout Even though Cart Is Empty
add_filter( 'woocommerce_checkout_redirect_empty_cart', '__return_false' );
add_filter( 'woocommerce_checkout_update_order_review_expired', '__return_false' );
?>
<?php
echo do_shortcode( '[woocommerce_checkout' );
?>
The problem I think is that the page refreshes upon add to cart and therefor empties the cart again. How do I make it run only once? or is there a better way to clear the cart?
Update 2
You could try the following code in your template:
$current_product_id = 22; // The product ID
$cart = WC()->cart; // The WC_Cart Object
// When cart is not empty
if ( ! $cart->is_empty() ) {
// Loop through cart items
foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
// If the cart item is not the current defined product ID
if( $current_product_id != $cart_item['product_id'] ) {
$cart->remove_cart_item( $cart_item_key ); // remove it from cart
}
}
}
// When cart is empty
else {
// allow checkout Event
add_filter( 'woocommerce_checkout_redirect_empty_cart', '__return_false' );
add_filter( 'woocommerce_checkout_update_order_review_expired', '__return_false' );
}
// show add to cart button
echo do_shortcode( "[add_to_cart id='22']");
// show Checkout
echo do_shortcode( "[woocommerce_checkout]" );

Only one currently added product into Woocommerce cart?

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;
}

Role based taxes in woocommerce / Cart page

I've set up a woocommerce store with multiple users (B2C & B2B). Some of them will automatically be exempt from tax and just have tax disappear from the cart/checkout. I've used a dynamic pricing plugin to provide different prices to different roles but there is no options for tax variations.
I found this answer and tried to put it in place Role based taxes in woocommerce but as #Jplus2 is telling, #dryan144 solution is not good because it is only applied during the checkout and not on the cart. I tried to figure out the way to do it but I still do have to refresh my 'cart' page to display taxes to 0 (as they are included in the price for "guest" or "customer", any help to launch the action when my cart page is called?
I did the following:
add_filter( 'woocommerce_before_cart_contents', 'prevent_wholesaler_taxes' );
add_filter( 'woocommerce_before_shipping_calculator', 'prevent_wholesaler_taxes' );
add_filter( 'woocommerce_before_checkout_billing_form', 'prevent_wholesaler_taxes' );
function prevent_wholesaler_taxes() {
global $woocommerce;
if ( is_user_logged_in() && !(current_user_can('customer'))){
$woocommerce->customer->set_is_vat_exempt(false);
} else {
$woocommerce->customer->set_is_vat_exempt(true);
}
} //end prevent_wholesaler_taxes
It's working straight away sometimes but most of the time it's only after working after a refresh of my cart which is not good.
Try to add https://eshoes.com.au/product/test-shoes08/ to the cart then -> View your basket
Any help would be greately appreciated ;)
Cheers
This solution works perfectly, instead of using set_is_vat_exempt() I simply used $tax)class = 'Zero Rate':
add_filter( 'woocommerce_before_cart_contents', 'wc_diff_rate_for_user', 1, 2 );
add_filter( 'woocommerce_before_shipping_calculator', 'wc_diff_rate_for_user', 1, 2);
add_filter( 'woocommerce_before_checkout_billing_form', 'wc_diff_rate_for_user', 1, 2 );
add_filter( 'woocommerce_product_tax_class', 'wc_diff_rate_for_user', 1, 2 );
function wc_diff_rate_for_user( $tax_class ) {
if ( !is_user_logged_in() || current_user_can( 'customer' ) ) {
$tax_class = 'Zero Rate';
}
return $tax_class;
}

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