如果购物车是空的,购物车页面将重定向到woocommerce的购物页面?(if cart is empty, the cart page will redirect to shop page in w

系统教程 行业动态 更新时间:2024-06-14 16:57:40
如果购物车是空的,购物车页面将重定向到woocommerce的购物页面?(if cart is empty, the cart page will redirect to shop page in woocommerce?)

我正在研究wordpress woocommerce

当购物车页面为空时,我想将购物车页面重定向到购物页面,否则显示购物车页面。 任何人都可以有解决方案?

这是我尝试过的代码,但它不起作用

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

最满意答案

// 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钩子每次都会运行。 使用template_redirect

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

在新的woocommerce中,他们已经更新了功能,现在您可以使用以下功能直接获取购物车内容数量。

WC()->cart->cart_contents_count

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

WC()->cart->cart_contents_count

更多推荐

本文发布于:2023-04-13 12:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/74e66edfd8d11cbacf146b9319753108.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:购物车   页面   重定向   woocommerce   cart

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!