Created
December 27, 2021 19:30
-
-
Save titodevera/b638d50d04f9f19c9a8d5d62ec098554 to your computer and use it in GitHub Desktop.
Revisions
-
titodevera created this gist
Dec 27, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ <?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 );