If the cart is empty, will the cart page be redirected to the store page in woocommerce?

I am working on wordpress woocommerce

I want to redirect the basket page to the store page when the basket page is empty, otherwise the basket page is displayed. Can anyone solve the problem?

Here is the code I 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' );
+5
source share
6 answers

// 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

add_action("template_redirect", 'redirection_function');
function redirection_function(){
    global $woocommerce;
    if( is_cart() && WC()->cart->cart_contents_count == 0){
        wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'shop' ) ) );
    }
}

inithook will be launched every time. usetemplate_redirect

==============Updates=============

In the new woocommerce, they updated the functionality, and now you can use the following function to directly get a count of the contents of the basket.

WC()->cart->cart_contents_count

+9
source

Here you are my friend :)

, - .

function cart_empty_redirect_to_shop() {
    global $woocommerce;

    if ( is_page('cart') and !sizeof($woocommerce->cart->cart_contents) ) {
        wp_redirect( get_permalink( wc_get_page_id( 'shop' ) ) ); exit;
    }
}

add_action( 'wp_head', 'cart_empty_redirect_to_shop' );
+3

@Pushpak, . , :

global $woocommerce;
if ( $woocommerce->cart->cart_contents_count != 0 ) {
    // cart has content
} else {
    // cart is empty
}
+1

2018 - 25

::

function cart_empty_redirect_to_shop() {
  global $woocommerce, $woocommerce_errors;

if ( is_cart() && sizeof($woocommerce->cart->cart_contents) == 0) { 
        wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) ); 
     exit;
    }
}
add_action( 'template_redirect', 'cart_empty_redirect_to_shop' );
+1

woocommerce CART.. cart-empty.php

:

<p><a class="button1 btn btn-normal" href="<?php echo get_permalink.....

getpermalink (woocommerce _.....

<p><a class="button1 btn btn-normal" href="<?php echo get_permalink('THE PAGE ID YOU WANT TO REDIRECT TO');

, .

, .

0

, WordPress woocommeece website.when mebu , . . , 404, . , , , . , . , . , , suuport, ..... http://www.a2zfashionandshopping.in/wp-admin/options-permalink.php

0

Source: https://habr.com/ru/post/1545175/


All Articles