开发者

How to set default selected options in magento product detail page

I have requirement in which i am getting product ID from external Application with product super_attribute options like color,size. I am getting all those and i can do the add to cart option.

But here my actual requirement is to select the requested options by customer and redirect them to product detail page i开发者_如何学运维n magento so that here they can still enter optional text to print. So i need to set the requested options in detail page and redirect them to product detail page instead of adding it to cart. They will enter more details and then they will do addtocart manually.

How can i set the selected options while loading itself.

Please help me.


Thankfully this is already built in to most themes. You need to know the ID of both attributes and values from the Simple product that is included in the Configurable product. I've only seen it work with Configurable types so that might be a limitation.

  • For each attribute make a key=value pair
  • Combine the attributes as a query string, eg. "123=345&678=890"
  • Append the string as a hash fragment (not as a query).
    This is an example from the official demo, note that the first option needs to be selected for the second to work so it has two key/value pairs.

    http://demo.magentocommerce.com/zolof-the-rock-and-roll-destroyer-lol-cat-t-shirt-126.html#525=99&272=22


Rough Magento2 example add the below for pre-selecting the custom options by name=value in:

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

The below code looks at the label of the option and the text value of the select. And depends on your theme structure. Example below for Luma.

It expects the following format in the url

product.html?SelectLabel=OptionValue&SelectLabel=OptionValue

This does not account for multi language etc.. you could easily adapt it to instead look for the select id and option id which would be more accurate replacing

$(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>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜