[SOLVED] Products Module - Attributes appear in random order

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
JimboDavies
Forum Members
Forum Members
Posts: 130
Joined: Fri Feb 25, 2011 3:58 pm

[SOLVED] Products Module - Attributes appear in random order

Post by JimboDavies »

In my Products module, I am using attributes to display various price options, and date options which can be selected by the user.

When I upload the dates, I put them all in calendar order, however as soon as I click 'submit' or 'update', somewhere along the line they get jumbled into what appears to be no logical order.

This random order is reflected both where they are displayed in the Cart, and also if I go back into editing the Attribute in the Products Admin module.

Some web research has thrown up this forum thread: http://forum.cmsmadesimple.org/viewtopic.php?t=22539 but having looked in Products.module.php it appears the attributes already have sort by id applied.

I've also had a quick look in action.add_attrib_set.php to see if I could find any clues there, however nothing immediately jumped out at me.

I would be grateful for any pointers on how I can sort the dates correctly.
Last edited by JimboDavies on Thu Feb 16, 2012 8:42 am, edited 1 time in total.
JimboDavies
Forum Members
Forum Members
Posts: 130
Joined: Fri Feb 25, 2011 3:58 pm

Re: Products Module - Attributes appear in random order

Post by JimboDavies »

I upgraded to the latest version of CMS Made Simple (1.10.3) and updated all modules to see if that would help, and I'm still experiencing this problem.

I can see on the forum that a number of people seem to have come across similar anomalies, so there must be a fix somewhere!
User avatar
chrisbt
Dev Team Member
Dev Team Member
Posts: 197
Joined: Sun Sep 05, 2010 6:11 am
Location: Sheffield, UK

Re: Products Module - Attributes appear in random order

Post by chrisbt »

I had this problem and my very basic solution was just to suffix each field name with '1_', '2_', etc then loop through the fields twice. On the inner loop I just used the field that had the suffix that matched the foreach index number. Was all done in smarty with was simple enough.
I was watching this post looking for a more elegant solution... and still am ;) .
Chris
JimboDavies
Forum Members
Forum Members
Posts: 130
Joined: Fri Feb 25, 2011 3:58 pm

Re: Products Module - Attributes appear in random order

Post by JimboDavies »

I'm wondering if I can use jQuery to sort the dropdown box - again not very elegant, but possible.

What is confusing is that there is no logic to it - you can edit other product information in the project manager and not touch the attributes, and they will change order... completely unpredictable!

Like yourself, I'm still hoping there is a more elegant, probably hard coded, solution.
JimboDavies
Forum Members
Forum Members
Posts: 130
Joined: Fri Feb 25, 2011 3:58 pm

Re: Products Module - Attributes appear in random order

Post by JimboDavies »

If I look in CGEcommerceBase, and go to "Supplier Settings", I can find a few more options that look interesting, and possibly relevant.

At the bottom of the page is "Product Attribute Display Template" which sounds like it's configuring how the attributes are retrieved and displayed. Mine currently displays:

Code: Select all

{$attrib_text} {*if !empty($attrib_adjust)*}  {*+*}{*$currency_symbol*}{*$attrib_adjust|number_format:2*} {*/if*}
Does anyone know if this is helpful to getting my attributes sorted?
JimboDavies
Forum Members
Forum Members
Posts: 130
Joined: Fri Feb 25, 2011 3:58 pm

Re: Products Module - Attributes appear in random order

Post by JimboDavies »

This is really frustrating me, as I can't seem to find a way of doing it.

One of my attributes generally only has three options, so it's less of an issue, but the dates are super frustrating. If there's anyone who has ever used the Products module as more of a 'Booking' system, I'd be really interested to hear how they handled dates.
User avatar
chrisbt
Dev Team Member
Dev Team Member
Posts: 197
Joined: Sun Sep 05, 2010 6:11 am
Location: Sheffield, UK

Re: Products Module - Attributes appear in random order

Post by chrisbt »

I've tried this and I think it works:

create at UDT called 'sort_attributes' with the Code:

Code: Select all

$Xattributes = $params['unsortedattributes'];
foreach ($Xattributes as &$Xattrib) {
   sort($Xattrib->attrib_options);
   }
$smarty->assign('sortedattributes',$Xattributes);
Then call this UDT using:
{sort_attributes unsortedattributes=$attributes}

then use a foreach loop of:
{foreach from=$sortedattributes item='attrib'}
instead of:
{foreach from=$attributes item='attrib'}

I hope it helps.
JimboDavies
Forum Members
Forum Members
Posts: 130
Joined: Fri Feb 25, 2011 3:58 pm

Re: Products Module - Attributes appear in random order

Post by JimboDavies »

Chris, that's fantastic, thanks so much! So far, so good!

Hopefully that that will work for chrisbt too.
User avatar
chrisbt
Dev Team Member
Dev Team Member
Posts: 197
Joined: Sun Sep 05, 2010 6:11 am
Location: Sheffield, UK

Re: [SOLVED] Products Module - Attributes appear in random o

Post by chrisbt »

Yup - that's me. A few more weeks using php and it was quite easy to step out of smarty and find a much simpler and more elegant solution.
JimboDavies
Forum Members
Forum Members
Posts: 130
Joined: Fri Feb 25, 2011 3:58 pm

Re: [SOLVED] Products Module - Attributes appear in random o

Post by JimboDavies »

Sorry Chris - too early in the morning and I thought I was talking to two people! I didn't realise it was you that had come up with the solution...!
CMSmonkey
Power Poster
Power Poster
Posts: 290
Joined: Thu Nov 27, 2008 4:58 pm

Re: Products Module - Attributes appear in random order

Post by CMSmonkey »

I'm using CMSMS 1.10.2, Products 2.15.1. I used the following instructions ----
chrisbt wrote:I've tried this and I think it works:

create at UDT called 'sort_attributes' with the Code:

Code: Select all

$Xattributes = $params['unsortedattributes'];
foreach ($Xattributes as &$Xattrib) {
   sort($Xattrib->attrib_options);
   }
$smarty->assign('sortedattributes',$Xattributes);
Then call this UDT using:
{sort_attributes unsortedattributes=$attributes}

then use a foreach loop of:
{foreach from=$sortedattributes item='attrib'}
instead of:
{foreach from=$attributes item='attrib'}

I hope it helps.
Correct me if I am wrong, but I called the UDT from the AddtoCart template and changed out the foreach loop inside that template as noted above. I still cannot get the attributes to reorder themselves according to what the admin UI shows. Did I do something wrong?
User avatar
chrisbt
Dev Team Member
Dev Team Member
Posts: 197
Joined: Sun Sep 05, 2010 6:11 am
Location: Sheffield, UK

Re: [SOLVED] Products Module - Attributes appear in random o

Post by chrisbt »

Has it output them sorted alphabetically?
I actually prefixed each of my attribute names with '1_','2_','3_', etc so they were sorted by that. I then just trimmed off the first 2 characters when I used the attribute names.

The other alternative would be to use the full attributes and sort on the contents of the SKU field.
CMSmonkey
Power Poster
Power Poster
Posts: 290
Joined: Thu Nov 27, 2008 4:58 pm

Re: [SOLVED] Products Module - Attributes appear in random o

Post by CMSmonkey »

chrisbt wrote:Has it output them sorted alphabetically?
I actually prefixed each of my attribute names with '1_','2_','3_', etc so they were sorted by that. I then just trimmed off the first 2 characters when I used the attribute names.

The other alternative would be to use the full attributes and sort on the contents of the SKU field.
I pretty much have only 2 attributes for the Products (No Design Needed [which is listed 1st in the admin panel] & Design Needed); I always want it to default to No Design Needed since there is no extra charge for it. Would your solution work for this? Is there any way to just tell it to always default to attribute id 1?
User avatar
chrisbt
Dev Team Member
Dev Team Member
Posts: 197
Joined: Sun Sep 05, 2010 6:11 am
Location: Sheffield, UK

Re: [SOLVED] Products Module - Attributes appear in random o

Post by chrisbt »

if you've just got the 2 attributes try using rsort instead of sort in the UDT:
rsort($Xattrib->attrib_options);

Chris

... http://www.php.net/manual/en/function.rsort.php
CMSmonkey
Power Poster
Power Poster
Posts: 290
Joined: Thu Nov 27, 2008 4:58 pm

Re: [SOLVED] Products Module - Attributes appear in random o

Post by CMSmonkey »

chrisbt wrote:if you've just got the 2 attributes try using rsort instead of sort in the UDT:
rsort($Xattrib->attrib_options);

Chris

... http://www.php.net/manual/en/function.rsort.php
OK, I threw in the "rsort" function instead and some product attributes show correctly - and some don't. :-\
Post Reply

Return to “Modules/Add-Ons”