Wednesday, 18 June 2014

How to create a dropdown list of countries in Magento ?

The list of countries in Magento doesn’t work like all other data collections. Rather than store country data in the database, Magento stores country data in an XML file and loads it in on each request.There were some simple functions we can use to access country names and codes in Magento. In this post, I will guide you through how to create a drop-down list of countries in Magento:




Get An Array of Country Names/Codes in Magento

<?php
 
    $countryList = Mage::getResourceModel('directory/country_collection')
                    ->loadData()
                    ->toOptionArray(false);
 
    echo '<pre>';
    print_r( $countryList);
    exit(');
?>
 
The above code will print out an array containing every country code and country name known to Magento.
 

Drop Downs and Country Information

The most common reason developers access country names in Magento is to create a drop down. There are several ways to accomplish this and they differ depending on whether you’re in the admin (backend) or the frontend.

Creating a Country Drop-Down in the Magento Frontend

You can add the following code to any template file in the frontend of Magento and you will get a drop down box using the country name as the label and the country code as the value.
 
<?php $_countries = Mage::getResourceModel('directory/country_collection')
                                    ->loadData()
                                    ->toOptionArray(false) ?>
<?php if (count($_countries) > 0): ?>
    <select name="country" id="country">
        <option value="">-- Please Select --</option>
        <?php foreach($_countries as $_country): ?>
            <option value="<?php echo $_country['value'] ?>">
                <?php echo $_country['label'] ?>
            </option>
        <?php endforeach; ?>
    </select>
<?php endif; ?>
 

Creating a Country Drop Down in the Backend Magento Admin

When creating forms in the Magento Admin area, it is very rare that we use actual HTML. The reason for this is that forms are generally built using pre-built functions. The benefit of this is that each Admin page looks uniform and helps to keep Magento looking like one whole application rather than having loads of bits stuck onto it. As our method of adding HTML changes, so must our method of creating our country drop down.
<?php
 
    $fieldset->addField('country', 'select', array(
        'name'  => 'country',
        'label'     => 'Country',
        'values'    => Mage::getModel('adminhtml/system_config_source_country')->toOptionArray(),
    ));
 
?>
 
I hope you find this tutorial helpful!
 

 




How to setup cross sell in Magento ?

Cross-selling is the action or practice of selling among or between clients, markets, traders, etc. or the action or practice of selling an additional product or service to an existing customer. For Magento, products that are offered as cross-sell will be automatically showed on the checkout cart page before the customer start with their checkout process.
This tutorial shows you how to setup Cross Sell for your Magento store to increase sales.

To set up cross-sell products:

In the admin panel, select Product Information on the left and click Cross-sells.

Next, click the Reset Filter button in the upper-right to list all the available products, or use the search filters at the top of each column to find specific products.


 In the product list, select the check-box of any product you want to feature as a cross-sell.
When finished, click the Save button.Now the selected products will be showed in Checkout Process as Cross Sell products.


How to add date picker to Magento admin backend page?

To add Date Picker to Magento admin backend configuration page. There is no direct model that can be called to add the date picker. However, these simple steps will allow you to add the date picker to back-end of your Magento site.

Step 1.

Edit system.xml, create the new field as follows:

<my_date translate="label comment">
<label>Expire On</label>
<frontend_type>text</frontend_type> <!-- Set the frontend type as Text -->
<frontend_model>MODULE_NAME/adminhtml_system_config_date</frontend_model> <!-- Specify our custom model -->
<sort_order>4</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>Set the expiry date for the Feature Tour</comment>
</my_date>
 
 

Step 2.

Create the new model file at the path: app\code\local\<NAMESPACE>\<MODULE>\Block\Adminhtml\System\Config\Date.php 


<?php
class Arvtour_Tour_Block_Adminhtml_System_Config_Date extends Mage_Adminhtml_Block_System_Config_Form_Field
{
    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {
        $date = new Varien_Data_Form_Element_Date;
        $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
 
        $data = array(
            'name'      => $element->getName(),
            'html_id'   => $element->getId(),
            'image'     => $this->getSkinUrl('images/grid-cal.gif'),
        );
        $date->setData($data);
        $date->setValue($element->getValue(), $format);
        $date->setFormat(Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT));
        $date->setForm($element->getForm());
 
        return $date->getElementHtml();
    }
}
 

How to change favicon of your magento site

Changing favicon of a magento site is very easy without making any changes to code. Here were the steps to follow:

First method:

Go to System->configuration->Design
Under design open HTML Head Tab and upload your favicon ico and Click Save Config Button


Second Method:

To change the favicon ico in magento to paste your favicon ico instead of magento favicon ico (remove magento favicon ico and paste your favicon ico) in following folders



(localhost address)
After paste favicon ico clear the browser history and again login your magento admin panel and also Refresh front page

Thursday, 5 June 2014

How will you call a CMS page in your module’s PHTML file?

$this->getLayout()->createBlock(‘cms/block’)->setBlockId(‘blockidentifier’)->toHtml();

What is codePool in Magento?

codePool is a tag which you have to specify when registering new module in app/etc/modules/Company_Module.xml
There are 3 codePools in Magento: core, community and local, which are resided at app/code/ directory.
Core codePool is used by Magento core team, Community is generally used by 3rd party extensions and Local codePool should be used for in-hour module development and overriding of core and community modules for custom requirement.
So in short, codePool helps Magento to locate module inside app/code/ for processing.

When will you need to clear cache to see the changes in Magento?

When you have added/modified XML, JS, CSS file(s).