3

I have removed all outdated products from my Magento 1.7.x website. Now I have some empty configurable products.

How can I find all configurable products without connection to a simple product? They should be deleted too.

3 Answers 3

2

You can get list of non child's configurable product easily. using Mysql Query

Magento save the relation between configurable product and it child products at catalog_product_super_link and save it product basic data at catalog_product_entity Table.

enter image description here

For getting those configurable products that are not have with a any simple product/child products.try below Query

SELECT * FROM catalog_product_entity where entity_id not in (SELECT parent_id FROM catalog_product_super_link`) and type_id='configurable

The above Query give you list of resulting configurable ids. Now can easily delete product from backend or doing by code.

error_reporting(E_ALL | E_STRICT);

require_once 'YourmagentoDir/app/Mage.php';
Mage::register("isSecureArea", 1);
Mage::setIsDeveloperMode(true);
Mage::app('admin'); 

$product = Mage::getModel('catalog/product')
            ->load($id);
        if($product->getId()):
        try {
            $product->delete();
            echo 'The product has been deleted.';
        } catch (Exception $e) {
           $e->getMessage())
        }

endif
3
  • Thank you for your answer. I've tweaked your answer and solved my issue.
    – hans2103
    Oct 6, 2015 at 10:18
  • @amit bera, pls see this : magento.stackexchange.com/questions/192506/… Sep 8, 2017 at 9:05
  • The query in this answer still seems to work to find all empty configurables in Magento 2. Not sure about the code however, do you know if it would still work? Sep 15, 2021 at 14:23
1

Copy this code and place it in your Magento root directory and run from url.

 <?php
    error_reporting(E_ALL | E_STRICT);
    $mageFilename = 'app/Mage.php';
    require_once $mageFilename;
    Mage::setIsDeveloperMode(true);
    ini_set('display_errors', 1);
    ini_set('memory_limit', '600M');
    ini_set('max_execution_time', 1800);
    Mage::register("isSecureArea", 1);
    umask(0);
    Mage::app('admin'); 
    /* Get all list of empty configurable products  */
        $_product= Mage::getResourceModel('catalog/product_collection');
        $_product->addAttributeToFilter('type_id',
            array('eq'=> Mage_Catalog_Model_Product_Type_Configurable::TYPE_CODE));
                $options = array();
        foreach($_product as $product){
        #Check if the product has children 1490
             $productAttributesOptions = $product->getTypeInstance(true)->getConfigurableOptions($product);
                  foreach ($productAttributesOptions as $productAttributeOption) {
                      if(count($productAttributeOption) == 0){
                        try{     
                            Mage::getModel("catalog/product")->load($product->getId())->delete(); 
                            echo 'deleted';
                        }
                        catch(Exception $e){     
                            echo "Delete failed"; 
                        }             

                      }

        }
    }

It's work For me.

1
  • Thank you for your answer, but I've ended up using the solution from @Amit-Beta. Creating the list of empty configurables using MySQL took less resources then your solution.
    – hans2103
    Oct 6, 2015 at 10:17
1

Tweaking the answer of @Amit-Bera I came up with this:

SQL statement to create a list of products to be deleted:

SELECT * FROM catalog_product_entity WHERE `entity_id` NOT IN (SELECT `parent_id` FROM `catalog_product_super_link`) AND `type_id`='configurable';

PHP in the Magento root:

<?php
  error_reporting(E_ALL | E_STRICT);


function deleteEmptyConfigurables()
{
  $mageFilename = getcwd() . '/app/Mage.php';

  if (!file_exists($mageFilename))
  {
    echo 'Mage file not found';
    exit;
  }
  require $mageFilename;

  Mage::setIsDeveloperMode(true);

  ini_set('display_errors', 1);
  ini_set('memory_limit', '600M');
  ini_set('max_execution_time', 1800);

  Mage::register('isSecureArea', true);
  umask(0);

  Mage::app('admin');

  $ids = array(...list of all id's...);

  foreach ($ids as $id)
  {
    $product = Mage::getModel('catalog/product')->load($id);

    if($product->getId())
    {
      try
      {
        $product->delete();
        echo 'Deleted: ' . $id . ';<br/>';
      }
      catch(Exception $e)
      {
          echo 'Delete failed: ' . $id . ';<br/>';
          $e->getMessage();
      }
    }
  }
  Mage::unregister('isSecureArea');
}
deleteEmptyConfigurables();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.