简体   繁体   中英

How to get variation id in shop page WooCommerce

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.

Try this:

global $post;
$post_id = $post->ID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM