0

I am getting an error while trying to pragmatically remove products from some categories.

Error:

Fatal error: Uncaught TypeError: Argument 3 passed to Mage_Catalog_Model_Resource_Abstract::_canUpdateAttribute() must be of the type array, null given, called in /xxx/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 1225 and defined in /xxx/public_html/app/code/core/Mage/Catalog/Model/Resource/Abstract.php:540

Stack trace: #0 /xxx/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php(1225): Mage_Catalog_Model_Resource_Abstract->_canUpdateAttribute(Object(Mage_Catalog_Model_Resource_Eav_Attribute), '2019-01-14 00:0...', NULL)

#1 /xxx/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php(1123): Mage_Eav_Model_Entity_Abstract->_collectSaveData(Object(Mage_Catalog_Model_Product))

#2 /xxx/public_html/app/code/core/Mage/Core/Model/Abstract.php(318): Mage_Eav_Model_Entity_Abstract->save(Object(Mage_Catalog_Model_Product))

#3 /home/admin/domains/gr in /xxx/public_html/app/code/core/Mage/Catalog/Model/Resource/Abstract.php on line 540

And my code:

$products = Mage::getModel('catalog/category')->setStoreId($storeId)->load($specialCategoryId)
    ->getProductCollection()
    ->addAttributeToSelect(array('special_to_date', 'category_ids'))
    ->addAttributeToFilter('special_to_date', array('lteq' => date("Y-m-d")));

foreach ($products as $product) {

    $oldCategories = $product->getCategoryIds();
    $newCategories = array_diff($oldCategories, $specialCategoryIds);

    if($oldCategories != $newCategories){
        $_product = Mage::getModel('catalog/product')->load($product->getEntityId());

        $_product->setCategoryIds($newCategories);

        Mage::log('Product ID: ' .$product->getEntityId(), null, 'RemoveFromCategoryLog.log', true);
        try {
            $_product->save();
        } catch(Exception  $e){
            Mage::log($e->getMessage(), null, 'RemoveFromCategoryLog.log', true);
        }
    }
}

Loading the product did not solve it. And as there are lots of products i am reluctant to use category_api.

  • you can get a category id have u check it – divya sekar Feb 1 at 9:21
0

If using SQL is an option for you, you can delete and add entries in the table catalog_category_product

For example, if you want to delete product with product_id = 1 from category with category_id = 99 use this statement:

delete from catalog_category_product where product_id = 1 and category_id = 99;

To assign the product with product_id = 1 to category with category_id = 77 (on first position) use this statement:

insert into catalog_category_product (category_id,product_id,position) values (77,1,1);

After doing this you need to rebuild the category index and everything is done.

Otherwise consider to use Mage_Catalog_Model_Category_Api and the methods assignProduct and updateProduct. The product model doesn't save the categoryIds you set with setCategoryIds() - since you need a position for the product in a category.

0

My final solution using sql.

resource = Mage::getSingleton('core/resource');
$write = $resource->getConnection('core_write');
$table = 'catalog_category_product';

$write->delete(
    $table,
    array(
      'category_id = ?' => $catId,
      'product_id = ?' => $product->getEntityId()
    )
);

Based on answers from @HelgeB and @Shadowbob in answer Magento 1 SQL Queries

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.