4

We have around 10 products in one category with category name as "custom" & id = "40"

We created some "Brands" [values] in backend.

enter image description here

Once we click on each brand, it will display like below image:

enter image description here

In above image, below Status field, we want to create a Multiselect box [similar like store view] and we want to display those 10 products assigned to category "custom" in multi-select box.

app/code/local/Company/Brand/Block/Adminhtml/Item/Edit/Tab/Min.php

<?php
class Company_Brand_Block_Adminhtml_Category_Edit_Tab_Main extends Mage_Adminhtml_Block_Widget_Form
{
    /**
     * Preparation of current form
     *
     * @return Outthink_Brand_Block_Adminhtml_Category_Edit_Tab_Main
     */
    protected function _prepareForm()
    {
        $model = Mage::registry('brand_category');

        $form = new Varien_Data_Form();
        $form->setHtmlIdPrefix('brand_');

        $fieldset = $form->addFieldset('base_fieldset', array (
                'legend' => Mage::helper('brand')->__('General information'), 
                'class' => 'fieldset-wide' ));

        if ($model->getCategoryId()) {
            $fieldset->addField('category_id', 'hidden', array (
                    'name' => 'category_id'
            ));
        }



        $fieldset->addField('category_name', 'text', array (
            'name' => 'category_name', 
            'label' => Mage::helper('brand')->__('Category Name'), 
            'title' => Mage::helper('brand')->__('Category Name'), 
            'required' => true,
        ));

        $fieldset->addField('cat_order', 'text', array (
            'name' => 'cat_order', 
            'label' => Mage::helper('brand')->__('Category Order'), 
            'title' => Mage::helper('brand')->__('Category Order'), 
            'required' => false,
        ));

        /**
         * Check is single store mode
         */
        if (!Mage::app()->isSingleStoreMode()) {
            $fieldset->addField('store_id', 'multiselect', 
                    array (
                            'name' => 'store_id[]', 
                            'label' => Mage::helper('brand')->__('Store view'), 
                            'title' => Mage::helper('brand')->__('Store view'), 
                            'required' => true, 
                            'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true) ));
        }
        else {
            $fieldset->addField('store_id', 'hidden', array (
                    'name' => 'store_id[]', 
                    'value' => Mage::app()->getStore(true)->getId() ));
            $model->setStoreId(Mage::app()->getStore(true)->getId());
        }

        $fieldset->addField('is_active', 'select', 
                array (
                        'label' => Mage::helper('brand')->__('Status'), 
                        'title' => Mage::helper('brand')->__('Category Status'), 
                        'name' => 'is_active', 
                        'required' => true, 
                        'options' => array (
                                '1' => Mage::helper('brand')->__('Enabled'), 
                                '0' => Mage::helper('brand')->__('Disabled') ) ));

        $form->setValues($model->getData());
        $this->setForm($form);

        return parent::_prepareForm();
    }
}

I will give 50 bounty points if it worked for me.

1 Answer 1

2
+50

Add this code in your form:

$products = Mage::getModel('catalog/category')->load(40)
    ->getProductCollection()
    ->addAttributeToSelect('*');

$categorylist = array();
$categorylist[] = array(
    'value' => '', 'label' => Mage::helper('sales')->__('Category')
);
foreach ($products as $product) {
    $categorylist[] = array(
        'value' => $product->getEntityId(), 'label' => $product->getName()
    );
}
$fieldset->addField('category', 'multiselect',
    array(
        'label' => Mage::helper('sales')->__('Category'),
        'class' => 'required-entry',
        'required' => true,
        'values' => $categorylist,
        'name' => 'category[]',

    ));
11
  • i added like this : pasted.co/8d3b5a75 , i added your code below the this line '0' => Mage::helper('brand')->__('Disabled') ) )); Commented Jul 21, 2016 at 9:46
  • what is the output it's working? Commented Jul 21, 2016 at 9:49
  • sorrry, not working for me Commented Jul 21, 2016 at 9:49
  • any error the filed is showing or not? Commented Jul 21, 2016 at 9:50
  • no error, it showing same as before. Commented Jul 21, 2016 at 9:51

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.