tuan
9/11/2015 - 10:50 AM

Add display setting for category #Magento #Category #DisplaySettings

Add display setting for category #Magento #Category #DisplaySettings

<?php

/**
 * RootCategoryName is the name of root for store.
 * Level: Level 1 child of root, Level2 child of level 1, Level is relative with rootCategoryLevel
 * Display Mode:
 *
 * @param $rootCategoryName
 * @param $level
 * @param $displayMode
 * @param $pageLayout
 * @return string
 */
$changeDisplaySettingAndCustomDesignOfLevel1Category = function($rootCategoryName, $level, $arrDisplaySettings, $arrCustomDesign) {
    $rootCategoryObj = Mage::getModel('catalog/category')->loadByAttribute('name', $rootCategoryName);
    $rootCategoryLevel = $rootCategoryObj->getLevel();

    $collection = Mage::getModel('catalog/category')
        ->getCollection()
        ->addPathsFilter($rootCategoryObj->getPath().'/');

    $arr = array();
    foreach($collection as $catObj) {
        $catLevel = $catObj->getLevel();

        if(($catLevel - $rootCategoryObj) == $level) {

            // set attribute Display Settings
            foreach($arrDisplaySettings as $key => $value) {
                $catObj->setData($key, $value);
            }

            // set attribute in Custom Design
            foreach($arrCustomDesign as $key => $value) {
                $catObj->setData($key, $value);
            }

            $catObj->save();

        }
    }

    return implode(",", $arr);
};
$changeDisplaySettingAndCustomDesignOfLevel1Category('AcerEMEA Root', 1,
array(
    'display_mode' => 'PRODUCTS'
),
array(
    'page_layout' => 'two_columns_right',
)
);