0

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
  • In this case, SSH (i.e. Putty) is the right way: run php categories.php in the directory where you put the file. Commented Dec 28, 2015 at 9:11

1 Answer 1

0

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

answered Dec 28, 2015 at 4:38
2
  • 1
    Thank 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. Commented Mar 31, 2017 at 14:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.