0

Question: I have sorted products in my subcategories by positions, but the sorting is not working for the parent category. For some reason magento mixes everything up (which I find extremely illogical). Can anyone tell me how I can make the parent category sort the products by the position and so that they go subcategory by subcategory not mixing up altogether.

In advance: there are solution in the internet but there is too little details. Solutions say something like - create a class with this content...

I am an absolute newbie in magento and I don't know where I need to create files, how to name them etc. Please, as more details as possible.

Thanks in advance.

  • I've been having the same problem for about a year and I've still had no luck. Magento seems to sort each sub-categories products (of the parent category), rather than just sorting all of the products. – Liam McArthur Sep 13 '16 at 10:15
  • 1
    I actually found the solution. I created a new web-site on a different platform and deleted all the magento files and database and never downloaded them since. Worked really well – Timur Gafforov Sep 14 '16 at 12:12
  • Haha! Great answer! I seem to be having quite a lot of problems with Magento lately. I've managed to fix the problem though, so I'll post my answer below. – Liam McArthur Sep 14 '16 at 15:05
2

I've been trying to wrap my head around this for months and I've finally figured out what the problem is.

You need to make sure that every single product category has it's parent categories ticked also. For some reason, any higher categories than the sub are just added to the end of the collection (if the anchor is set). Here's a script I've written and placed into the shell folder so that it can be run manually or via cron:

<?php
/**     
 *
 * @category    Mage
 * @package     Mage_Shell
 * @copyright  Copyright (c) 2006-2017 X.commerce, Inc. and affiliates (http://www.magento.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

require_once 'abstract.php';

/**
 * Assign products to parent category
 *
 * @category    Mage
 * @package     Mage_Shell
 * @author      Liam McArthur <email-address@withdrawn.com>
 */
class Mage_Shell_Compiler extends Mage_Shell_Abstract {

    public function run() {

        $collection = Mage::getModel( 'catalog/product' )->getCollection()->addAttributeToSelect( '*' );

        foreach ( $collection as $product ) {

            $lastCategoryId = end( $product->getCategoryIds() );

            $category = Mage::getModel( 'catalog/category' )->load( $lastCategoryId );

            $ids = array();

            foreach ( $category->getParentCategories() as $parent ) {
                // Ignore root category
                if ( $parent->getId() != 2 ) {
                    $ids[] = $parent->getId();
                }
            }

            // Assign above categories to product
            $product->setCategoryIds( $ids );
            $product->save();
            echo $product->getSku() . " saved..." . PHP_EOL;

        }
    }

}

$shell = new Mage_Shell_Compiler();
$shell->run();
  • Dear Liam! Well I can't check it as I hope that I will never have to work with magento again, but I will set this as a solution :) Even though I still think that this is the most stupid system ever (for small projects), as it is one of the core features that should have been enabled when the code was first written. I've heard a lot that it's a very good system for projects of high load and with correct tweaking, but I really think that if you have a big project it' would be way easier to develop own software :) – Timur Gafforov Sep 16 '16 at 4:51
  • Very interesting Liam ! Can you share this script with us ? – Gael COAT Aug 29 '17 at 16:11
  • @GaelCOAT unfortunately, I don't have the script on this computer anymore. But it was as simple as looping through all products, getting the category ID, then looping through them categories to get their parent category ID's as arrays and adding these ID's back to the product - then re-saving the product. – Liam McArthur Sep 4 '17 at 14:52
  • @GaelCOAT I've updated my answer with a shell script that does the job. – Liam McArthur Aug 23 '18 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.