wordpress 如何跳过woocommerce中的“您无法添加另一个到您的购物车”错误并直接跳转到结账?

bsxbgnwa  于 2022-12-26  发布在  WordPress
关注(0)|答案(3)|浏览(220)

Woocommerce所做的是...
当产品单独出售,并且当产品已经存在于购物车中,客户点击添加到购物车,Woocommerce显示错误消息“您不能添加另一个到您的购物车...查看购物车”
而不是上面的流我想要..
当客户点击添加到购物车,如果产品已经存在于购物车中,那么woocommerce应该直接重定向到结帐页面。
我认为这可以通过在Woocommerce插件的class-wc-cart.php中编辑几行代码来实现。
如下所示:

// Force quantity to 1 if sold individually and check for existing item in cart
                if ( $product_data->is_sold_individually() ) {
                    $quantity         = apply_filters( 'woocommerce_add_to_cart_sold_individually_quantity', 1, $quantity, $product_id, $variation_id, $cart_item_data );
                    $in_cart_quantity = $cart_item_key ? $this->cart_contents[ $cart_item_key ]['quantity'] : 0;

                    if ( $in_cart_quantity > 0 ) {
                        throw new Exception( sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', wc_get_cart_url(), __( 'View Cart', 'woocommerce' ), sprintf( __( 'You cannot add another &quot;%s&quot; to your cart.', 'woocommerce' ), $product_data->get_title() ) ) );
                    }
                }
cedebl8k

cedebl8k1#

我自己也在研究如何做到这一点。user1072884的答案是可以的,但是,它一次只适用于一个产品ID。如果你有多个产品,这就不理想了。
这是一个适用于所有产品的简化解决方案。
只需将以下代码添加到子主题functions.php中,并将占位符文本与结帐页面URL交换。

/** If multiple items are in the cart redirect to the checkout page instead of throwing an error **/
add_filter( 'woocommerce_add_to_cart_validation', 'check_cart' );
function check_cart( $cart_item_data ) {
    global $woocommerce;

    if ( $woocommerce->cart->cart_contents_count > 0 ) {
        $direct_url = home_url( 'xyz' ); // Change the value of your actual redirect url.
        wp_redirect( $direct_url );
        exit();
    }
}

旧线程,但也许其他人会发现这有帮助。

lb3vh1jj

lb3vh1jj2#

试试这个,它会工作:

add_filter( 'woocommerce_add_to_cart_sold_individually_quantity', 'vipcomment_change_quantity_to_zero', 10, 5 );
function vipcomment_change_quantity_to_zero( $one, $quantity, $product_id, $variation_id, $cart_item_data ) {
    $your_product_id = 96;
    if ( $product_id == $your_product_id ) {
        $product_cart_id = WC()->cart->generate_cart_id( $product_id );
        $in_cart         = WC()->cart->find_product_in_cart( $product_cart_id );
        if ( $in_cart ) {
            return 0;
        } else {
            return $quantity;
        }
    } else {
        return $quantity;
    }
}

add_filter( 'woocommerce_add_to_cart_sold_individually_found_in_cart', 'vipcomment_is_product_exist_in_cart', 10, 5 );
function vipcomment_is_product_exist_in_cart( $exist, $product_id, $variation_id, $cart_item_data, $cart_id ) {
    $your_product_id = 96;
    if ( $product_id == $your_product_id ) {
        return false;
    } else {
        return $exist;
    }
}
7d7tgy0s

7d7tgy0s3#

最简单的方法是:

throw new Exception( sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', 
  wc_get_cart_url(), __( 'View cart', 'woocommerce' ), 
  header( "Location: https://www.example.com/cart/" ) ) );

而不是错误。请注意:这将把用户重定向到购物车页面(其中产品应该已经就位)。

相关问题