woocommerece Add to cart button and buy now button on custom product page

I’m trying to set “Add-to-cart” and “Buy Now” buttons in my product page:

  • Add-to-cart button: Simply add product in cart
  • Buy Now button:Add-to-cart and redirect to checkout page

I’ve tried this solution found on blogs:

Read More
 function dj_redirect_checkout( $url ) {
        global $woocommerce;
        $checkout_url = $woocommerce->cart->get_checkout_url();
        return $checkout_url;
    }
add_filter( 'add_to_cart_redirect', 'dj_redirect_checkout' );

After adding the above code, add-to-cart also redirects link to checkout page. How to accomplish this for both buttons in the product page?

Related posts

Leave a Reply

2 comments

  1. Please try the following code:

    function woo_redirect_to_checkout() {
     global $woocommerce;
     if($_POST['byenowpro'] == 'byenow'){
    $checkout_url = $woocommerce->cart->get_checkout_url();
        return $checkout_url;
      }
    }
    add_filter ('add_to_cart_redirect', 'woo_redirect_to_checkout');
    

    HTML example:

    <input type="hidden" id="byenowpro" name="byenowpro" value="" />
    <input type="submit" class="single_add_to_cart_button button secondary" id="byenow" name="byenow" value="Bye Now" onclick="byenowbutton" />