[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.
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 »

Ok try this as the UDT 'sort_attributes'. I was sorting the values not the keys (the attribute names)!

Code: Select all

$Xattributes = $params['unsortedattributes'];  // attributes array 
foreach ($Xattributes as &$Xattrib) {
   if ($params['desc']==true) {   // set parameter desc='1' to sort attributes in reverse
      krsort($Xattrib);
   } else {
      ksort($Xattrib);
   }
}
$smarty->assign('sortedattributes',$Xattributes);
Then call this UDT using either:
{sort_attributes unsortedattributes=$attributes} {* returns $sortedattributes *}

OR:
{sort_attributes unsortedattributes=$attributes desc='1'} {* returns $sortedattributes in descending order *}
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:Ok try this as the UDT 'sort_attributes'. I was sorting the values not the keys (the attribute names)!

Code: Select all

$Xattributes = $params['unsortedattributes'];  // attributes array 
foreach ($Xattributes as &$Xattrib) {
   if ($params['desc']==true) {   // set parameter desc='1' to sort attributes in reverse
      krsort($Xattrib);
   } else {
      ksort($Xattrib);
   }
}
$smarty->assign('sortedattributes',$Xattributes);
Then call this UDT using either:
{sort_attributes unsortedattributes=$attributes} {* returns $sortedattributes *}

OR:
{sort_attributes unsortedattributes=$attributes desc='1'} {* returns $sortedattributes in descending order *}
Sorry for the late response - its been a busy week :)

When I implement the code you suggested (using the descending order one), I get the following error:

Code: Select all

Parse error: syntax error, unexpected T_ENDFOREACH in /myserver/Cart^%%46^46D^46D66F50%%module_db_tpl%3ACart%3Baddtocart_addtocarttemplate.php on line 24
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 »

Sounds like you are missing an end curly bracket '}' for some reason.

Does your template work if you remove the: desc='1'

Or even if you remove the whole: {sort_attributes unsortedattributes=$attributes desc='1'}

If you did a copy & paste check that the single quotes are normal single quotes or just retype them.

Otherwise I am struggling to spot why it doesn't work. Works perfectly for me. Will try it again today on a different site and let you know if I spot anything else.
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 »

Yet another update to the UDT:

Code: Select all

// sorts attributes by 'name' and attribute options by value (ascending or descending if param desc='1')

$Xattributes = $params['unsortedattributes'];  // attributes array

foreach ($Xattributes as &$Xattrib) {
   if ($params['desc']==true) {   // sort attribute options in reverse if parameter desc='1' to 
      arsort($Xattrib->attrib_options);
   } else {   // or sort ascending
      asort($Xattrib->attrib_options);
   }
   $sortedattributes[$Xattrib->name] = $Xattrib;   // add to $sortedattributes indexed by name
}
ksort($sortedattributes);   // sort $sortedattributes acsending on index ('name')

$smarty->assign('sortedattributes',$sortedattributes);
Note: This will now sort the attributes in ascending order and the attribute options ascending/descending (if desc='1' in call).
Note2: This does not sort either the SKU's or the 'control' item.
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 »

Chris,

I'm probably being thick as this is (I suspect!) either simple or impossible - but can your UDT be modified to sort by SKU?

Cheers,

Jimbo
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Location: The Netherlands
Contact:

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

Post by Rolf »

- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
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 »

Rolf - that looks as though it would do what I'm after - although I'm not sure (in the context of my attributes) how to have it sort by SKU.

My current code is:

Code: Select all

{if isset($attribute_count) && $attribute_count gt 0}
{sort_attributes unsortedattributes=$attributes}
  {foreach from=$sortedattributes item='attrib'}
    <p>{$attrib->name}: {$attrib->control}</p>
  {/foreach}
{/if}
If I used your plugin, that would change to:

Code: Select all

{if isset($attribute_count) && $attribute_count gt 0}
  {foreach from=$attributes|sort_array item='attrib'}
    <p>{$attrib->name}: {$attrib->control}</p>
  {/foreach}
{/if}
But I guess that would sort it by the Attribute name? It's the attribute control element that I suppose I'm after sorting, ideally by SKU.

Thanks for your help so far - any more is gratefully received.
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 »

Thinking about this - I've been a bit thick.

These sort modifiers are only sorting the Attributes by name - the bit that's coming up in a random order for me is that Attribute->control bit.
Post Reply

Return to “Modules/Add-Ons”