m-graphix Posted March 27, 2012 Share Posted March 27, 2012 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 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 More sharing options...
damonsk Posted March 28, 2012 Share Posted March 28, 2012 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 More sharing options...
m-graphix Posted March 28, 2012 Author Share Posted March 28, 2012 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 More sharing options...
damonsk Posted March 28, 2012 Share Posted March 28, 2012 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 More sharing options...
m-graphix Posted March 28, 2012 Author Share Posted March 28, 2012 Did that, unfortunately nothing shows up, the function is not being called at all... You have any other ideas? Link to comment Share on other sites More sharing options...
damonsk Posted March 28, 2012 Share Posted March 28, 2012 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 More sharing options...
m-graphix Posted March 28, 2012 Author Share Posted March 28, 2012 Still the same, the order looks the same. If you want to visualize it better, I attached some screen captures - the list is what I see in the admin and you can see in the database capture how I would like it to see... Link to comment Share on other sites More sharing options...
damonsk Posted March 28, 2012 Share Posted March 28, 2012 The previous code should output the order based on the id and not the attribute name. As a test backup your /classes/Attribute.php class and replace the getAttributes method with the one in my last post. Link to comment Share on other sites More sharing options...
m-graphix Posted March 28, 2012 Author Share Posted March 28, 2012 I'm sorry, but I'm a real newbie in PHP and Prestashop. I backed up /classes/Attribute.php file, but how do I replace the getAttributes method with the one in your last post? Link to comment Share on other sites More sharing options...
damonsk Posted March 28, 2012 Share Posted March 28, 2012 Open the file and search for getAttributes. You will see a function like the above, just copy and paste the code above replacing the old function code. Save and upload. Link to comment Share on other sites More sharing options...
m-graphix Posted March 28, 2012 Author Share Posted March 28, 2012 Oh, OK, I did that, now the list is totally empty... Link to comment Share on other sites More sharing options...
damonsk Posted March 28, 2012 Share Posted March 28, 2012 copy and paste the method code from your backup onto here. Also what version of prestashop are you using? Link to comment Share on other sites More sharing options...
m-graphix Posted March 28, 2012 Author Share Posted March 28, 2012 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 More sharing options...
damonsk Posted March 28, 2012 Share Posted March 28, 2012 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 More sharing options...
m-graphix Posted March 28, 2012 Author Share Posted March 28, 2012 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? Link to comment Share on other sites More sharing options...
damonsk Posted March 28, 2012 Share Posted March 28, 2012 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 More sharing options...
m-graphix Posted March 28, 2012 Author Share Posted March 28, 2012 I changed /override/classes/Attribute.php, but now after you mentioned it I tried changing /classes/Attribute.php too. Still the same result... Link to comment Share on other sites More sharing options...
m-graphix Posted March 28, 2012 Author Share Posted March 28, 2012 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now