0

I have a seperaed .php file and that is located in root folder. Which all are the code i need to include to get the magento predefined function. The following code i run in server and the getting category array values is null

require_once('/var/www/html/app/Mage.php'); //Path to Magento
umask(0);
Mage::app();

$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
mysql_select_db('test');

$product = Mage::getModel('catalog/product')->load('40185');
$cats = $product->getCategoryIds();

foreach ($cats as $category_id) {
   $_cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($category_id);
   print_r($_cat);
}

Result

 Mage_Catalog_Model_Category Object (

     [_eventPrefix:protected] => catalog_category
     [_eventObject:protected] => category
     [_cacheTag:protected] => catalog_category
     [_useFlatResource:protected] => 1
     [_designAttributes:Mage_Catalog_Model_Category:private] => Array
         (
             [0] => custom_design
             [1] => custom_design_from
             [2] => custom_design_to
             [3] => page_layout
             [4] => custom_layout_update
             [5] => custom_apply_to_products
         )

     [_treeModel:protected] => 
     [_urlModel:protected] => 
     [_defaultValues:protected] => Array
         (
         )

     [_storeValuesFlags:protected] => Array
         (
         )

     [_lockedAttributes:protected] => Array
         (
         )

     [_isDeleteable:protected] => 1
     [_isReadonly:protected] => 
     [_resourceName:protected] => catalog/category_flat
     [_resource:protected] => 
     [_resourceCollectionName:protected] => catalog/category_flat_collection
     [_dataSaveAllowed:protected] => 1
     [_isObjectNew:protected] => 
     [_data:protected] => Array
         (
             [store_id] => 1
         )

     [_hasDataChanges:protected] => 
     [_origData:protected] => Array
         (
             [store_id] => 1
         )

     [_idFieldName:protected] => 
     [_isDeleted:protected] => 
     [_oldFieldsMap:protected] => Array
         (
         )

     [_syncFieldsMap:protected] => Array
         (
         )

 )

2 Answers 2

0

Try add bellow code in your file

<?php
require_once('app/Mage.php');
Mage::app('default');

echo '<pre>';
$product = Mage::getModel('catalog/product')->load(40185);
$cats = $product->getCategoryIds();

foreach ($cats as $category_id) {
   $_cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($category_id);
   print_r($_cat);
}
?>
0

You may also try with following code:

$product = Mage::getModel('catalog/product')->load('40185');

$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
    $_category = Mage::getModel('catalog/category')->load($category_id) ;
    echo $_category->getName();
} 

Also use $_category->getData(), to get the other details of category. It returns the array of category info.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.