[英]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.如果你不明白,请告诉我。
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.