Custom Link Directly to Woocommerce Checkout

This week I was working on an feature in a WordPress for those who using Woocomerce to make the store.

the owners want to add a link in every product page to lead the users directly yo checkout without passing on “Add To Cart” functionality.

Woocomerce plugin take the ID of each product and adding it to the last of the link like below when you want to checkout:

www.yoursite.com/checkout/?add-to-cart=PRDOUCT_ID

Real Example: www.yoursite.com/checkout/?add-to-cart=1000 , so here we will order the product number 1000

I make a small search and i found that there a built in function in Woocommerce give us the power to add a text above or below the “Add to Cart button” using functions.php file without touch the source code of the plugin, so I use that to add a link instead.

So go to your functions.php in your theme and add this function:

add_action( 'woocommerce_after_add_to_cart_button', 'link_to_command_directly' );
function link_to_command_directly() {

  echo "<a href='#' class='directly-command'>Command</a>";
  
}

Now you will be able to see that “Command” Text is added to your product single page, but its not working.. let’s make it work.

add_action( 'woocommerce_after_add_to_cart_button', 'link_to_command_directly' );
function link_to_command_directly() {
    
  global $product;
  $id = $product->get_id();
  $generated_link_with_product_id = 'https:www.yoursite/checkout/?add-to-cart=' . $id; 
  echo "<a href='".$generated_link_with_product_id."' class='directly-command'>Command</a>";
  
}

Let’s comment the above code; we declare the global variable $product to give use the “$product” object so we are in all product now.

After that we get the ID of each product.. so I create a variable to stock in the site URL concatenated with the URL

And here we are.. ENJOY

See you, and hope it helps.

Leave a Reply

Your email address will not be published. Required fields are marked *