Jump to content

Attributes Generator - sort function


m-graphix

Recommended Posts

OK, I've been fooling around with this for 2 days already and I can't figure it out, so any expert help is greatly appreciated, willing to even pay for it...

 

 

I have a prestashop selling a lot of fishing products, thus we have a lot of color variations for each product. All colors are uploaded to the database in this format:

ID COLOR NAME

1 FIRST PRODUCT ------

2 Red

3 White

4 Blue

5 SECOND PRODUCT ----

6 Red

7 White

8 Green

 

 

But the popup list in Attributes Generator page is showing me the available colors sorted by Color Name not by Color ID, like this:

Blue

FIRST PRODUCT ------

Green

Red

Red

SECOND PRODUCT ----

White

White

post-343063-0-45814700-1332856969_thumb.jpg

 

I located the piece of code below in AdminAttributeGenerator.php file, but I have no clue how to sort the list by ID not by NAME, to look like in my database (blue text above).

PLEASE HELP ME GUYS, I'm desperate!!! THANK YOU

 

private function displayGroupSelect($attributeJs, $attributesGroups)

{

echo ' <div>

<select multiple name="attributes[]" id="attribute_group" style="width: 250px; height: 700px; margin-bottom: 10px;">';

 

foreach ($attributesGroups AS $k => $attributeGroup)

{

$idGroup = (int)$attributeGroup['id_attribute_group'];

if (isset($attributeJs[$idGroup]))

{

echo ' <optgroup name="'.$idGroup.'" id="'.$idGroup.'" label="'.htmlspecialchars(stripslashes($attributeGroup['name'])).'">';

foreach ($attributeJs[$idGroup] AS $k => $v)

echo ' <option name="'.$k.'" id="attr_'.$k.'" value="'.htmlspecialchars($v, ENT_QUOTES).'" title="'.htmlspecialchars($v, ENT_QUOTES).'"">'.$v.'</option>';

echo ' </optgroup>';

 

 

}

}

echo ' </select>

</div>';

}

Link to comment
Share on other sites

The file responsible is the AttributeGroup class file

 

Override this file - create a new PHP file in /override/classes names AttributeGroup.php

 

Paste the following code into the file.

 

 

 

<?php
/**
* <InsertFileDescription>
*
* @date: 28/03/2012
*
*/
class AttributeGroup extends AttributeGroupCore
{

/**
* Get all attributes for a given language / group
*
* @param integer $id_lang Language id
* @param boolean $id_attribute_group Attribute group id
* @return array Attributes
*/
public static function getAttributes($id_lang, $id_attribute_group)
{
return Db::getInstance()->ExecuteS('
SELECT *
FROM `' . _DB_PREFIX_ . 'attribute` a
LEFT JOIN `' . _DB_PREFIX_ . 'attribute_lang` al ON (a.`id_attribute` = al.`id_attribute` AND al.`id_lang` = ' . (int)($id_lang) . ')
WHERE a.`id_attribute_group` = ' . (int)($id_attribute_group) . '
ORDER BY a.`id_attribute`');
}

}

?>

 

 

If that works, great if not, delete the file and report back.

Link to comment
Share on other sites

Thanks for the reply, damonsizzle.

I tried that, but the list looks exactly the same, nothing changed...

 

If you can give me another solution, I'd appreciate a lot. I'm not an ace in PHP, I can follow and partially understand the code, but can't do anything from scratch.

Link to comment
Share on other sites

Try putting die("works"); in the function to see if the method is being called. If you see works in your browser it is calling the function.

 

So it looks lik e

 

 

public static function getAttributes($id_lang, $id_attribute_group)

{

die("works");

return Db::getInstance()->ExecuteS('

SELECT *

FROM `' . _DB_PREFIX_ . 'attribute` a

LEFT JOIN `' . _DB_PREFIX_ . 'attribute_lang` al ON (a.`id_attribute` = al.`id_attribute` AND al.`id_lang` = ' . (int)($id_lang) . ')

WHERE a.`id_attribute_group` = ' . (int)($id_attribute_group) . '

ORDER BY a.`id_attribute`');

}

 

If it shows, then we know the code is being called but isn't right. If it doesn't show then the code never executed. Let me know the result.

Link to comment
Share on other sites

My mistake, it appears its the Attribute Class

 

Try the following

 

New file in /override/classes named Attribute.php

 

 

 

<?php
/**
 * <InsertFileDescription>
 * @date: 28/03/2012
 *
 */
class Attribute extends AttributeCore
{
 /**
  * Get all attributes for a given language
  *
  * @param integer $id_lang Language id
  * @param boolean $notNull Get only not null fields if true
  * @return array Attributes
  */
 public static function getAttributes($id_lang, $notNull = false)
 {
  return Db::getInstance()->ExecuteS('
 SELECT ag.*, agl.*, a.`id_attribute`, al.`name`, agl.`name` AS `attribute_group`
 FROM `' . _DB_PREFIX_ . 'attribute_group` ag
 LEFT JOIN `' . _DB_PREFIX_ . 'attribute_group_lang` agl ON (ag.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = ' . (int)($id_lang) . ')
 LEFT JOIN `' . _DB_PREFIX_ . 'attribute` a ON a.`id_attribute_group` = ag.`id_attribute_group`
 LEFT JOIN `' . _DB_PREFIX_ . 'attribute_lang` al ON (a.`id_attribute` = al.`id_attribute` AND al.`id_lang` = ' . (int)($id_lang) . ')
 ' . ($notNull ? 'WHERE a.`id_attribute` IS NOT NULL AND al.`name` IS NOT NULL' : '') . '
 ORDER BY agl.`name` ASC, a.`id_attribute` ASC');
 }
}
?>

Link to comment
Share on other sites

I'm using Prestashop 1.4.7

This is the Attribute.php BACKUP:

 

<?php
/**
 * <InsertFileDescription>
 * @date: 28/03/2012
 *
 */
class Attribute extends AttributeCore
{
 /**
  * Get all attributes for a given language
  *
  * @param integer $id_lang Language id
  * @param boolean $notNull Get only not null fields if true
  * @return array Attributes
  */
 public static function getAttributes($id_lang, $notNull = false)
 {
  return Db::getInstance()->ExecuteS('
 SELECT ag.*, agl.*, a.`id_attribute`, al.`name`, agl.`name` AS `attribute_group`
 FROM `' . _DB_PREFIX_ . 'attribute_group` ag
 LEFT JOIN `' . _DB_PREFIX_ . 'attribute_group_lang` agl ON (ag.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = ' . (int)($id_lang) . ')
 LEFT JOIN `' . _DB_PREFIX_ . 'attribute` a ON a.`id_attribute_group` = ag.`id_attribute_group`
 LEFT JOIN `' . _DB_PREFIX_ . 'attribute_lang` al ON (a.`id_attribute` = al.`id_attribute` AND al.`id_lang` = ' . (int)($id_lang) . ')
 ' . ($notNull ? 'WHERE a.`id_attribute` IS NOT NULL AND al.`name` IS NOT NULL' : '') . '
 ORDER BY agl.`name` ASC, a.`id_attribute` ASC');
 }
}
?>

 

 

And this is the one I made using your instructions (Attribute.php):

<?php
/**
 * <InsertFileDescription>
 * @date: 28/03/2012
 *
 */
class Attribute extends AttributeCore
{
 /**
  * Get all attributes for a given language
  *
  * @param integer $id_lang Language id
  * @param boolean $notNull Get only not null fields if true
  * @return array Attributes
  */
 public static function getAttributes($id_lang, $notNull = false)
 {
return Db::getInstance()->ExecuteS('
SELECT *
FROM `' . _DB_PREFIX_ . 'attribute` a
LEFT JOIN `' . _DB_PREFIX_ . 'attribute_lang` al ON (a.`id_attribute` = al.`id_attribute` AND al.`id_lang` = ' . (int)($id_lang) . ')
WHERE a.`id_attribute_group` = ' . (int)($id_attribute_group) . '
ORDER BY a.`id_attribute`');
 }
}
?>

Link to comment
Share on other sites

Not sure what you have done, but the code in your backup should be the code used. The second block of code is from early on in this thread and for a different class and shouldn't be used.

 

Looks like you got a little mixed up somewhere.

 

If you update your current code with the code listed as your backup, it should work for you.

 

I'm also on 1.4.7 and the code works fine.

Link to comment
Share on other sites

Yeah, likely I mixed them.

 

Anyway, I did use the one from BACKUP, but the results are still the same - sorted by name, not by ID. I attached screen captures from my database and admin page.

 

Do you think there's something wrong in my database?

post-343063-0-76644600-1332945427_thumb.jpg

post-343063-0-24258100-1332945428_thumb.jpg

Link to comment
Share on other sites

can't see anything wrong with your database. The query ran against the database is still sorting by the name and not the id.

 

Which file did you change, was it /override/classes/Attribute.php or /classes/Attribute.php?

Link to comment
Share on other sites

Guess what I just found out - the list is sorted just the way I want it, but only in the Product Combination page. If I go to Product Combination Generator, it's still sorted by name.

 

So definitely you're onto it, you put your finger in the right place, but maybe Product Combination Generator is using another function to display the list?

 

See the image attached...

 

Well, even if we can't figure it out, you've been a great help and I appreciate that.

post-343063-0-54062700-1332964368_thumb.jpg

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...