Skip to main content

Programatically Delete Products by Sku ( Using CSV )

Hi All, Recently while working on a project. I need to delete products from Magento and I only have a CSV having all the product's SKU. For that I have written a script which reads SKU from CSV file and deleting products.

Below is the code which you can use:

        
<?php

//Path to Magento

require_once('app/Mage.php');

umask(0);

Mage::app();

ini_set('display_errors', 1);



// Register a secure admin environment

Mage::register('isSecureArea', 1);



$file = "remove_product.csv"; // CSV File name with SKU's

$fileData=fopen($file,'r');



if (($handle = fopen("remove_product.csv", "r")) !== FALSE) {

  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

    $num = count($data);

    $row++;

    for ($c=0; $c < $num; $c++) {

        echo $data[$c] . "<br />\n";

        $sku = $data[$c];

        $product = Mage::getModel('catalog/product')->loadByAttribute('sku',  $sku);

        if($product) {

            try {

                $product->delete();

                echo $productSku . " Deleted<br>";

            } catch (Exception $e) {

                echo $productSku . " Not Deleted<br>";

            }

        } else {

            echo $productSku . " Not Available<br>";

        }

    }

  }

  fclose($handle);

}

Comments

Popular posts from this blog

Add Products to cart Programmatically in Magento

Sometimes we need to add products into cart programmatically in controller. Below is the code which you can use to add product to cart.   1) For Simple Products If you are working in magento controller and wants to add a simple product programmatically then here is the code: Suppose our product id value stored in $productId.         // Below code will create instance of cart         $cart = Mage::getModel('checkout/cart');         // This will initialize cart         $cart->init();         // Get the product collection by passing product id         $productCollection = Mage::getModel('catalog/product')->load($productId); This product collection will be required as we have to pass it at the time of adding product to cart. For adding the product we...

Magento Multiple Store Setup

Many times we need to create multiple stores in magento website. In magento we create different stores to make it more local. We can access different stores using below ways: 1) Domain (e.g. www.store1.com and www.store2.com) 2) Subdomain (e.g. store1.mystore.com and store2.mystore.com) 3) Folder (e.g. mystore.com/store1/ and mystore.com/store2/) Its all upto to you which way you prefer. For all these we need to follow below steps: Step 1: Add new Magento store: It might be possible that both store have different  catalogue so to manage this. We need to make two different Base Categories. To create a new base category follow below steps: Log in to your Magento admin panel. Go to Catalog -> Manage Categories . If you want both your websites to share same “ Default Category ”, select it by clicking on it on the left. Or click Add Root Category to create a new root category different from the existing one. Once the category is selected, under the Gene...

Angular JS Table Filter and Sorting

Hi All, I am new to angular js and doing some random stuff. I am sharing the code here. Hope this will help you some where. <style> table, td  {   border: 1px solid grey;   border-collapse: collapse;   padding: 5px; } .sortorder:after {   content: '\25b2'; } .sortorder.reverse:after {   content: '\25bc'; } </style> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <body> <div ng-app="myApp" ng-controller="customersCtrl"> <table>     <tr>         <td ng-click="order('Name')">           Customer Name           <span class="sortorder" ng-show="predicate === 'Name'" ng-class="{reverse:reverse}"></span>         </td>     ...