Products Module - Add Related Products Functionality [SOLVED]

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
bryan

Products Module - Add Related Products Functionality [SOLVED]

Post by bryan »

I'm using CMSMS 1.5.4 and the Products module version 2.3.1 to create an online catalog for a client. I was able to setup a 'Related Products' section on the product detail page using the following method:

Add a Related Products Text Field
This is where you will enter a comma separated list of the product IDs for each related product.
1. Navigate to Content » Product Manager » Field Definitions
2. Click 'Add A Field'
3. Name: relatedproducts, Prompt: Related Products, Type: Text Input

Add a 'Related Products' Section to the Detail Template
Using the 'Sample' detail template, insert the following code at line 40 (just above the attributes):

Code: Select all

{foreach from=$entry->fields key='name' item='field'}
  {if $field->name == 'relatedproducts' && $field->value != '' && $field->value != 0}
    {assign var='relatedproducts' value=","|explode:$field->value}
    <h4>{$field->prompt}</h4>
    {foreach from=$relatedproducts item='relatedproduct'}
      {Products action='details' productid=$relatedproduct detailtemplate='Related-Products'}
    {/foreach}
  {/if}
{/foreach}
Create the 'Related-Products' Detail Template
Create a new detail template
1. Navigate to Content » Product Manager » Detail Templates
2. Click 'New Template'
3. Template Name: Related-Products

Option 1: If you want to show a thumbnail of the related products:

Code: Select all

<div class="related-product">
  {foreach from=$entry->fields key='name' item='field'}
    {if $field->type == 'image' && isset($field->thumbnail) && $field->name == 'image01'}
      <img src="{$entry->file_location}/{$field->thumbnail}" alt="{$field->value}"/>
    {/if}
  {/foreach}
  <div class="related-product-title">
    <a href="{root_url}/index.php?mact=Products,cntnt01,details,0&cntnt01productid={$actionparams.productid}&cntnt01returnid={$page_id}">{$entry->product_name}</a>
  </div>
  {if $entry->price ne '' && $entry->price ne 0}
    <div class="related-product-price">
      {$currency_symbol}{$entry->price}
    </div>
  {/if}
</div>
Option 2: If all you need is a text-only list of related products:

Code: Select all

<li>{module_action_link module='Products' action='details' productid=$entry->id text=$entry->product_name}</li>
NOTE: If you do this, be sure to add a tag in the related products section of your 'Sample' detail template:

Code: Select all

    <h4>{$field->prompt}</h4>
    <ul>
    {foreach from=$relatedproducts item='relatedproduct'}
      {Products action='details' productid=$relatedproduct detailtemplate='Related-Products'}
    {/foreach}
    </ul>
You can now add a comma-separated list of product IDs (e.g. - "5,20,103,68") within each product that will list a set of related products.

Notes:
As of this writing, the hyperlinks inside the Related-Products detail template do not support pretty urls.
My first version of this workflow used an attribute list which was much more cumbersome than this method.
Thanks to tyman00 for all the great suggestions below.
Last edited by bryan on Fri Jun 19, 2009 3:06 pm, edited 1 time in total.
bryan

Products Module - self-link in details template (to add Related Products).

Post by bryan »

I need to self-link to a product detail page with pretty urls when looping through products as such:

Code: Select all

{foreach from=$attribset key='label' item='adjustment'}
  {Products action='details' productid=$label detailtemplate='Related-Products'}
{/foreach}
I don't see that as an option though the product detail template. Any thoughts?
Last edited by bryan on Thu Jun 18, 2009 3:37 pm, edited 1 time in total.
bryan

Re: Products Module - self-link in details template (to add Related Products).

Post by bryan »

Nevermind, I solved it. It's a ghetto caveman solution that doesn't support pretty urls, but it works. I've revised my original post.  :-\
Last edited by bryan on Wed Jun 17, 2009 9:44 pm, edited 1 time in total.
tyman00
Power Poster
Power Poster
Posts: 906
Joined: Tue Oct 24, 2006 5:59 pm

Re: Products Module - Add Related Products Functionality [SOLVED]

Post by tyman00 »

Couldn't you use a Custom Field to do that. Make it a text field. Then you can enter the Product ID's of the related products.

Then in your related products template you could get that product id from the custom field and display the link properly. If you wanted to do multiple related products you could setup multiple custom fields or you could list the ID's in a single field and just separate them with commas. Then in the template you would just explode that value into an array and keep building your links/images off of that array.
If all else fails, use a bigger hammer.
M@rtijn wrote: This is a community. This means that we work together and have the same goal (a beautiful CMS), not that we try to put people down and make their (voluntary) job as difficult as can be.
bryan

Re: Products Module - Add Related Products Functionality [SOLVED]

Post by bryan »

Couldn't you use a Custom Field to do that. Make it a text field. Then you can enter the Product ID's of the related products.

Then in your related products template you could get that product id from the custom field and display the link properly. If you wanted to do multiple related products you could setup multiple custom fields or you could list the ID's in a single field and just separate them with commas. Then in the template you would just explode that value into an array and keep building your links/images off of that array.
Thanks, tyman00. Exploding a textfield into an array is a much simpler method for adding to the related products list. I added a custom text field called 'relatedproducts' and used this instead of the attribute set:

Code: Select all

{foreach from=$entry->fields key='name' item='field'}
  {if $field->name == 'relatedproducts' && $field->value != '' && $field->value != 0}
    {assign var='relatedproducts' value=","|explode:$field->value}
    <h4>Related Products</h4>
    {foreach from=$relatedproducts item='relatedproduct'}
        {Products action='details' productid=$relatedproduct detailtemplate='Related-Products'}
    {/foreach}
  {/if}
{/foreach}
Much easier for the end-user to simply enter comma-separated product ids in a field. Is there a more effective way to get the links/images of related products? The related products are still displayed by looping the 'Related-Products' detail template and the links will not support pretty urls. Despite it's obvious efficiency and ease-of-use over my previous version, I sense that I'm missing the big picture.
Last edited by bryan on Thu Jun 18, 2009 7:53 pm, edited 1 time in total.
tyman00
Power Poster
Power Poster
Posts: 906
Joined: Tue Oct 24, 2006 5:59 pm

Re: Products Module - Add Related Products Functionality [SOLVED]

Post by tyman00 »

This is completely untested and I haven't put much research into it.

Code: Select all


{assign var=newArray value=","|explode:$relatedproducts} 
{foreach from=$newArray item='prodID'}
...
Is each item going to have different related products? I was thinking why don't you use the categories as related items. Then just modify your detail template to display other products from the same category that the current product belongs to.
If all else fails, use a bigger hammer.
M@rtijn wrote: This is a community. This means that we work together and have the same goal (a beautiful CMS), not that we try to put people down and make their (voluntary) job as difficult as can be.
bryan

Re: Products Module - Add Related Products Functionality [SOLVED]

Post by bryan »

Is each item going to have different related products?
Yes, each product can link to multiple products in any other category. I'm using a hierarchical category structure to organize them. I've also setup a custom checkbox field that can set certain products as sub-items so that they don't display in the summary views, only in the related products. The only way I can see it working with pretty urls is through some sort of UDT that patches the functionality into the system.

For now I'm going to leave the template setup with related products at attribute lists since over 100 products are already entered that way. I'm currently hosting the site here if you're interested in seeing how the products display:
http://www.webprosprojects.com/wincomfg/
Last edited by bryan on Thu Jun 18, 2009 8:13 pm, edited 1 time in total.
tyman00
Power Poster
Power Poster
Posts: 906
Joined: Tue Oct 24, 2006 5:59 pm

Re: Products Module - Add Related Products Functionality [SOLVED]

Post by tyman00 »

That is a very nice implementation of Products.

I was looking into your Pretty URL's issue.  I don't see why Pretty URL's couldn't be used. Would you mind posting your Related-Products template. It should be able to generate Pretty URL's.
If all else fails, use a bigger hammer.
M@rtijn wrote: This is a community. This means that we work together and have the same goal (a beautiful CMS), not that we try to put people down and make their (voluntary) job as difficult as can be.
bryan

Re: Products Module - Add Related Products Functionality [SOLVED]

Post by bryan »

Thanks so much  ;D

The Related-Products template is the last code block in my first post. I checked {debug} {get_template_vars} and several {$variable|print_r} tests to reveal the pretty url. The closest thing I got was that while on the host product's page, I found the product_title_used_in_the_pretty_url in {$actionparams.junk}. It parsed nothing to the host product's page when I called to it from within the Related-Products template.
Last edited by bryan on Thu Jun 18, 2009 9:00 pm, edited 1 time in total.
tyman00
Power Poster
Power Poster
Posts: 906
Joined: Tue Oct 24, 2006 5:59 pm

Re: Products Module - Add Related Products Functionality [SOLVED]

Post by tyman00 »

I noticed after I posted that your link was up there. After speaking with the module developer I found that currently there isn't a way to generate a Pretty URL for that. In time that ability might come with some changes to 1.7 and a change to CGSimpleSmarty so you can use something like this:

Code: Select all

{module_action_link module='Products' action='details' productid='{$entry->id}' text='{$entry->name}'}
Currently that does not support pretty urls but it does the same as what you have in that detail template.
If all else fails, use a bigger hammer.
M@rtijn wrote: This is a community. This means that we work together and have the same goal (a beautiful CMS), not that we try to put people down and make their (voluntary) job as difficult as can be.
bryan

Re: Products Module - Add Related Products Functionality [SOLVED]

Post by bryan »

Well, the method works as described. I can't be too disappointed if the only thing that isn't available is pretty urls. I'm always amazed at what this community is able to produce. I'm sure we'll see pretty urls available in the future.

I had to modify the code slightly to make it work with the Products module.

Code: Select all

{module_action_link module='Products' action='details' productid=$entry->id text=$entry->product_name}
Your suggestion is a great way to create a text-only list of related product links. It will still need to to be created in a separate detail template so that it can access the {$entry->product_title} attribute.

I'll revise my first post so that both methods are shown.
Thanks for all your input, tyman00.  :)
tyman00
Power Poster
Power Poster
Posts: 906
Joined: Tue Oct 24, 2006 5:59 pm

Re: Products Module - Add Related Products Functionality [SOLVED]

Post by tyman00 »

No problem. If I know that pretty urls comes available to module_action_link I will try to remember to post back in this thread.

Edit: I will be using that Products installation for inspiration in the future. Well done.
If all else fails, use a bigger hammer.
M@rtijn wrote: This is a community. This means that we work together and have the same goal (a beautiful CMS), not that we try to put people down and make their (voluntary) job as difficult as can be.
bryan

Re: Products Module - Add Related Products Functionality [SOLVED]

Post by bryan »

Thanks again. I'll also post a link to the completed site when it's launched so that users will see a working example.
clj83
Forum Members
Forum Members
Posts: 195
Joined: Tue Jul 07, 2009 4:09 pm

Re: Products Module - Add Related Products Functionality [SOLVED]

Post by clj83 »

Hey,

Is there a way to modify this code so that instead of setting individual related products for each product the related products could instead be all those items from a certain 'collection'? What I mean is that instead of adding a 'RelatedProducts' field definition you add, for example, a 'Collection' field definition with a text input. Then each product that is part of the, for example, 'Rosewood' collection is shown where the current related products are in the detail template?

I know you could do this using the current setup but it would be much quicker if you could just type, 'Rosewood', for example into all the products from that collection. I think you need to change this template:

{foreach from=$entry->fields key='name' item='field'}
  {if $field->name == 'relatedproducts' && $field->value != '' && $field->value != 0}
    {assign var='relatedproducts' value=","|explode:$field->value}
    {$field->prompt}
    {foreach from=$relatedproducts item='relatedproduct'}
      {Products action='details' productid=$relatedproduct detailtemplate='Related-Products'}
    {/foreach}
  {/if}
{/foreach}

So that instead of checking for the values it checks for matching values to the current product. I have had a go at editing the above but I am still quite new to smarty and I cant get it to work. Is it even possible?

Thanks for your help

Chris
Locked

Return to “Modules/Add-Ons”