Created
December 27, 2021 19:30
-
-
Save titodevera/b638d50d04f9f19c9a8d5d62ec098554 to your computer and use it in GitHub Desktop.
Display WooCommerce product variations dropdown select on the shop, categories, catalog... pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
Product image change automatic after adding this code
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
Agreed, would be nice if it added to cart and stayed on the page.