How to remove a product from a category magento 1.5

How to remove a product from a category magento 1.5

October 2, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to remove a product from a category magento 1.5

Removing Products from Specific Categories in Magento 1.5

Hello, developers! Today I’d like to address a common challenge faced by new Magento users - removing products from specific categories based on certain criteria. Recently, we received this question and I’m excited to provide a step-by-step guide using the latest best practices.

The Initial Approach: Disabling Products

The user has already started with the right approach. They are using Magento’s core functionality to fetch products that have passed their ‘news_to_date’ field and then disabling those products. This is a great starting point, but it doesn’t actually remove the product from the category. Let’s fix this.

Step 1: Fetching the Specific Category

Before we can remove a product from a category, we need to fetch that specific category object first. In your case, you want products removed from the ‘Sales’ category. Here’s how you do it:

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

In this line of code, replace 2 with the actual ID of your ‘Sales’ category.

Step 2: Removing Products from the Category

Now that we have our category object, we can loop through each product in that category and remove it. The key is to use the removeProduct() method provided by Magento:

foreach ($category->getProducts() as $product) {
    $category->removeProduct($product->getId());
}

This code will iterate over every product in your ‘Sales’ category and use the removeProduct() method to physically remove it from that category.

Step 3: Saving the Category

After you’ve removed all the products, don’t forget to save the category for the changes to take effect:

$category->save();

A Better Approach: Using Magento’s Product Collection Filtering

The user in the question is already using a product collection filter, which is great! We can build upon this. Instead of filtering by just one field (‘news_to_date’), we can also add a category condition to our filter:

$collection = Mage::getModel('catalog/product')->getCollection();
$categoryId = 2; // Replace with your 'Sales' category ID
$collection->addAttributeToFilter('category_id', array('in' => $categoryId))
          ->addFieldtofilter('news_to_date', array(array('to' => $date)));

In this code, we’re filtering the product collection by both the ‘category_id’ (which matches our ‘Sales’ category) and the ‘news_to_date’.

The Complete Solution

Combining all these steps together, your final script should look like this:

require_once 'app/Mage.php';
Mage::app();
$date = Mage::getModel('core/date')->date('Y-m-d H:i:s');

$collection = Mage::getModel('catalog/product')->getCollection();
$categoryId = 2; // Replace with your 'Sales' category ID
$collection->addAttributeToFilter('category_id', array('in' => $categoryId))
          ->addFieldtofilter('news_to_date', array(array('to' => $date)));

foreach ($collection as $product) {
    $product->delete();
}

This script will fetch all products in the ‘Sales’ category that have passed their news_to_date, and it will physically delete them from your database.

Remember, this is a powerful operation. Always make sure to backup your data before running such scripts.

Conclusion

In conclusion, removing products from specific categories in Magento involves fetching the category object, looping through its products, using the removeProduct() method, saving the changes, and possibly deleting the product if no longer needed. By combining these steps with proper filtering techniques, you can create efficient cron jobs that handle your product management needs.

Don’t forget to explore more advanced Magento features on our blog at Laravel Company. We cover everything from Magento upgrades to e-commerce performance optimization. Thanks for reading!

Magento 1 Catalog Errors

Our network