Page 2 of 2

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

Posted: Mon Mar 12, 2012 9:27 am
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 *}

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

Posted: Sun Mar 18, 2012 5:33 pm
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

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

Posted: Mon Mar 19, 2012 8:22 am
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.

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

Posted: Mon Mar 19, 2012 3:57 pm
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.

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

Posted: Wed Feb 20, 2013 10:42 am
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

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

Posted: Wed Feb 20, 2013 12:44 pm
by Rolf

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

Posted: Wed Feb 20, 2013 1:04 pm
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.

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

Posted: Fri Feb 22, 2013 9:48 am
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.