Can someone please give a noob tutorial, step by step how to run php scripts. I need to run the script located here: Categories - how to display in alphabetical order? (Programmatically) But I cannot find anywhere on the internet, how to run this script. From what I understand you make a .php with the script in it. But what then? how do you run? through putty? through the browser?? can someone give specific instructions how this works. Tell me it like im 5 years old. Thank you.
1 Answer
Follow bellow steps
Step : 1 Fist create categories.php
file on the magento root folder
Step : 2 After add bellow code in file categories.php
.
<?php
require_once('app/Mage.php');
ini_set('display_errors', 1);
Mage::app('admin');
class CAT
{
public function index()
{
//set the initial parameters for the function
$root = 1;
$conn = Mage::getSingleton('core/resource')->getConnection('core_write');
//sort categories
$this->orderCategories($root, $conn);
}
public function orderCategories($parentId, $conn)
{
//The position field is in the main category table
//add prefix if you have one
$table = 'catalog_category_entity';
//get the children for a specific category id
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('parent_id', $parentId);
$categories = array();
foreach ($collection as $category){
//in case there are 2 categories with the same name we shouldn't skip them
//but there shouldn't be
$categories[$category->getName().'_'.$category->getId()] = $category->getId();
}
//sort categories by name
ksort($categories);
//set their position
$position = 1;
foreach ($categories as $name=>$id){
$q = "UPDATE {$table} SET `position` = {$position} where `entity_id` = {$id}";
$conn->query($q);
$position++;
//sort the current category children by calling the same function recursively
$this->orderCategories($id, $conn);
}
}
}
$obj = new CAT();
$obj->index();
?>
Step : 3 Run categories.php
file in your browser for eg. http://magentohost.com/categories.php
-
1Thank you so much. A programmer was planning to charge 100$ to fix this problem for me. You saved my wallet. Commented Jan 7, 2016 at 1:07
-
You can also extend the Mage_Shell_Abstract class which gives you a way to parse parameters and loads up magento for you.– DominoCommented Mar 31, 2017 at 14:04
php categories.php
in the directory where you put the file.