1

I want use the code, but i get a error:
"Fatal error: Call to a member function delete() on boolean in …"

The code:

public function _del()   
{
    umask(0);
    Mage::app();
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    $f = file("var/import/sku.csv");

    foreach ($f as $line_num => $productSku) {
        $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $productSku);
        $product->delete();
    }
}

With the sku.csv:

220112¬  
220111¬  
220114¬  
3

Tom,you need to upload product object by primary key.loadByAttribute() not capable for delete the product.

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



   /* product exit then the prooduct will be deleted
*/
    if($product->getId()):
        $id=$product->getId();
        $product = Mage::getModel('catalog/product')->load($id);
        $product->delete();
     endif;
  • Thanks for your help Amit. Ok. Works well with a single SKU '220114',but not with foreach. I get "Fatal error: Call to a member function getId() on a non-object in …" by the line "if($product->getId()): …" Do you have a tipp? – user23008 Mar 2 '15 at 12:42
  • first i check product is exit in system by using product id – Amit Bera Mar 2 '15 at 12:43
  • yes i've checked it several times and try. I've try to change the sku's in the csv to '148568' … but it isn't work. – user23008 Mar 2 '15 at 14:54
2

loadByAttribute() could return a boolean. It will return the object on success and boolean false on failure (see Mage_Catalog_Model_Abstract::loadByAttribute()). So in your specific case the SKU could not be found. You need to factor the object first assigning it to a variable and then on the next line call loadByAttribute() to avoid this:

public function _del()   
{
    umask(0);
    Mage::app();
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    $f = file("var/import/sku.csv");

    foreach ($f as $line_num => $productSku) {
        $product = Mage::getModel('catalog/product');
        $product->loadByAttribute('sku', $productSku);
        if ($product->getId() && $product->delete()) {
            // Do something like `return true;`?
        }
    }
}
  • 1
    $product->getId() && $product->delete(); – Jeroen Mar 3 '15 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy