I am working on wordpress woocommerce
I want to redirect the cart page to shop page when the cart page is empty otherwise shows the cart page. Can anyone have the solution ?
Here is the code I have tried, but it does not work
function my_empty_cart() {
global $woocommerce;
if (isset( $_GET['empty-cart'] ) ) {
wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'product' ) ) );
}
}
add_action( 'init', 'my_empty_cart' );
I tried @Pushpak's solution, but it doesn't work anymore. To check the cart content please use this code:
// old woocommerce : use sizeof( $woocommerce->cart->cart_contents) to check cart content count
// In new woocommerce 2.1+ : WC()->cart->cart_contents_count to check cart content count
init
hook will run everytime. usetemplate_redirect
==============Updates=============
In new woocommerce, they have updated the functionality and now you can use following function to directly get the cart content count.
WC()->cart->cart_contents_count
2018 - 25 - Sept
working for me today::
Simply go to woocommerce folder and navigate to the CART folder.. in there is a cart-empty.php
then locate the following:
it would say getpermalink(woocommerce_.....
simply change that to
and bobs your uncle.. it will redirect to page with id you specify.
Let me know if you dont understand.
Here you are my friend :)
Just tested this myself as I needed something similar.