Display WooCommerce Product Variations Dropdown on the Shop Page

Remi | September 28, 2016 | in WooCommerce

Riaan, my fellow colleague at Automattic asked me if I had a snippet to display the variations dropdown select on the shop page for variable products so that customers can shop variable product directly from an archive page without having to visit the product page… and I didn’t have such a snippet. So I built it! (that was a long sentence right?)

The idea here is pretty simple, when you visit the shop page, or an archive page, the code checks the product type and if it’s equal to variable, the “Select Options” button is replaced by the regular add to cart button for variable products. In other words, it displays the dropdown selects for the product attributes and the add to cart button on the shop page for all variable products.

woocommerce-dropdowns-selects-on-archive-page

If you want to use this snippet, simply copy and place it in the functions.php file in your theme folder!

The author

About the author Remi: Hey! My name is Rémi Corson, i'm a WordPress Expert Developer, i create high level themes and plugins, and i provide free stuff every week on this website!

18 Comments

  1. mgason

    Hi,
    I added the code. I am using Storefront theme from woocommerce. I get a selector and quantity field etc but none of my selectors actually contain the variations. Any ideas?

  2. javied

    Hello Remi,

    I tried your code.. but its showing me blank page.

    I have a question,
    Is it possible to show the varitions dropdown based on product id .If yes how can I achieve that.

    Thanks

  3. javied

    Hello Remi,

    I have created a custom shortcode to show products based on id with an image, description,price and add to cart url which send the product directly to cart. Now I have created a varibale product, and added its id in my shortcode. Would you gudie me how can i show the variation options dropdown for that product and add to cart url which can send this product direcly to cart with all the variations seclected.
    However I tried your code. And it breaks my website, simply a blank page is coming below header.

    This is the my custom shortcode:-
    function ayu_magazine_subscription_woo_products_shortcode ( $atts ) {
    global $post; $thePostID = $post->ID;

    $atts[ ‘ids’ ] = explode( “,”, $atts[ ‘ids’ ] );
    extract ( shortcode_atts (array (
    ‘ids’ => $atts[ ‘ids’ ]
    ), $atts ) );
    ///////$atts[ ‘parent’ ] = explode( “,” $atts[ ‘parent’ ] );
    ///////extract( $atts );

    $output.=”;
    $output.=’

    ‘;

    foreach($atts[‘ids’] as $productId)

    {
    // print_r($productId);
    $product = wc_get_product( $productId );

    if($productId==”5207″)
    {
    $title_head=”Digital”;
    }elseif($productId==”5208″)
    {
    $title_head=”Print”;
    } elseif($productId==”5206″)
    {
    $title_head=”Combo”;
    }
    else{
    $title_head=$product->get_title();
    }

    $output.=”;
    $output.=”.$title_head.”;
    $output.=”;
    /*$output.=’

    ‘. $product->get_image($size = “full”).’
    ‘;*/
    $output.=’‘. $product->get_image($size = “full”).’‘;
    $post_content = get_post($productId);
    $content = $post_content->post_content;
    $output.=”.apply_filters(‘the_content’,$content).”;
    $output.=”;
    $output.=”.$product->get_price_html().”;
    $output.=”;

    $output.=”;
    $output.=”;

    $output.= ‘Subscribe‘;
    $output.=”;

    $output.=”;

    }

    $output.=”;

    return $output;

    }
    add_shortcode(‘magazine_subscription_products’, ‘ayu_magazine_subscription_woo_products_shortcode’);

    Guide me how can i show variations options also through this code.

    Thanks

  4. eni

    Hello Remi! Thanks for your hard work.I used your code and the result is that i get a drop-down phrase “choose” only and on the left a small text “brand” but i have weight as variable,the button add to cart or select options is missing also.Any idea would be helpful.Below is a pic of my site.Thank you!

    https://postimg.org/image/a48x8saaf/

  5. jeroen365

    He Remi! Thank you for this plugin. I have a small question though. I’ve added the php and i get the following error in my archive page

    Warning: in_array() expects parameter 2 to be array, object given in /home/p5343/domains/onshus.nl/public_html/wp-content/plugins/woocommerce/includes/wc-template-functions.php on line 2213

    On line 2213 the product therms get read, but apperantly as objects instead of arrays. Would you know what is up? Would be of great help thanks!!

    1. jeroen365

      He Remi! Thank you for this plugin. I have a small question though. I’ve added the php and i get the following error in my archive page

      Warning: in_array() expects parameter 2 to be array, object given in /wp-content/plugins/woocommerce/includes/wc-template-functions.php on line 2213

      On line 2213 the product therms get read, but apperantly as objects instead of arrays. Would you know what is up? Would be of great help thanks!!

  6. IamAnujSharma

    Please help me,
    Is it possible to get price tag in a div on selecting value from the variation dropdown on product page? Like, What happens in single product page.
    Thank You.

  7. riverbank

    Remi,
    Latest Version of WP and AVADA THEME. Site is not yet live but still in development. I want to display all variations just like simple products so found your coding above.

    I embedded your coding above (Display variations dropdowns on shop page for variable products). I placed that code inside my child theme functions.php file.

    Now each of my hundreds of product variations are attempting to load, but the resulting error shows as follows:

    Warning: in_array() expects parameter 2 to be array, object given in /home/southsid/public_html/wp-content/plugins/woocommerce/includes/wc-template-functions.php on line 2247

    Next, I went to the applicable wc-template-functions.php file and here is line 2247 shown below:

    if ( in_array( $term->slug, $options ) ) {
    $html .= ‘slug ) . ‘” ‘ . selected( sanitize_title( $args[‘selected’] ), $term->slug, false ) . ‘>’ . esc_html( apply_filters( ‘woocommerce_variation_option_name’, $term->name ) ) . ”;
    }
    }

    Can you determine how to proceed to get your variations displaying?

    1. riverbank

      By the way, the Warning shown above is replicated hundreds of times across three columns, one code block in the place for each variation hundreds of times down the page.

Leave a reply