1

Regarding the answer from Vinai here https://magento.stackexchange.com/a/5775/199 I have one follow-up question:

How can I get the ProductPosition array when I use flat category tables?

In my system configuration I use these settings:

Catalog -> Frontend -> Use Flat Catalog Category = yes
Catalog -> Frontend -> Use Flat Catalog Product  = yes

When I use getProductsPosition() I receive the following:

Fatal error: Call to undefined method  Mage_Catalog_Model_Resource_Category_Flat::getProductsPosition() in [...]/app/code/core/Mage/Catalog/Model/Category.php on line 318

Any ideas?

Thanks in advance :)

6

This should work.

$categoryId = '46'; //replace with your own
$category = Mage::getModel('catalog/category')->load($categoryId);
$positions = Mage::getResourceModel('catalog/category')->getProductsPosition($category);

This ignores the 'Flat categories' flag. It goes directly to the category resource model.

You can even skip the category loading for performance. and just pass as parameter an object that has an id parameter.

$categoryId = '46'; //replace with your own
$category = new Varien_Object(array('id'=>$categoryId));
$positions = Mage::getResourceModel('catalog/category')->getProductsPosition($category);
  • Thank you. This works in general :-) But I still have a problem left. Now the resetting of the product positions does not work as it did while using non-flat category tables. Do you know why? $categoryId = 273; $category = Mage::getModel('catalog/category')->load($categoryId); $positions = Mage::getResourceModel('catalog/category')->getProductsPosition($category); print_r($positions); $positions[1859] = 8; print_r($positions); $category->setPostedProducts($positions); $category->save(); Sorry for the chaotic formatting :-/ – Celldweller Sep 16 '13 at 15:10
  • btw I really like the Varien_Object approach. Great stuff :-) – Celldweller Sep 16 '13 at 15:19
  • I know...There is a way of saving the positions also. Unfortunately the Mage_Catalog_Model_Resource_Category::_saveCategoryProducts is protected and cannot be called from anywhere. But you can create your own class that extends Mage_Catalog_Model_Resource_Category add a method to it that is a wrapper for _saveCategoryProducts and call the wrapper method in your code. where there's a will there's a way – Marius Sep 16 '13 at 15:43
  • I don't think that Mage_Catalog_Model_Resource_Category::_saveCategoryProducts is the key here. My approach is that you use the setPostedProducts method on a category object and then use save(). Like I said before it already worked. But unfortunately it only works when not using flat category setting. I just tried and checked when Mage_Catalog_Model_Resource_Category::_saveCategoryProducts is called. It is only called when not using flat categories so overwriting would not work or am I missing something? – Celldweller Sep 16 '13 at 15:52
  • You are missing something. Let's say you have class SomeClass sxtends Mage_Catalog_Model_Resource_Category with a method: public function saveProductRelation($category){return $this->_saveCategoryProducts($category)}. $now you can save the products like this: $category = new Varien_Object(array('id'=>$categoryId)); $category->setPostedProducts($positions); $category->setProdyctsPosition($oldPositions);//set the old positions $myClass = new SomeClass(); $myClass->saveProductRelation($category); Sorry for the code format. – Marius Sep 16 '13 at 16:00

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.