如果在Woocommerce中使用Ajax删除购物车商品时,如果购物车为空,则重定向至商店页面

时间:2020-09-03 05:25:34

标签: ajax wordpress redirect woocommerce hook-woocommerce

在Woocommerce中,当购物车为空时,我想将购物车页面重定向到商店页面。使用ajax删除购物车中的商品,因此,在删除所有商品之后,我将重定向到商店页面而无需刷新页面。

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

function cartitem_after_remove_product($cart_item_key) {
    global $woocommerce;
     $total_count = $woocommerce->cart->cart_contents_count;
     if($total_count == 0)
     {
         wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'shop' ) ) );
     }
}
add_action( 'woocommerce_cart_item_removed', 'cartitem_after_remove_product' );

0 个答案:

没有答案