7/30/2012

How to Add country and state Dropdown in magento admin

I din't able to create this in proper way like magento does but if you will have all the state of all country then I think this is the perfect solution. If you don't have all state for all country then this module is not solved your problem. If any one knows the correct way then please add the solution via comment. I am describing here How I exactly did.

Open your form which is in Yournamespace/Modulename/Block/Adminhtml/Modulename/Edit/Tab/Form.php then add below fields

$country = $fieldset->addField('country', 'select', array(
            'name'  => 'country',
            'label'     => 'Country',
            'values'    => Mage::getModel('adminhtml/system_config_source_country') ->toOptionArray(),
            'onchange' => 'getstate(this)',
        ));

$fieldset->addField('state', 'select', array(
            'name'  => 'state',
            'label'     => 'State',
            'values'    => Mage::getModel('modulename/modulename')
                            ->getstate('AU'),
        ));

         /*
         * Add Ajax to the Country select box html output
         */
        $country->setAfterElementHtml("<script type=\"text/javascript\">
            function getstate(selectElement){
                var reloadurl = '". $this
                 ->getUrl('modulename/adminhtml_modulename/state') . "country/' + selectElement.value;
                new Ajax.Request(reloadurl, {
                    method: 'get',
                    onLoading: function (stateform) {
                        $('state').update('Searching...');
                    },
                    onComplete: function(stateform) {
                        $('state').update(stateform.responseText);
                    }
                });
            }
        </script>");

Now Create State Action in modulenamecontroller.php file which will be like this

    public function stateAction() {
        $countrycode = $this->getRequest()->getParam('country');
        $state = "<option value=''>Please Select</option>";
        if ($countrycode != '') {
            $statearray = Mage::getModel('directory/region')->getResourceCollection() ->addCountryFilter($countrycode)->load();
            foreach ($statearray as $_state) {
                $state .= "<option value='" . $_state->getCode() . "'>" . $_state->getDefaultName() . "</option>";
            }
        }
        echo $state;
    }

Wysiwyg Editor not working in custom module created by Module creator

When you create a module using module creator then if you try to turn on the Wysiwyg editor of magento then you need to add some function in the Edit.php file which resides under Namespace/Modulename/Block/Adminhtml/Modulename/Edit.php

protected function _prepareLayout() {
    parent::_prepareLayout();
    if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
        $this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
    }
}

Now turn on the Wysiwyg editor in your code which may be like below

$fieldset->addField('summary', 'editor', array(
            'name' => 'summary',
            'label' => Mage::helper('modulename')->__('Description'),
            'title' => Mage::helper('modulename')->__('Description'),
            'style' => 'height:100px;',
            'wysiwyg' => true,
            'required' => true,
        ));

7/24/2012

Join custom table to product collection in magento

For your custom module if you want to join your custom table data with Magento default product collection then you need to join tables with the entiry_id of the product and your product_id stored in your custom table. Here I have just used the resource model collection to join the table. I tried with getmodel feature of Magento don't know why it doesn't able to create the Product grid in the backend. So I used resource model and it worked. If you are only joining the table, not to generate the Product grid, then you can use getmodel instead of resource model.

$collection = Mage::getResourceModel('catalog/product_collection')
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('sku')
                ->addAttributeToSelect('price')
                ->addAttributeToSelect('status')
                ->addAttributeToSelect('visibility')
                ->addAttributeToFilter('type_id', array('eq' => 'simple'))
                ->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                ->addAttributeToFilter('visibility', array('neq' => 1));

$collection->getSelect()->join(array('mep' => "mage_brand_product"), "e.entity_id = mep.product_id", array('mep.*'));