Monday, 25 July 2016

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;

    }

Wednesday, 29 June 2016

How to create multiple custom options programmatically Magento ?


 To create a multiple custom options of a product in single time you can use  below  piece of code

<?php
require_once 'app/Mage.php';
Mage::app();
$productId = 905;   // your product id
$product = Mage::getModel('catalog/product')->load($productId);
                    $options = array(
                        array(
                        'title' => 'Color',
                        'type' => 'drop_down',
                        'is_required' => 1,
                        'sort_order' => 0,
                        'values' => array(
                                        array(
                                            'title' => 'Red',
                                            'price' => 10.50,
                                            'price_type' => 'fixed',
                                            'sku' => '',
                                            'sort_order' => 0,
                                        ),
                                        array(
                                            'title' => 'Gold',
                                            'price' => 0,
                                            'price_type' => 'fixed',
                                            'sku' => '',
                                            'sort_order' => 0,
                                        )
                                    )
                        ),
                        array('title' => 'size',
                        'type' => 'drop_down',
                        'is_required' => 1,
                        'sort_order' => 0,
                        'values' => array(
                                        array(
                                            'title' => 'small',
                                            'price' => 10.50,
                                            'price_type' => 'fixed',
                                            'sku' => '',
                                            'sort_order' => 0,
                                        ),
                                        array(
                                            'title' => 'large',
                                            'price' => 0,
                                            'price_type' => 'fixed',
                                            'sku' => '',
                                            'sort_order' => 0,
                                        )
                                    )
                        )
            );
//print_r($options);die;
 foreach($options as $options1){
$product->getOptionInstance()->unsetOptions();
$optionsnew = array($options1);
$product->setProductOptions($optionsnew);
$product->setCanSaveCustomOptions(true);
$product->save();
}
?>