Skip to content

Instantly share code, notes, and snippets.

@df2k2
Last active December 6, 2016 03:22
Show Gist options
  • Save df2k2/22007c16503ec17599a3fbfb0ecdb13f to your computer and use it in GitHub Desktop.
Save df2k2/22007c16503ec17599a3fbfb0ecdb13f to your computer and use it in GitHub Desktop.
Df2k2's Magento Scripts
<?php
/**
* External Script for listing categories levels and the quantity of various product types and
*
* @copyright Copyright (c) 2016 Chris Snedaker
* @author Chris Snedaker <df2002@gmail.com>
*
*/
// This file is placed in a sub-directory of Magento's root.
/* @var $mageFilename Magento Filename */
$mageFilename = '../app/Mage.php';
error_reporting(E_ALL | E_STRICT);
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
umask(0);
Mage::app();
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
?>
<!doctype html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CMS Pages Data</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.3/css/bootstrap.min.css" integrity="sha384-MIwDKRSSImVFAZCVLtU0LMDdON6KVCrZHyVQQj6e8wIEJkW4tvwqXrbMIya1vriY" crossorigin="anonymous">
</head>
<body>
<?php
// External Magento Code
//$inactive = Mage::getModel('cms/page')->getCollection()->addFieldToSelect('*')->addFieldToFilter('is_active',false);
//$collection = Mage::getModel('cms/page')->getCollection()->addFieldToSelect('*')->addFieldToFilter('is_active',true);
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE)
->addAttributeToFilter('type_id', Mage_Catalog_Model_Product_Type::TYPE_SIMPLE);
$configurables = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('visibility', array('neq' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE))
->addAttributeToFilter('type_id', Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE);
$visibles = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('visibility', array('neq' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE));
//$category = Mage::getModel('catalog/category')->load(135);
/*->addAttributeToSelect('*')
->addAttributeToFilter('level','2');*/
$visibleEntity = Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'visibility');
//Zend_Debug::dump(Mage::getModel('catalog/product_visibility')->getOptionText(1));
//Zend_Debug::dump($visibleEntity->getSource()->getData());
$visibilityOptions = Mage::getModel('catalog/product_visibility')->getOptionArray();
?>
<section>
<div>Not Visible Simple Product Count: <strong><?php echo $collection->getSize(); ?></strong></div>
<div>Configurable Product Count: <strong><?php echo $configurables->getSize(); ?></strong></div>
<div>Total Visible Product Count: <strong><?php echo $visibles->getSize(); ?></strong></div>
<?php
$levels = [2, 3, 4, 5];
$allTotal = 0;
foreach ($levels as $level): ?>
<?php
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('level', $level);
//load(135);
$total = 0; ?>
<hr>
<div class="h5">Level <?php echo $level; ?> Categories</div>
<?php foreach ($categories as $category): ?>
<?php $simpleCount = 0;
$simpleCount = $category->getProductCollection()->addFilter('type_id', 'simple')->getSize(); ?>
<div>
Category <strong><?php echo $category->getName(); ?></strong> has
<strong><?php echo $category->getProductCount(); ?></strong> products assigned and
<strong> <?php echo $category->getProductCollection()->addFilter('type_id', 'simple')->getSize(); ?> </strong> are simple products.
</div>
<?php $total += $simpleCount; ?>
<?php endforeach; ?>
<?php $allTotal += $total; ?>
<div class="m-y-2">
Total Simple Associations for level <?php echo $level; ?> Categories:
<strong><?php echo $total; ?></strong>
</div>
<?php endforeach; ?>
<div class="m-t-2 m-b-1">ALL Total Simple Associations for All Levels: <strong><?php echo $allTotal; ?></strong></div>
</section>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js" integrity="sha384-THPy051/pYDQGanwU6poAc/hOdQxjnOEXzbT+OuUAFqNqFjL+4IGLBgCJC3ZOShY" crossorigin="anonymous"></script>
<script>window.jQuery || document.write('<script src="/assets/js/vendor/jquery.min.js"><\/script>')</script>
<script src="//v4-alpha.getbootstrap.com/assets/js/vendor/tether.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.3/js/bootstrap.min.js" integrity="sha384-ux8v3A6CPtOTqOzMKiuo3d/DomGaaClxFYdCu2HPMBEkf6x2xiDyJ7gkXU0MWwaD" crossorigin="anonymous"></script>
<script src="//v4-alpha.getbootstrap.com/assets/js/docs.min.js"></script>
<script>
Holder.addTheme('gray', {
bg: '#777',
fg: 'rgba(255,255,255,.75)',
font: 'Helvetica',
fontweight: 'normal'
})
</script>
</body>
</html>
<?php
/**
* External Script for removing unnecessary product-to-category relationships because of anchor parent categories
*
* @copyright Copyright (c) 2016 Chris Snedaker
* @author Chris Snedaker <df2002@gmail.com>
*
*/
// This file is placed in a sub-directory of Magento's root.
/* @var $mageFilename Magento Filename */
$mageFilename = '../app/Mage.php';
error_reporting(E_ALL | E_STRICT);
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
umask(0);
Mage::app();
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$resource = Mage::getSingleton('core/resource');
$adapter = $resource->getConnection('core_write');
$table = $resource->getTableName('catalog_category_product');
$collection = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('level', array('gteq' => 2))
->addAttributeToFilter('is_anchor', true)
->addAttributeToFilter('children_count', array('gt' => 0));
$ids = $collection->getAllIds();
//Zend_Debug::dump($ids);
// -----------------------------------------------------------------------------
$cond = array(
'category_id IN (?)' => $ids
);
$adapter->delete($table,$cond);
// Reindex
Mage::getSingleton('index/indexer')
->getProcessByCode('catalog_category_product')
->reindexAll();
<?php
/**
* External Script for removing unnecessary product-to-category relationships because products are
* simple products and "not visible individually." They are child products of a configurable where configurable parent is
* associated to category.
*
* @copyright Copyright (c) 2016 Chris Snedaker
* @author Chris Snedaker <df2002@gmail.com>
*
*/
// This file is placed in a sub-directory of Magento's root.
/* @var $mageFilename Magento Filename */
$mageFilename = '../app/Mage.php';
error_reporting(E_ALL | E_STRICT);
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
umask(0);
Mage::app();
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$resource = Mage::getSingleton('core/resource');
$adapter = $resource->getConnection('core_write');
$table = $resource->getTableName('catalog_category_product');
// -----------------------------------------------------------------------------
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE)
->addAttributeToFilter('type_id', Mage_Catalog_Model_Product_Type::TYPE_SIMPLE);
$ids = $collection->getAllIds();
$cond = array(
'product_id IN (?)' => $ids,
//'category_id IN (?)' => $ids
);
$adapter->delete($table,$cond);
Mage::getSingleton('index/indexer')
->getProcessByCode('catalog_category_product')
->reindexAll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment