![]() |
Auto-Generated Color or Image Swatches from Product Options
This mod was inspired by sstillwell's post: Variant chart in product description
I adapted the code to display all of the variants on a page in a chart as the previous mod detailed, but also to use it to create color (or image) swatches for the items that were product options. A great necessity for anyone who sells items of color, like t-shirts or apparel. Best of all, it can be applied to any of the "tabs" mods to have a dropdown color chart or drawn directly into the product.tpl as I have detailed below. This is a finished mod, but.... I need some help making it loop in two columns as some of my items have 30 color variants and one column is way too long... if you use this mod, please assist me in the correct Smarty syntax of the loop and modifying it to display in two-columns. I used the Product Variants to call an equivalent name in a CSS file so that some swatches could be made from either html hex codes or others (like a patterned or designed apparel item) could use images. You could call anything you wanted really, but automating color /image swatches is key for many stores and a very useful function- on the fly color/swatch charts! In the product.tpl, add an include: Code:
<P align="left"> I put it under the product description code. Then your product options must be variant type (not modifiers). If they are, then this will work. I am working the code to allow for modifiers as well and you could script it to also use the html_link command to change the product image (another mod). If you have any ideas or can make improvements, please do so. You can see in the include, that I created a directory (Color_Charts) in the skin1/modules directory and then created the template "Color_Swatches.tpl" in there. You can change this and put it wherever you would like. But, this works to call the "Color_Swatches.tpl". In the Color_Swatches.tpl, I put: Code:
{if $variants} I created the language variable: $lng.lbl_available_colors which just states "Available Colors" and set that as an H2, but you can call it and change the CSS accordingly. Also note: Code:
<td width="16" height="16" class="{$item.option_name|replace:" ":""|replace:"/":""}">[img]{$ImagesDir}/resources/dot_clear.gif[/img]</td> Also in that code is class="{$item.option_name|replace:" ":""|replace:"/":""}" This is the call to a CSS class based on the "Product Option" name. But, many colors have multiple names, like Baby Blue and Light Pink... so I included the Smarty code that replaces a space with nothing. Thus, "Baby Blue" is now a CSS class called "BabyBlue" and also "Black/White" is replaced with BlackWhite. You can add other variable replacements so that you can correctly call a CSS style. This is key as being CSS means that you can use colors, images, anything! I also created a custom CSS file called "colors.css" and put the include in the home.tpl underneath the main skin1.css: <LINK rel="stylesheet" href="{$SkinDir}/colors.css"> In this CSS file I listed my colors that could be called. If you have multiple color variations per name (two or three "Red" variants) just use an {if} statement to call the correct CSS file per manufacturer, like: Code:
{if $product.manufacturerid eq "21"} I recommend bookmarking that one as it got me started playng with all of these minor but nifty tweaks... In the colors.css, I just listed the CSS names and color with hex codes. Like: Code:
.Pink { In theory, if all of my swatches would have been images, I could have just ignored the CSS and called the images as background images in the swatch array drawn from the product options. OK- that about sums it up. But, I want to add some additionals to this mod and perhaps you can help.
Enjoy! And I'd love to see some samples of this in use. One nice thing about it is that a customer only see the colors that are available. If you remove or deactivate an option, no corresponding swatch appears. And being Smarty, you can call it or not with any of the many variables... - Mike |
here is a 2 column layout for a custom detailed images mod.
you will have to make some tweaks, but you get the idea. Code:
{if $images ne ""} you can see it in action here. http://ridersinc.com/store/customer/product.php?productid=4357&cat=1083&page=1 the detailed images section below the price and large thumb. |
Thanks ETI! I'll see if I can get that to work right now... By the way, you could tweak that detailed mod of yours to swap the product image. But, how are those thumbs called? It appears to be an "extra" detailed image php call...
- Mike |
Re: Auto-Generated Color or Image Swatches from Product Opti
Hi I've used you mod and it works well thanks.
But..... there's always a but :D I want my customer to be able to disable some of the variants if a color becomes unavailable. If you do so, color_swatches.tpl still do the loop as if all the variants were still available. The result is that I get blank space with an X at the place of the picture. Have you figure out how to skip th variant if its disable? |
Re: Auto-Generated Color or Image Swatches from Product Opti
Hello I've combined your mod with ETI's and have color swatch on 5 colums and i'm using images instead of css.
Feel free to use. Code:
{if $variants} However I still got this issue. :( Quote:
|
tabarsoft,
My mod works for "available" options only, so I don't get any empties when options are unavailable (it only calls what is seen in the dropdown). Getting better and better though! :-) - Mike |
Solution for achieving multiple columns of swatches:
Code:
{if $variants} - Mike |
No problem.
Glad it worked out. As for the image swap on the ridersinc site. All those watermarked images are dynamically generated, your suggestion would require another thumbnail to be generated and some more coding...wasnt required by the client.... but it could be done. |
I was asked about using images for this mod, simply change the CSS to reflect an image URL:
Code:
} Or if you have some images that you want to display per option, simply remove the border and call them. I did this in CSS as products are very different across manufacturers or lines. Better to keep control of the function in an easy to trasnport form.. - Mike |
I am trying to change my product image by clicking the generated swatch (the images change by changing the variant option dropdown), but I can only get the swatch to change the dropdown and the dropdown to change the image.
In the img src tag of the swatch, I have added: Code:
onClick="javascript:document.getElementById('po'+{$item.classid}).value={$item.optionid} Thanks for your help, Mike |
Switch the product image on click of color swatch
Just worked this to take advantage of ianwebster's brilliant "Product Options and Image previews" mod. Now, the swatches change the product image and it looks and work great. But, first implement his mod in both user and admin. Then you can manually add or insert the table variantid's so that both the variant dropdown AND the color swatches can change the product image. Here's the main section: Code:
{assign var="image_counter" value=0} Next, I'll work the names to do the swap onclick as well... getting there. - Mike |
Quote:
Hello Mike, Have you tried to disable some? Because I've tried your code has is, and you do get holes for the missing disabled options. See images below. Mine is above and yours is below. Colors don't apears for the avaible options because I didn't create the css for it but we can still see the names of the options. http://www.tmdesign.ca/color/swatch.jpg disabled in redhttp://www.tmdesign.ca/color/swatch003.jpghttp://www.tmdesign.ca/color/swatch2.jpg It must be the way we're building the table. We're close to the solution. Be back soon. Martin |
Solution to use images without CSS !
Solution to use images without CSS !
Hello Mike and ETI Good news I've got it! If you want to use images without css you can use this code. Any number of column, options disabled or not, and the table containing the swatch is now close correctly. Code:
{if $variants} It might not be the cleanest code but it works. Feel free to improve it. Martin |
Matrin,
Yes, I see what you mean. But, though you meant disabled options. If you disable an option it doesn't show in the dropdown nor in the swatch array. But, if you hav an option with no CSS (image or hex code in CSS) then the TD is empty. This is normal behaviour as we need swatched to display for all available options that are variants. Thus, works well. My current issue, is to get the name to make the product image change when clicked... like the swatch. Thanks for your contribution to this work in progress. - Mike |
Could some of you post links to your sites where you're using this so we can see?
Thanks! |
Hi Danielle,
Unfortunately the site isn't available to public yet. But for now here's a picture. http://tmdesign.ca/color/swatch006.jpg The swatch shows the 11 curently available colors out of the 15. I wrote a modifier to split the value of each options to call the picture and put a description underneat. |
OK, so this will look the same as if I had an options dropdown box and then detailed images, except that the images will actually be associated with the options in the box, and will be labelled with the option name as well, is that right?
|
Quote:
Close, :wink: I still have detailed images associated to the product. But for the rest of it you right. |
Oh OK, so I could still have detailed images below the swatches then if I wanted?
|
Exactly.
:D |
Yes, the swatches don't really have anything to do with the current functions of a product, other than product options. I used ianwebster's solution to then apply an image swap with the click of the auto-generated swatches and also change in the dropdown.
Keep in mind- this is currently for "variant" options and not modifier. If using size and color variants, I have kept the color as a variant and the size as a modifier. It doesn't enable a SKU per size/color combo as if both were variants, but it wasn't needed and most will be satisfied with usage. Still a work in progress. - Mike |
Switch the product image on click of color swatch - files?
Hi Mike,
I have got Ian Websters "Images changing with variants" working well and wanted to also add colour swatches (that also change the images and colours if clicked) - hence really interested in your post. Just couple of things not 100% clear on... I was wanting to use the code you posted (pls see below) - is this code you listed just an addition to the variants.tpl or do I need to apply other code as well (e.g. to the product.tpl or product.php etc.)? If you can let me know a little more would be really appreciated. Thanks for your help and best regards - Andrew PS: I also have a color table with variants listed but not used (any changes to this code needed or does it work around this?) Thanks again!!! Quote:
|
My first post discusses which files need to be added or modified. It isn't completely perfect but works for available variants. Keep in mind that X-Cart has implemented this into 4.1 versions of the cart and you may find a better workaround in those files and/or wait until it is stable.
Thanks, Mike |
Does anyone have this working on a live site? I would like to see it in action. I currently have over 3000 products because I had to set up each color of each clothing item as a separate product. It would optimize and reduce my store product count to just over 400 if I could incorporate different images for each color option for a single item, rather than having a separate product for each color. I see several changes to the code in this post. If someone could post the final working version and instructions on installing it I would greatly appreciate it. I would also like to know if this has been successfully combined with Telefirma's image MOD yet. A live site to preview would be appreciated. Thanks.
|
Any idea to change so All options can be variants & work
Hi Mike,
I got this module working and looks good (thanks)... 1) Change so mod works for multiple languages Maybe this is helpful to some so have put this here - my question is actually below as point 2 however. I just did one change which is use the "order_by" varible instead of the "option_name" varible - the advantage of this is that it will work when one uses multiple languages (as obviously for example pink (english) changes to rose (french) for example meaning linking on "option_name" would result in a crash for the 2nd and so on languages. I have looked at the SQL table and this works as long as one names their swatch references (CSV reference or photo name) using these order_by numbers. For example photos as X.jpg - where X is the "order_by" number (e.g. 1.jpg) For others (know you know mike) the order_by numbers for an option can be seen in the product options of Admin and if like changed by using replace in a CSV file and then imported - if some wants to). Quote:
2) Question I have - how to change code to allow for 2 options which are both variants? Question I have is... have you managed to make this mod work with two options both being variants and not with one being a modifier and one a variant? Reason I ask this, is as stock levels can not be attached to modifiers - so obviously it is useful to have two or several options all being variants (and having just the one lot of colour swatches showing). As you know - at the moment making two options both as variants results in a multi-count of the swatches (namely results in double or triple copies of a color swatch being created / appearing). Any ideas on this Mike - anything at all is very much appreciated. Thanks and best regards - Andrew |
No, I haven't been able to modify stillwell's post regarding the issue of "multiple variants" and duplicates. But, I am sure there is a way. Although I liked working this very obscure solution of a mod (probably too obscure for most), it did work.
As this basic functionality is coming in 4.1, I am sure we will see a better solution than a CSS-very-strange-hack that I have here. But, glad a couple folks got some use out of it. ;-) - Mike |
I was wondering about using your colour swatch mod for my site. At the moment i hardcode a swatch image in the description is obviously a pain in the x-cart.
But one question i needed answering was that at the moment i have my home set up so that depending on which category you enter changes whichever css file it uses (to change the background colours). what Im wanting to know is can i alter that so that it uses say css1+coloursswatchcss or css2 + colourswatchcss or css3 + colourswatchcss. basically what im asking is that you can use two separate css files? |
I was positive that this functiontionality was available in 4.1 but now that I'm looking for it, I can't figure out where to set it up. Anyone?
thanks, Glen |
I mistook the variant images in 4.1 as able to have swatches, but that doesn't seem to be the case. This still remains a mod that X-Cart will sell, but hasn't included in the funstionality of the cart.
- Mike |
Sorry Mike but I still need to know is it ok to have two css files selected in the home.tpl one for the general colours and then one just for colour swatches??
at the moment i have an if statement there already so that it changes css files depending on which category you are in. Just need to make sure its ok to have two css files to use? |
Yes, you can have multiple CSS files. Though most people may get away with the other method in this thread and just use images.
Here's an example in my product.tpl: I show a manufactures logo. But, as I do not want to call the logo from the MySQL table and php, I use a Smarty variable replacement. Code:
[img]{$ImagesDir}/brands/{$product.manufacturer|replace:' ':''}.gif[/img] If I put an image in the skin1/images dir and in a folder called "brands", I could then call the images with a Smarty variable. But, some brands (and colors) would have multiple words. So I tweak the variable to replace "spaces" with no space. That's the |replace:' ':'' part of the code. Using the other solution in this thread, you could then also use the "Product Option" itself to show an image with it's name. Easier than CSS if you only have a few brands or charts to display, but CSS works well if you have a lot of brands with a lot of color variations. Say I have 130 brands with 50 diff. color charts. It might only work to use the CSS technique and if statement method as you have done. That way, you only get the core CSS skin1 file AND the one manufacturers CSS file for the color calls... a lot less code than calling all CSS files into the template structure. And as I mentioned a decent entry-level alternative vs. php and MySQL work which would be necessary if you actually wanted to assign a swatch image to the variant table. X-Cart did this for a site I know, but it was a bit complex and a few hundred dollar job. I'm sure they have the code for many specific needs. - Mike |
Do you know how i can change the coding so that it looks at a specific variant ie colour/swatch variant rather than sizes?
we dont have that many colours usually black, white or hivis. I just want it to bring in the colour swatch.tpl if there is a colour variant available and not have it bring in blank swatches for the sizes? |
I have started trying to get this to work for 4.1.x and have managed to display one column of variants (colors only). Can anyone help me to: 1. Allow for 2 or 3 columns and 2. Display only available color swatches/names?
Code:
{capture name=dialog} My mod has a directory in the images dir. called "S" and the images are named "Brand_ColorTitle.gif". Thank for helping me to develop this intermediary solution. - Mike |
Re: Auto-Generated Color or Image Swatches from Product Options
Hi Mike,
Thanks a ton for sharing all your time on this. I've just read this whole thread and wanted to touch base with you to see what your current take on the best solution to this is... any advice would be greatly appreciated as I'm about to jump in to trying to get this mod to work. Thanks! Damian |
Re: Auto-Generated Color or Image Swatches from Product Options
Has any figured out how to put thumbnail images and place them under the product image for 4.1 verision as http://www.ridersinc.com/alpinestars-boots-supertech-r-2008-p100221.html is using it in addition to the dropdown that is comes with the cart?
I see how the drop down works by swamping images but I would also like to have thumbnails under the product image. Most people will not know to use the dropdown to see other images. |
All times are GMT -8. The time now is 03:40 AM. |
Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.