How to set selected defaults on the magento product details page

I have a requirement in which I get the product identifier from an external application with the super_attribute parameters of the product, such as color, size. I get it all and I can make the option to add to the cart.

But here my actual requirement is to select the options requested by the client and redirect them to the product details page in magento so that they can still enter additional text for printing here. Therefore, I need to set the required parameters on the details page and redirect them to the product description page, and not add it to the basket. They will enter more detailed information, and then they will do addtocart manually.

How can I set the selected options at boot time.

Please help me.

+8
magento
source share
2 answers

Fortunately, this is already built into most topics. You need to know the identifier of both attributes and values ​​from the Simple product, which is part of the custom product. I saw how it works with custom types, so this could be a limitation.

+12
source share

Add Rough Magento2 example below to pre-select custom parameters by name = value in:

/www/mysite/app/design/frontend/mycompany/mytheme/Magento_Catalog/templates/product/view/options.phtml 

The code below displays the option label and the text value of the selection. And it depends on your topic structure. Example below for Luma.

It expects the following format in URL

 product.html?SelectLabel=OptionValue&SelectLabel=OptionValue 

This does not take into account multilingualism, etc., you can easily adapt it, instead find the identifier of the choice and the identifier of the option, which will be a more accurate replacement

 $(label).parents().eq(1).find('select option:contains('+arr[k]+')').attr('selected', true); 

with (untested)

 $("#"+k+" option[id='"+arr[k]+"']").attr("selected", "selected"); 

 <script> require(['jquery'],function($){ $(document).ready(function(){ function getJsonFromUrl() { var query = location.search.substr(1); var result = {}; query.split("&").forEach(function(part) { var item = part.split("="); result[item[0]] = decodeURIComponent(item[1]); }); return result; } var arr = getJsonFromUrl(); for (var k in arr){ if (arr.hasOwnProperty(k)) { //alert("Key is " + k + ", value is" + arr[k]); var label = $('.product-options-wrapper').find("span:contains('"+k+"')"); $(label).parents().eq(1).find('select option:contains('+arr[k]+')').attr('selected', true); } } }); }); </script> 
+1
source share

All Articles