0

I want to add a product to a category page depending on the category ID. This product is not part of the category.

In Mage_Catalog_Block_Product_List I see the following line:

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

Would it be possible to have an if underneath? - Is this the right place?

$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
if ($this->getCategoryId() == 123) {
  //add product id 456 to the collection
}

If so, how do I add a product to the $category collection?

And further to this, is it possible to insert it in a particular place, lets say inbetween the 3rd and 4th product?

0

Try Below code fastest way to set product to category

$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
if ($this->getCategoryId() == 123) {
  //add product id 456 to the collection
    $productIds[$product->getId()]  = 1; // here key is product id and value is position 
    $category->setPostedProducts($productIds);
    $category->save();
}

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.