Skip to content

Instantly share code, notes, and snippets.

@sreichel
Last active July 9, 2018 17:21
Show Gist options
  • Save sreichel/b080bd01feb5eae2ba7a928878bf31f3 to your computer and use it in GitHub Desktop.
Save sreichel/b080bd01feb5eae2ba7a928878bf31f3 to your computer and use it in GitHub Desktop.
Delete orphan product data
<?php
require_once('../app/Mage.php');
Mage::app('admin');
$entityType = Mage::getModel('catalog/product')->getResource()->getTypeId();
$attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection')
->setEntityTypeFilter($entityType);
$count = 0;
foreach ($attributeSetCollection as $attributeSet) {
$productIds = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('attribute_set_id', $attributeSet->getId())
->getAllIds();
$attributeIds = Mage::getResourceModel('catalog/product_attribute_collection')
->setAttributeSetFilter($attributeSet->getId())
->getColumnValues('attribute_id');
$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('core_read');
$write = $resource->getConnection('core_write');
$tables = array(
'catalog_product_entity_datetime',
'catalog_product_entity_decimal',
'catalog_product_entity_int',
'catalog_product_entity_text',
'catalog_product_entity_varchar'
);
# just for tests
foreach ($tables as $table) {
$query = $read->select()
->from($resource->getTableName($table), array('value_id'))
->where('attribute_id NOT IN (?)', $attributeIds)
->where('entity_id IN (?)', $productIds);
$deletaIds = $read->fetchCol($query, 'value_id');
$count = $count + count($deletaIds);
}
//foreach ($tables as $table) {
// $condition = array(
// $write->quoteInto('entity_id IN (?)', $productIds),
// $write->quoteInto('attribute_id NOT IN (?)', $attributeIds)
// );
// $write->delete($resource->getTableName($table), $condition);
//}
}
var_dump($count);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment