简体   繁体   中英

Redirect to checkout - Woocommerce

I have a shop when i have one product. Client is able to shop one product at one time. If he want to buy it one more time, he need to finish the purchase and start over again. That is client brief.

Now i have little function like below to redirect my client after click "add to cart" but it's only working when the cart is empty. When client add already a produkt and then go to homepage for no reason clicking on add to cart does nothing. For me it's logic to click on Cart button but client had specific dream of always redirect user to Checkout page..

This is my current function

function my_custom_add_to_cart_redirect( $url ) {
    $url = WC()->cart->get_checkout_url();
    return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );

I was trying to make a IF loop to check if cart is empty and when cart is full redirect to page checkout, but this doesnt work for me:(

This is my atempt to solve it - with doesnt work at all

    function my_custom_add_to_cart_redirect( $url ) {
        if ( ! WC()->cart->get_cart_contents_count() == 0 ) { 
        $url = WC()->cart->get_checkout_url();
        return $url; 
} else {
}
$url = WC()->cart->get_checkout_url();
        return $url; 
    }
    add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );

That can be simplified.

add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );
function my_custom_add_to_cart_redirect() {
    return wc_get_checkout_url();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM