|
<?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'); |
|
*/ |