Created
April 7, 2014 15:55
-
-
Save jreinke/10023002 to your computer and use it in GitHub Desktop.
Magento: find potential unused product attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* MAGENTO_ROOT_DIR/unused.php | |
* php -f unused.php | |
*/ | |
$mageFilename = 'app/Mage.php'; | |
if (!file_exists($mageFilename)) { | |
echo $mageFilename." was not found"; | |
exit; | |
} | |
require_once $mageFilename; | |
// Bootstrap Magento | |
Mage::app('admin'); | |
set_time_limit(0); | |
ini_set('display_errors', 1); | |
$product = Mage::getModel('catalog/product'); | |
$attributesByTable = $product->getResource()->loadAllAttributes($product)->getAttributesByTable(); | |
$adapter = Mage::getResourceSingleton('core/resource')->getReadConnection(); | |
foreach ($attributesByTable as $table => $attributes) { | |
foreach ($attributes as $attribute) { | |
$sql = "SELECT `value`, COUNT(*) AS `count` FROM `$table` WHERE `attribute_id` = " . $attribute->getAttributeId() . " GROUP BY `value`"; | |
$rows = $adapter->fetchAll($sql); | |
if (count($rows) >= 0 && count($rows) <= 2) { | |
var_dump($attribute->getAttributeCode(), $rows); | |
} | |
} | |
} |
I ended up doing it like this: https://github.com/borasocom-team/Magento-AttributeCleaner/blob/0092895c603bcc38dfb2515054bc24b34cb7004d/src/app/code/community/Boraso/AttributeCleaner/Helper/Data.php#L147 Feedback is very welcome
Just in case someone else runs into this... I think the correct if test would be -
if(( count($rows) === 1 && $rows[0]["value"] === null )|| (count($rows) === 0)){
You are looking for attributes that only have null for a value or has no no rows in the attribute table.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi mate, thanks for your work! I'm using your base to build a module here https://github.com/borasocom-team/Magento-AttributeCleaner .
At https://gist.github.com/jreinke/10023002#file-unused-php-L30 : i think there is a bug. If there is only value associated, the array has two elements. Please see this:
A better condition should be:
Am I wrong?