Is it possible to increase product position automatically for specific category. For example, when Product 1 is added under Category A, then the position of Product 1 will be 1 in the Position Column under Category Products. When Product 2 is added under Category A, then the position of Product 2 will be 2 in the Position Column under Category Products and so on. Is there a way to do so?
How to Auto Increase Product Position in Magento Category Products
categoryposition;product
Related Solutions
You need to rewrite the method Mage_Catalog_Model_Resource_Product::_saveCategories
Somewhere in there this is this:
$data[] = array(
'category_id' => (int)$categoryId,
'product_id' => (int)$object->getId(),
'position' => 1
);
Replace 1
with 100
.
To rewrite the resource model add this to the config.xml
of your custom module inside the global tag
<models>
<catalog_resource>
<rewrite>
<product>[Namespace]_[Module]_Model_Resource_Product</product>
</rewrite>
</catalog_resource>
</models>
Then create the class [Namespace]_[Module]_Model_Resource_Product
that extends Mage_Catalog_Model_Resource_Product
, copy the method _saveCategories
to it and replace 1
with 100
.
There is an API class with a method specifically for this. In version 1.9.1.0 you can find the function defined in app/code/core/Mage/Catalog/Model/Category/Api.php
line 467
public function assignProduct($categoryId, $productId, $position = null, $identifierType = null)
So, my recommendation is to scrap using $product->setCategoryIds() and utilize this method instead. $product->setCategoryIds() works just fine if you know ALL the categories the product should belong too, and don't care about position. However it can easily cause problems for you especially if you're doing scripting work on existing products, as it will blow out any other categories the product may have been assigned to (that you didn't supply the id for) as well as blow out the current position the product has been assigned even if the product was already associated to the category prior to performing your scripting work.
Anyways... so if we examine the method's definition, we can see that we need to already have a product id. So if we were to reference your original code, you'll want to save the product, then associate it to the category/categories.
$i = 0;
foreach ($array as $product) {
createProduct($params, $i);
$i++;
if($i == $count){
// finish and continue
}
}
function createProduct($params, $position)
{
$product = Mage::getModel('catalog/product');
$product->setCreatedAt(strtotime('now'));
$product->setAttributeSetId(11);
$product->setName(xxxxxx);
$product->setPrice(xxxxx);
$product->setDescription(xxxxxxx);
// and so on...
$category_ids = array('xx','xxx');
try {
$product->save();
$api = Mage::getSingleton('catalog/category_api');
foreach ($category_ids as $category_id) {
$api->assignProduct($category_id, $product->getId(), $position);
}
}
catch (Exception $ex) {
// catch error
}
}
Best Answer
A way to do it is to add an attribute like Position.
Then set position to say 1.
Then you can run the script getting the attribute value to whatever categoryId you want
Something like this might help.
This can be a way if you set the script on cron to update automatically, ofcourse the script will need to modify.