Dora
Dora

Reputation: 285

How to add WooCommerce variation automatically when press on button?

I have product category called "the-collection" with many products inside. All of that products is variable products with variations inside. I have variation with Size: "Simple Pot" and Finish: "Chalky Interior Matt" inside. I want to add custom button, that will auto add this varialtion into cart. For that i added this function into functions.php file:

add_action('woocommerce_after_add_to_cart_button','cmk_additional_button');
function cmk_additional_button()
{
  echo '<a href="#" button type="submit" class="button alt">Sample 
Pot</button></a>';

}

That add me a button. I also followed this manual how to create varaible link, but variation ID is not exact for all products. I also want to show that button only for "The Collection" product category. How to do that?

Upvotes: 0

Views: 903

Answers (1)

mujuonly
mujuonly

Reputation: 11861

add_action('woocommerce_after_add_to_cart_button','cmk_additional_button');
function cmk_additional_button()
{
  echo '<a href="cart/?add-to-cart=10233&variation_id=10234&attribute_pa_color=red" button type="submit" class="button alt">Sample 
Pot</a>';

}

try the snippet like this

Upvotes: 2

Related Questions