Adding a Buy Button on single product page

[ad_1]

Hi,

I’m using advanced product fields plugin to add extra product fields to my woocommerce products.

I’ve added a “Buy Now” button alongside the “Add to Cart” button at the single product page by using the following code:


add_action ('woocommerce_after_add_to_cart_button', 'ecommercehints_buy_now', 10, 0);
function ecommercehints_buy_now() {
	global $product;
	if ( ! $product->is_type( 'simple' ) ) return;
	echo '<a href="/cart/?add-to-cart='.$product->get_ID().'" class="button">Buy Now</a>';
};

The problem is, when a user clicks on the “Buy Now” button without filling in the required product fields it redirects to the cart page without adding the product to cart because its required fields weren’t filled.

How can I add a “Buy Now” button alongside the “Add to Cart” button when there are required fields?

The button needs to validate the required fields before redirection to cart.

Thank you.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer