Linking into a product page with preselected attributes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 years ago
There is a product with an attribute, like color. I would like to have 'href' link from the home page (e.g. nivoslider) or any other external site directly to a product page with some product attributes preselected.
e.g
http://example.com/productone?color=red      or     http://example.com/productone/color/red     would load the product page with the red version preselected.

I understand it is not out-of-the-box feature but is it possible with some added code ?  I would welcome some clues where to look at for the solution.
3 years ago
update:  I figured out a solution with just a few lines added to _ProductAttributes.cshtml  works fine.   in case someone else is interested, I will post it here.
3 years ago
please do!
3 years ago
In the _ProductAttributes.cshtml    (go for the one under your Theme directory if you have this file there)
Add after the first '@foreach {'     (~ line 15)
string preselect = @Context.Request.Query[attribute.Name].ToString();

at line 33 begins a long switch statement beginning with the line '@switch (attribute.AttributeControlType)' it goes thru all  possible types of attributes, my implementation is for the following attribute types:

RadioList, ColorSquares, ImageSquares
for these three add after their  '@foreach (var attributeValue in attribute.Values)  {'
if (!string.IsNullOrEmpty(preselect)) {
    attributeValue.IsPreSelected = (preselect ==  attributeValue.Name);
}

Checkboxes, ReadonlyCheckboxes
after the '@foreach (var attributeValue in attribute.Values) {' add:
if (!string.IsNullOrEmpty(preselect)) {
    attributeValue.IsPreSelected = (preselect.Split(',').Any(x => x == attributeValue.Name));
}

Textbox, MultilineTextbox add to both right after the 'case' lines:
if (!string.IsNullOrEmpty(preselect)) attribute.DefaultValue = preselect;

USAGE:
http(s)://yourstoreurl.com/productname?attributename1=value&attributename2=value...
examples:
http(s)://yourstoreurl.com/cake?message=Happy%20Birthday
http(s)://yourstoreurl.com/tshirt?size=Large&color=Blue

in case of multiple selections ie. checkboxes, the value can be a comma separated list:
http(s)://yourstoreurl.com/productname?extras=abs,ac,heatedseats    (all three will be prechecked)

This feature overrides the Default values for preselection you have any stored with your product data. The attribute names are those you registered the attribute with, these are NOT case-sensitive, but the attribute values are case sensitive.

Caveat. No error/security considerations were made,  the query part of the url gets ignored without my code,  there might be some content validation, like length limiting, or injected code check needed.
3 years ago
Awesome, thanks for sharing.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.