简体   繁体   English

如果购物车为空,购物车页面将重定向到 WooCommerce 中的商店页面?

[英]If cart is empty, the cart page will redirect to shop page in WooCommerce?

In WooCommerce, I want to redirect the cart page to shop page when the cart page is empty otherwise shows the cart page.在 WooCommerce 中,我想在购物车页面为空时将购物车页面重定向到商店页面,否则会显示购物车页面。 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' );

// 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' ) ) );
    }
}

init hook will run everytime. init钩子每次都会运行。 use template_redirect使用template_redirect

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

In new woocommerce, they have updated the functionality and now you can use following function to directly get the cart content count.在新的 woocommerce 中,他们更新了功能,现在您可以使用以下功能直接获取购物车内容计数。

WC()->cart->cart_contents_count

Just tested this myself as I needed something similar.我自己测试了这个,因为我需要类似的东西。

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' );

I tried @Pushpak's solution, but it doesn't work anymore.我尝试了@Pushpak 的解决方案,但它不再起作用了。 To check the cart content please use this code:要检查购物车内容,请使用以下代码:

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

2018 - 25 - Sept 2018 - 25 - 九月

working for me today::今天为我工作::

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' );

Simply go to woocommerce folder and navigate to the CART folder.. in there is a cart-empty.php只需转到 woocommerce 文件夹并导航到 CART 文件夹.. 有一个 cart-empty.php

then locate the following:然后找到以下内容:

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

it would say getpermalink(woocommerce_.....它会说 getpermalink(woocommerce_.....

simply change that to只需将其更改为

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

and bobs your uncle.. it will redirect to page with id you specify.和 bob 你的叔叔.. 它会重定向到你指定的 id 的页面。

Let me know if you dont understand.如果你不明白,请告诉我。

2021 Update 2021 更新

Since WooCommerce version 3 use the following to redirect to shop page when trying to access cart page and cart is already empty:由于 WooCommerce 版本 3 在尝试访问购物车页面并且购物车已经为空时使用以下内容重定向到商店页面:

add_action( 'template_redirect', 'empty_cart_redirect' );
function empty_cart_redirect(){
    if( is_cart() && WC()->cart->is_empty() ) {
        wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
        exit();
    }
}

Code goes in functions.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。


Notes - Obsolete and deprecated code:注释 - 过时和弃用的代码:

  • global $woocommerce; use with $woocommerce->cart is replaced simply by WC()->cart$woocommerce->cart一起使用被简单地替换为WC()->cart
  • sizeof($woocommerce->cart->cart_contents) == 0 is replaced by simple WC()->cart->is_empty() sizeof($woocommerce->cart->cart_contents) == 0替换为简单的WC()->cart->is_empty()
  • woocommerce_get_page_id() is replaced by wc_get_page_id() woocommerce_get_page_id()替换为wc_get_page_id()

When removing all cart items on cart page , to make the redirection active, some additional jQuery code is required, see: Redirect to shop if cart is emptied on cart page in WooCommerce 3+当删除购物车页面上的所有购物车项目时,要使重定向处于活动状态,需要一些额外的 jQuery 代码,请参阅: 如果购物车在 WooCommerce 3+ 中的购物车页面上清空,则重定向到商店

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在结帐时添加一个按钮以清空购物车并重定向到Woocommerce中的商店页面 - Add a button in checkout to empty cart and redirect to shop page in Woocommerce 如果购物车在 WooCommerce 3+ 的购物车页面上清空,则重定向到商店 - Redirect to shop if cart is emptied on cart page in WooCommerce 3+ Woocommerce - 将商店页面上的“添加到购物车”重定向到产品页面? - Woocommerce - Redirect Add-to-Cart on Shop page to Product Page? 更改商店页面中 woocommerce 商店中“添加到购物车”按钮的重定向 - Changing the redirect of the “add to cart” button in woocommerce store in Shop page WooCommerce:更改空购物车页面中“返回商店”按钮的标签 - WooCommerce: Change the label of the “Return to shop” button in the empty cart page 从“清空购物车”页面移除或隐藏 WooCommerce“返回商店”按钮? - Removing or hiding WooCommerce "Return to Shop" button from 'empty cart' page? 如何将woocommerce购物车内容定向到商店页面? - how to direct woocommerce cart content to shop page? 根据商店存档页面验证woocommerce购物车 - Validate woocommerce cart depending on shop archive page 在 Woocommerce 商店页面上自定义添加到购物车按钮 - Customizing add to cart buttons on Woocommerce shop page WooCommerce-页面刷新上的空购物车 - WooCommerce - Empty Cart on Page Reload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM