Skip to content
Blog Careers Google Partner 12+ Years · 5000+ Projects
Best Website design company in India January 31, 2023 2 min read

How to show product variation dropdowns in product archive page

You can show product variation dropdowns on the WooCommerce archive page by adding a short piece of code to the functions.php file of your child theme. Here’s how you can do it:

  1. Open your child theme’s functions.php file in a text editor.
  2. Add the following code to the file:

add_action( ‘woocommerce_after_shop_loop_item’, ‘display_variation_dropdowns_in_archive’, 9 );
function display_variation_dropdowns_in_archive() {
global $product;
if ( $product->is_type( ‘variable’ ) ) {
$attributes = $product->get_variation_attributes();
foreach ( $attributes as $attribute_name => $options ) {
echo ‘<select name=”‘ . esc_attr( $attribute_name ) . ‘” id=”‘ . esc_attr( sanitize_title( $attribute_name ) ) . ‘”>’;
foreach ( $options as $option ) {
echo ‘<option value=”‘ . esc_attr( $option ) . ‘”>’ . esc_html( apply_filters( ‘woocommerce_variation_option_name’, $option ) ) . ‘</option>’;
}
echo ‘</select>’;
}
}
}

 

 

  1. Save the functions.php file and upload it to your server.

This code adds a function display_variation_dropdowns_in_archive that displays a dropdown for each variation attribute of a product in the archive page. The function is then added to the woocommerce_after_shop_loop_item action, which is fired after each product in the archive page.

With this code in place, the variation dropdowns should now display on the archive page of your WooCommerce store.

What the product variation dropdowns can and cannot do

The snippet prints one dropdown for each attribute of a variable product, such as size or colour, under the product on the shop page. However, the dropdowns are plain select boxes. They show the choices, but they do not change the price or add the chosen variation to the cart by themselves. For that, the theme needs extra script, or the customer still opens the product page.

Therefore, use this as a starting point. It works well when you only want shoppers to see which options exist before they click.

Checks before you add the code

  • Work in a child theme, because a theme update replaces the parent theme’s files. WordPress explains child themes in its developer handbook.
  • Take a backup first, since an error in functions.php can stop the site.
  • Test on mobile, where a row of dropdowns can crowd the product grid.
  • Check page speed afterwards if your shop page lists many products.

If you want variation selection and add to cart working on the shop page, our WooCommerce development services team can build it, along with website speed optimization services.

See also: Set minimum order amount in woocommerce and Clear cart data before adding new.

Keep Reading

Related Articles

View All Articles