1

I am usign Magento 1.7. I have a store with thousands of products, but I only need to keep some of them. I have a list of the names of the products that I need to keep.

Which is the best way to only have the products I want in my store?

  • Should I delete all the other products? Which would be the best way to do so?

  • Should I just disable all the other products? Is there a way to do so programmatically? It is not practical to disable some thousands of products.

I don't have the list of SKU's by the moment. I only have the list of product names.

I am still a bit new to Magento and don't fully know how it works. Any help will be much appreciated.

2

If you don't need this products, i also would delete them.

You can try this (just comment $action->delete(); or $action->disable();):

MAGENTO_ROOT/myProductAction.php

<?php
require_once('./app/Mage.php');
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$action = new MyProductAction;

// to delete
$action->delete();

// to disable
$action->disable();

class MyProductAction
{
    // add your product names here
    public $productNames = array(
        'Product Name 1',
        'Product Name 2'
    );

    public function delete()
    {
        $collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToFilter('name', $this->productNames)
        $collection->walk('delete');
    }

    public function disable()
    {
        $products = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToFilter('name', $this->productNames)
            ->addAttributeToSelect(array('name'), 'inner');
        Mage::getSingleton('core/resource_iterator')->walk(
            $products->getSelect(),
            array(array($this, 'disableCallback'))
        );
    }

    public function disableCallback($args)
    {
        $product = Mage::getModel('catalog/product');
        $product->setData($args['row']);
        $product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
        $product->getResource()->saveAttribute($product, 'status');
    }
}
| improve this answer | |
  • That is such a great answer, thank you so much. – LeCodeur Jun 9 '17 at 7:35
1

It's better to delete all the useless products in your store, if you are sure that you won't need them later down the line.

You can easily do a script that will query the DB to get all the product SKUs corresponding to the names you have (even better is to get their IDs).

Once you have their IDs, get the collection (using Mage::getResourceModel('catalog/product_collection')) and select all the other products.
Loop on this collection of undesirable products and simply delete them like so :

$product->delete();
| improve this answer | |
  • 2
    Instead of loop on the collection you can do $collection->walk('delete'); – sv3n Jun 6 '17 at 9:16
  • I didn't know that, thanks for your input ;) – Julien Lachal Jun 6 '17 at 10:37

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.