简体   繁体   English

如果我单击已在购物车中的产品的“添加到购物车”按钮,如何重定向到购物车页面

[英]How do I redirect to the cart page if I click on the 'Add to cart' button for a product that's already in cart

How do I redirect to the cart page if I click on the 'Add to cart' button for a product that's already in cart?如果我单击购物车中已有产品的“添加到购物车”按钮,如何重定向到购物车页面?

Woocommerce/wordpress: Woocommerce/wordpress:

This is my code currently.这是我目前的代码。 At the moment it is not redirecting to the cart page.目前它没有重定向到购物车页面。

    add_filter( 'woocommerce_add_to_cart_redirect', 'woo_in_cart' );
function woo_in_cart($product_id) {
    global $woocommerce;         
    $url = WC()->cart->get_cart_url();
    foreach($woocommerce->cart->get_cart() as $key => $val ) {
        $_product = $val['data'];

        if($product_id == $_product->id ) {
            wp_redirect($url);
        }
    }         
}

this snippet does what you asked for, and it supports ajax calls.此代码段满足您的要求,并且支持 ajax 调用。 some points about with your code:关于您的代码的一些要点:

1- 'woocommerce_add_to_cart_redirect' fires when product is already added to cart. 1- 'woocommerce_add_to_cart_redirect' 在产品已添加到购物车时触发。

2- WC()->cart->get_cart_url() is deprecated 2- WC()->cart->get_cart_url() 已弃用

3- will not redirect if ajax add to cart is enabled 3- 如果启用了 ajax 添加到购物车,则不会重定向

add_filter('woocommerce_add_to_cart_validation' , 'shalior_flag_already_added_products_as_invalid' , 10 , 2);
function shalior_flag_already_added_products_as_invalid ($result , $product_id){
    if (false === $result){
        return $result;
    }

    $url = wc_get_cart_url();
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $cart_item_product_id = $cart_item['product_id'];
        if($product_id === $cart_item['product_id'] ) {
            $result = false;
        }
    }
    if (false === $result){
        add_filter('woocommerce_cart_redirect_after_error' , function (){
            return wc_get_cart_url();
        } , 999 );
    }
    return $result;
}

暂无
暂无

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

相关问题 如果产品已在购物车中,我正在尝试更改单个页面和产品上的“添加到购物车”按钮中的链接和文本 - I am trying to change links & text from the Add to Cart button on a single page and product if the product is already in the basket 如果产品在购物车中,则更改“添加到购物车”按钮文本,并在 WooCommerce 中满足条件时重定向到购物车页面 - Change "add to cart" button text if product is in cart and redirect to cart page when condition is met in WooCommerce WooCommerce:如何在常规页面上使用“添加到购物车”按钮? - WooCommerce: How do I use an Add to Cart button on a normal page? 如何将购物车商品产品重定向到Woocommerce中的特定页面? - How can i redirect cart item product to specific page in Woocommerce? 如果产品已经在 WooCommerce 购物车中,则禁用添加到购物车按钮 - Disable add to cart button if product is already in WooCommerce cart 如果产品已经在购物车中,则禁用Woocommerce添加到购物车按钮 - Disable Woocommerce add to cart button if the product is already in cart Woocommerce 产品页面重定向中断添加到购物车按钮 - Woocommerce product page redirect breaks add to cart button 如何在购物车页面的产品包装中放置商品的缩略图? - How do I place a thumbnail of items in a product package on the cart page? 快速添加到购物车无缘无故重定向到产品页面 - Quick Add to cart redirect to product page for no reason 如何将产品再次添加到购物车或“添加到购物车”按钮不起作用? - How to add the product again to the cart or add to cart button not working?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM