Monday 25 July 2016

How to add new option to attribute programatically Magento ?

   

we can easily do this witht the below peace of code. It will add a new option to the existing attribute.

   $arg_attribute = 'your_attribute_code'; // enter your attribute code here
        $arg_value = 'new option value';
        $attr_model = Mage::getModel('catalog/resource_eav_attribute');
        $attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
        $attr_id = $attr->getAttributeId();
        $option['attribute_id'] = $attr_id;
        $option['value']['any_option_name'][0] = $arg_value;
        $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
        $setup->addAttributeOption($option);

Get Category and subcategory ids array in Magento

Hi with the below code you will get the array of parent child categories.



$rootcatId  = Mage::app()->getStore()->getRootCategoryId();
    $root_cat   = Mage::getModel('catalog/category')->load($rootcatId);
    $categories = $this->get_child_categories($root_cat);
    echo "<pre>";
    print_r($categories);
    echo "</pre>";


    function get_child_categories($parent) {
        $cat_model = Mage::getModel('catalog/category');
        $categories = $cat_model->load($parent->getId())->getChildrenCategories();
        $ret_arr = array();
        foreach ($categories as $cat)
        {
            $ret_arr[] = array(
                                'cat_id' => $cat->getId(),
                                'cat_name' => $cat->getName(),
                                'cat_url' => $cat->getUrl(),
                                'child_cats' => $this->get_child_categories($cat),
                                );
        }
        return $ret_arr;

    }