0

i have a WooCommerce store with mostly variable products, i want that the users can add to the cart directly from the shop page without having to go to the single product page. for that im creating a function to make a custom button with the link of the products:

function custom_add_to_cart_redirect() { 
 global $product;
 $product_id = $product->id;
 echo '<a rel="nofollow" href="http://localhost/alanicwordpress/shopping-car/?add-to-cart='.$product_id.'&variation_id='.$variation.'&attribute_pa_size=size" class="button product_type_variable add_to_cart_button"><span class="lnr lnr-cart"></span></a>';
}
 add_filter( 'woocommerce_after_shop_loop_item', 'custom_add_to_cart_redirect' );

but when i click in the button send my to the cart and give a warning that a must select product options because is a variable product. i want to know how to obtain the variation id to add it to the link im building. or is there is a way to put the default variation that have every product an add that to the cart when the user click the button.

R. Garcia
  • 57
  • 9
  • Possibly helpful: http://stackoverflow.com/a/27278035/383847 – helgatheviking Mar 11 '17 at 19:36
  • im kind of new in woocommerce where should i put that code, and im seeing that i still have to specified the product data, like the id and the variation id in the ajax call – R. Garcia Mar 11 '17 at 19:50
  • As a variable product contains at list 2 variations or more, that you get once you have selected the different corresponding attributes values, is not really possible in shop page to get a variation ID as this preliminary selections are maid only in single product pages... – LoicTheAztec Mar 11 '17 at 21:19
  • can i then a least try to sent the product to the cart with the variation that the product have be default? – R. Garcia Mar 13 '17 at 04:17

1 Answers1

-1

Try this:

global $post;
$post_id = $post->ID
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40