Skip to content

Instantly share code, notes, and snippets.

@titodevera
Created December 27, 2021 19:30
Show Gist options
  • Select an option

  • Save titodevera/b638d50d04f9f19c9a8d5d62ec098554 to your computer and use it in GitHub Desktop.

Select an option

Save titodevera/b638d50d04f9f19c9a8d5d62ec098554 to your computer and use it in GitHub Desktop.

Revisions

  1. titodevera created this gist Dec 27, 2021.
    33 changes: 33 additions & 0 deletions functions.php
    Original 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 );