Magento 1.9 – Add Position 999 When Assigning Product to Category

magento-1.9PHP

I'm using Magento 1.9.x and trying to change default position number of products.

Ex, when we assign product to category via product page i need to set it to 999 position

enter image description here

I changed default position field value of catalog_category_product table

enter image description here

But nothing changed.

i changed magento\app\code\core\Mage\Catalog\Model\Resource\Category.php

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

but it only effect when adding products to category via category page.

anyone know a solution for this please, Thank You

Best Answer

Navigate here,

app\code\core\Mage\Catalog\Model\Resource

Open Product.php, find function name _saveCategories

You will find the d following array within that file.

$data[] = array(
    'category_id' => (int)$categoryId,
    'product_id'  => (int)$object->getId(),
    'position'    => 1  
);

Replace $data array with following one.

$data[] = array(
    'category_id' => (int)$categoryId,
    'product_id'  => (int)$object->getId(),
    'position'    => 999
);

Additional Note: Go here, app\code\core\Mage\Catalog\Model\Resource, Open Category.php Find _saveCategoryProducts() method name, and looking for the following code.

This will work from category pages in add assign mode.

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

This will work from category pages in update assign mode.

/**
* Update product positions in category
*/
if (!empty($update)) {
    foreach ($update as $productId => $position) {
        $position = 999; //Added this line
        $where = array(
            'category_id = ?'=> (int)$id,
            'product_id = ?' => (int)$productId
        );
        $bind  = array('position' => (int)$position);
        $adapter->update($this->_categoryProductTable, $bind, $where);
    }
}
Related Topic