Skip to content

Instantly share code, notes, and snippets.

@titodevera
Created December 27, 2021 19:30
Show Gist options
  • Save titodevera/b638d50d04f9f19c9a8d5d62ec098554 to your computer and use it in GitHub Desktop.
Save titodevera/b638d50d04f9f19c9a8d5d62ec098554 to your computer and use it in GitHub Desktop.
Display WooCommerce product variations dropdown select on the shop, categories, catalog... pages
<?php
/**
* Display WooCommerce product variations dropdown select on the shop, categories, catalog... pages
* Tested on WordPress 5.8.2 + WooCommerce 5.9.0 + Storefront 3.9.1 + PHP 7.3.11
*
* Alberto de Vera Sevilla <[email protected]>
*/
add_filter( 'woocommerce_loop_add_to_cart_link', function( $html, $product ) {
if ( $product->is_type( 'variable' ) ) {
wp_enqueue_script( 'wc-add-to-cart-variation' );
$get_variations = count( $product->get_children() ) <= apply_filters( 'woocommerce_ajax_variation_threshold', 30, $product );
ob_start();
wc_get_template(
'single-product/add-to-cart/variable.php',
[
'available_variations' => $get_variations ? $product->get_available_variations() : false,
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_default_attributes(),
]
);
$html = ob_get_clean();
// Override "yourtheme/woocommerce/single-product/add-to-cart/variable.php" template for advanced customizations
}
return $html;
}, 10, 2 );
@singham-again-on-hindi2

Product image change automatic after adding this code

@salweb
Copy link

salweb commented May 30, 2023

Exist an update function of this? It Not work now thos. The variation is showed but not is possibile add to cart.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment