0

I am creating a custom admin module. One of its tab, I need to add two dropdowns. One for selecting available product types in magento and another one for selecting available categories in magento. How can I achieve this? Please provide some hints for this.

I have searched a lot. But almost all results saying about getting product type and category according of a product that has loaded by an id. I dont want to load any products for this. Just want to get product type and category collections.

Please direct me properly. Thanks in advance

3

You can get the available product types like this:

Mage::getModel('catalog/product_type')->getOptionArray();

So if you want to use that in a select, you can create your select like this:

   $fieldset->addField('product_type', 'select', array(
        'label' => Mage::helper('catalog')->__('Product Type'),
        'title' => Mage::helper('catalog')->__('Product Type'),
        'name'  => 'type',
        'values'=> Mage::getModel('catalog/product_type')->getOptionArray()
    ));

To get the categories is a little trickier: you can do this:

$categories = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToFilter('level', array('gt'=>0)); //this excludes the root of all roots. If you want to exclude the default category replace `'gt'=>0` with `'gt'=>1`.

Now put them in an array:

$arr = array();
foreach ($categories as $category){
    $arr[$category->getId()] = $category->getName();
}

and you can define the field :

$fieldset->addField('categoory_id', 'select', array(
    'label' => Mage::helper('catalog')->__('Category'),
    'title' => Mage::helper('catalog')->__('Category'),
    'name'  => 'category_id',
    'values'=> $arr
));
  • @maris both gave same answer. But you have more detail. I remove my answer. – Keyul Shah Feb 10 '14 at 13:57
  • super.this answer is prefect solution. Thank you so much marius. – Rajeev K Tomy Feb 10 '14 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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