Skip to content

Instantly share code, notes, and snippets.

@jreinke
Created April 7, 2014 15:55
Show Gist options
  • Save jreinke/10023002 to your computer and use it in GitHub Desktop.
Save jreinke/10023002 to your computer and use it in GitHub Desktop.
Magento: find potential unused product attributes
<?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);
}
}
}
@ZaneCEO
Copy link

ZaneCEO commented Dec 1, 2017

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:

img

A better condition should be:

if( count($rows) === 1 && $rows[0]["value"] === null ) {

Am I wrong?

@ZaneCEO
Copy link

ZaneCEO commented Dec 1, 2017

@groggu
Copy link

groggu commented Apr 28, 2018

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