2

Hi I want whenever someone opens configurable product page with size and color the first child should be preselected

how to achieve this I am using latest Magento 2 version

2 Answers 2

0

please follow the below link,
you need to change this as per your attribute,
it working fine for me

https://meetanshi.com/blog/auto-select-first-child-product-of-magento-2-configurable-product/

1
  • already tried this not working
    – NsVentures
    Jul 2, 2020 at 15:29
0

First, you need to override this file in your custom theme as below:-

app/design/frontend/Vendor/theme/Magento_Swatches/web/js/swatch-renderer.js

Change _RenderControls() function

After the end of this function add the below code:

var swatchLength = $('.swatch-option.color').length; // if color attribute 
  if(swatchLength == 1){
     if($('.swatch-attribute').hasClass("color")){
        $('.swatch-option').first().trigger('click');
      }
  }

And then you need to run below command:-

php bin/magento setup:static-content:deploy -f 

Also, you can get more information here:-

https://magento.stackexchange.com/a/278663/85907 https://magento.stackexchange.com/a/129887/85907

Hope this helps you

Thanks ...

1
  • I am using size attribute as dropdown and this code is not working help
    – NsVentures
    Jul 4, 2020 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.