Skip to content

Instantly share code, notes, and snippets.

@btray77
Last active January 16, 2016 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btray77/fd23fe265de58eae95fb to your computer and use it in GitHub Desktop.
Save btray77/fd23fe265de58eae95fb to your computer and use it in GitHub Desktop.
Used make categories active or turn them off depending on if they have products in them in Magento 1.9.2.2 (other versions may work). This should be installed in your webroot/shell folder and run via a cron script as often as you wish it to run. We run it 2 times a day.

Here's an example cron. It runs at 3am/3pm daily. Only if the website is not undergoing maintenance.

0 3,15 * * * ! test -e /var/www/html/maintenance.flag && /usr/bin/php /var/www/html/shell/activeCategories.php

You could use something like this.

0 3,15 * * * /usr/bin/php /var/www/html/shell/activeCategories.php

<?php
/*
The MIT License (MIT)
Copyright (c) 2016 Brad Traynham
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
ini_set('auto_detect_line_endings', TRUE);
ini_set('display_errors', 'On');
ini_set('memory_limit', '512M');
error_reporting(E_ALL);
require_once('abstract.php');
class Mage_Shell_Updater extends Mage_Shell_Abstract
{
protected $_argname = array();
public function __construct() {
parent::__construct();
// Time limit to infinity
set_time_limit(0);
}
// Shell script point of entry
public function run() {
// You may need to change the store
Mage::app()->setCurrentStore('default');
$collection = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*');
foreach($collection as $cat) {
$category = Mage::getModel('catalog/category')->load($cat->getEntityId());
$prodCollection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($category);
$countItems = $prodCollection->getSize().PHP_EOL;
$isActive = $category->getIsActive();
if ($countItems < 1 && $isActive ==1) {
//If no items, and it is an active category.
$category->setIsActive(0);
$category->save();
}
// More than 1 item and is not active make active.
elseif ($countItems > 0 && $isActive ==0)
{
$category->setIsActive(1);
$category->save();
}
$_subcategories = $category->getChildrenCategories();
if (count($_subcategories) > 0){
foreach($_subcategories as $_subcategory){
$countItems = $_subcategory->getProductCount().PHP_EOL;
$isActive = $_subcategory->getIsActive();
if ($countItems < 1 && $isActive ==1) {
//If no items, and it is an active category.
$_subcategory->setIsActive(0);
$_subcategory->save();
// More than 1 item and is not active make active.
}elseif ($countItems > 0 && $isActive ==0)
{
$_subcategory->setIsActive(1);
$_subcategory->save();
}
}
}
}
}
}
$shell = new Mage_Shell_Updater();
// Uncomment these commented lines if you want to stop Index changes while this is running.
/*
$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_MANUAL));
$processes->walk('save');
*/
$shell->run();
/*
$processes->walk('reindexAll');
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME));
$processes->walk('save');
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment