3

I would like to update the default position value that products are assigned when they are added to a category.

When you create/update a category from the categories editor, new products are saved with a position of 0.

What I want to do is have items save with a position of 100 instead of 1.

Thank you!

3
  • What is the purpose of it? In which version of Magento you want to achieve the same?
    – Taran
    Commented Mar 13, 2018 at 20:53
  • My Magento is 1.9.2. I want to do this because everytime I create a new category and I want to sort the products, I have to change all the positions to 100 (or another number different than 0) to realocate the products that I want to show first with a lower number of position.
    – Vinícius
    Commented Mar 14, 2018 at 12:30
  • You can assign 100 position to product while product creation, let me know if you need more information. Commented Mar 22, 2018 at 5:00

2 Answers 2

1

There is no method or event to change this. If you want to change default value you can rewrite Mage_Catalog_Model_Resource_Category::_saveCategoryProducts

/**
 * Add products to category
 */
if (!empty($insert)) {
    $data = array();
    foreach ($insert as $productId => $position) {
        $data[] = array(
            'category_id' => (int)$id,
            'product_id'  => (int)$productId,
            // changed code
            // 'position'    => (int)$position
            'position'    => (int)$position ? (int)$position : 100
        );
    }
    $adapter->insertMultiple($this->_categoryProductTable, $data);
}

This should set position to 100 if you do not enter a value.

2
  • Thanks this save my time file 1.9.2.4 path is app/code/core/Mage/Catalog/Model/Resource/Category.php Line No 342, it's better to copy this file to local an edit, if you wish to upgrade Magento version in future Commented Sep 4, 2018 at 17:01
  • I notice this not work when product assign to a category via product edit page category tree, anyone know a solution for that magento.stackexchange.com/questions/241188/… Commented Sep 7, 2018 at 16:59
1

You can work with negative position numbers too - i.e. since all your products have set position = 0 by default just set those you want to have displayed before them to position = -1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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