Search code examples
magentomagento-1.9configurable-product

Magento Configurable Product Overwrite Defaults by URL


I was browsing through js/varien/configurable.js and noticed a comment that said, // Overwrite defaults by url. Does mean there is a way to pre-select the drop down values by altering the url?

If so, can you please show me an example of how this is accomplished (example: color)? Perhaps http://www.example.com/test/product.html#color=blue? What are the options for the url to modify the selections? Associated sku? Attribute and option labels? Attribute and option IDs?

    // Overwrite defaults by url
    var separatorIndex = window.location.href.indexOf('#');
    if (separatorIndex != -1) {
        var paramsStr = window.location.href.substr(separatorIndex+1);
        var urlValues = paramsStr.toQueryParams();
        if (!this.values) {
            this.values = {};
        }
        for (var i in urlValues) {
            this.values[i] = urlValues[i];
        }
    }

    // Overwrite defaults by inputs values if needed
    if (config.inputsInitialized) {
        this.values = {};
        this.settings.each(function(element) {
            if (element.value) {
                var attributeId = element.id.replace(/[a-z]*/, '');
                this.values[attributeId] = element.value;
            }
        }.bind(this));
    }

Thank you in advance!


Solution

  • So it seems that you can pre-select product attribute options using the url, however, it is not a very user-friendly way of doing so. The full url must be followed by #attribute_id=option_id. You'll want to have access to the database to get the appropriate ids unless you have plans of using native Magento functions to implement this.

    Example

    http://www.example.com/test/product.html#107=54&33=82

    When you load this url, Magento will pre-select those values from the dropdown menus. Believe me, I would rather it be something like this: #attribute_code=option_code (#color=dark_blue), although I am almost certain that there are only ids and labels for options of a drop down.

    If you're looking for a way to make this more user-friendly, perhaps try adding url rewrites to accomplish this. Example: http://www.example.com/test/product.html#107=54&33=82 to http://www.example.com/test/dark-blue-product.html