1

Using CE 1.8.1, I've just categorized a number of products using code like this:

$product = mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$product->setCategoryIds($cats);
$product->save();

I can't get the products to show up in their new categories on the front end. I see the results in the catalog_category_product table - the information is in the database. I also see it when I edit individual products in the admin. From the admin, I've reindexed everything and flushed all cache storage.

What can I do to see these new product categorizations?

3
  • Did you try this with products that are already showing up? So, add a product to a new category or are these new products altogether?
    – user4351
    Commented Apr 27, 2014 at 13:18
  • Did you try to add $product->setStoreId(0) before to save ? Commented Nov 2, 2014 at 10:45
  • did you reindex Category Products index?
    – goivvy.com
    Commented Dec 16, 2016 at 10:29

1 Answer 1

0

The right way of doing this is to add the product to a category rather than assigning categories to a product.

Here is how Magento add a product to a category. Hope it helps...

public function assignProduct($categoryId, $productId, $position = null, $identifierType = null)
{
    $category = $this->_initCategory($categoryId);
    $positions = $category->getProductsPosition();
    $productId = $this->_getProductId($productId);
    $positions[$productId] = $position;
    $category->setPostedProducts($positions);

    try {
        $category->save();
    } catch (Mage_Core_Exception $e) {
        $this->_fault('data_invalid', $e->getMessage());
    }

    return true;
}
2
  • setCategoryIds() is a valid method, and does set the associations. docs.magentocommerce.com/Mage_Catalog/…
    – Laizer
    Commented Apr 27, 2014 at 12:14
  • setCategoryIds(array) simply set the category_ids data of the product. Category ids in database is edited when a product is assigned to a category.
    – ctasca
    Commented Apr 27, 2014 at 13:36

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.