Pages

Sunday, July 7, 2013

How to limit the collection of a model in Magento

In this post, I will show you how to limit the collection of a Model in Magento. I will use catalog product module for this example.

First, you can use setPageSize() method for setting number of items you want to display in one page.

    Mage::getModel(‘catalog/product’)->getCollection()
       ->setPageSize(10);

The second example is to display 10 items from a 5th item

    $start = 5;
    $limit = 10;
    $collection = Mage::getModel(‘catalog/product’)->getCollection();
    $collection->getSelect()
       ->limit($limit,$start);

Change the page tittle in Magento

There are some ways for you to change the page title in Magento. Today, Magento Tutorial will introduce you some of the easiest ways:

1) CMS page: Go to backend and open CMS > Manage Pages. Select page to edit and you can set title in ‘Page Title’ field.

2) In frontend and any module, you can use xml file to set new title for your page. The syntax is:

    <reference name=“header”>
    <action method=“setTitle” translate=“title”><title>Your new page title</title></action>
    <reference>
    </reference></reference>

3) If it does not work, now you should look at your block php file. You might easily find this line:

    $this->getLayout()->getBlock(‘head’)->setTitle($title);

Change the $title to what you want.

How to fix Cookie Problem in Magento

Today, Magento Tutorial will give you the way to fix Cookie problem. When you try to add a product to your cart, it redirects you back to a Cookies Page, which is annoying.

So, here are two ways to fix this :

1) Increase Cookie Lifetime

– Go to System –> Configuration –> General –> Web –> Session Cookie Management –> Cookie Lifetime = 5400
– By default, Cookie Lifetime = 3600. You can make it more than 5400 and try if it works.

2) Disable redirect to enable-cookies CMS page

– Go to System –> Configuration –> General –> Web –> Browser Capabilities Detection –> Redirect to CMS-page if cookies are disabled = No

How to hide other shipping methods when free shipping is enabled

When free shipping method is enabled, it is shown along with other available shipping methods unlike free payment method ‘Zero Subtotal Checkout’.
There is no harm in showing other payment methods along with free shipping method. Nevertheless some merchants wants to hide rest of the methods when it is enabled.

There are many ways to do it. One of the way is to override the method:

    Mage_Checkout_Block_Onepage_Shipping_Method_Available::getShippingRates()

Step 1. Rewrite the block class

File: app/code/local/MagePsycho/Shipmentfilter/etc/config.xml:

entfilter/etc/config.xml:
Code:

    …
    <blocks>
        …
        <checkout>
            <rewrite>
                <onepage_shipping_method_available>MagePsycho_Shipmentfilter_Block_Onepage_Shipping_Method_Available</onepage_shipping_method_available>
            </rewrite>
        </checkout>
        …

Step 2. Override the getShippingRates() method

File: app/code/local/MagePsycho/Shipmentfilter/Block/Onepage/Shipping/Method/Available.php

Code:

    <?php
    /**
     * @category   MagePsycho
     * @package    MagePsycho_Shipmentfilter
     * @author     magepsycho@gmail.com
     * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
     */
    class MagePsycho_Shipmentfilter_Block_Onepage_Shipping_Method_Available extends Mage_Checkout_Block_Onepage_Shipping_Method_Available
    {
        public function getShippingRates()
        {
            $rates = parent::getShippingRates();
            if (array_key_exists(‘freeshipping’, $rates)) {
                $rates = array(‘freeshipping’ => $rates['freeshipping']);
            }
    
            return $rates;
        }
    }

Step 3. Refresh the cache.

How to filter payment method in one page checkout

There are several way to filter payment method in one page checkout:

     By overriding template: app/design/frontend/[interface]/[theme]/template/checkout/onepage /payment/methods.phtml

     By overriding method: Mage_Checkout_Block_Onepage_Payment_Methods::_canUseMethod()
     By overriding method: Mage_Payment_Model_Method_Abstract::isAvailable()
     By overriding method: Mage_Checkout_Block_Onepage_Payment_Methods::getMethods()
     By observing event: payment_method_is_active

Among above methods obviously using event-observer technique is the best way to go.
And here I will be discussing about how to enable the PayPal (Website Standard) method only when current currency is USD.
Suppose a skeleton module(MagePsycho_Paymentfilter) has already been created.

Step 1:

Register the event: ‘payment_method_is_active’ in config.xml.
Add the following xml code in app/code/local/MagePsycho/Paymentfilter/etc/config.xml:

    …
    <frontend>
        …
        <events>
            <payment_method_is_active>
                <observers>
                    <paymentfilter_payment_method_is
    _active>
                        <type>singleton</type>
                        <class>paymentfilter/observer</class>
                        <method>paymentMethodIsActive</method>
                    </paymentfilter_payment_method_is_active>
                </observers>
            </payment_method_is_active>
        </events>
        …
    </frontend>
    …

Step 2: Implement the observer model

Create observer file: app/code/local/MagePsycho/Paymentfilter/Model/Observer.php and paste the following code:

    <?php
    /**
     * @category   MagePsycho
     * @package    MagePsycho_Paymentfilter
     * @author     magepsycho@gmail.com
     * @website    http://www.magepsycho.com
     * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
        */
    class MagePsycho_Paymentfilter_Model_Observer {
    
        public function paymentMethodIsActive(Varien_Event_Observer $observer) {
            $event           = $observer->getEvent();
            $method          = $event->getMethodInstance();
            $result          = $event->getResult();
            $currencyCode    = Mage::app()->getStore()->getCurrentCurrencyCode();
    
            if( $currencyCode == ‘USD’){
                if($method->getCode() == ‘paypal_standard’ ){
                    $result->isAvailable = true;
                }else{
                    $result->isAvailable = false;
                }
            }
        }
    
    }


Step 3:
Go ahead for testing.

How to pass Magento certification exam in 30 days

1. What is Magento Certification Program?

Magento Certification is designed to validate real-world job skills and give hiring managers a tool for identifying qualified individuals. Exams are mid-advanced level certifications geared for Magento professionals that have real-world experience with Magento implementations.
In 2012, Magento started Magento certification exams which are geared toward professionals who want to differentiate themselves form the competition with the ultimate Magento credential. Store-owners also benefit form this since they can easily find a qualified developer to make sure their website is handled by trusted hands, Magento certificate becomes valuable and indispensable to developers form all over the world who want to make money in Magento field.
WHAT EXAMS ARE AVAILABLE?
  • Magento Front End Developer Certification
  • Magento Certified Developer
  • Magento Certified Developer Plus
2. How to get high score in Magento certification exam?
 
Today, Magento tutorial wants to introduce all of you the Magento Free ebook: “How to pass Magento Certification exam in 30 days”. This book is written based on the 10 topics announced in the Study guide of Magento. This book presents full knowledge of each topic as well as provides useful tips for developers to work with Magento. At the end of some topics, you can find questions that are designed to help revise what you have learned. This ebook is written by David Nguyen, technical manager of MagestoreHe’s leader for many important projects of Magestore last year. 


3. How to download it?


Hope it helpful for you!

How to convert multi-select field to checkbox in advanced search form of Magento

Today, Magento Tutorial will discuss with you how to convert multi-select field to checkbox in advanced search form of Magento.

Step 1:

Copy the following file to your working theme:
app/design/frontend/[interface]/[theme]/template/catalogsearch/advanced/form.phtml

 Step 2:

Open the form.phtml (from above) and find the following line just after the case ‘select’: ?>

<div class=“input-box”>

     <?php echo $this->getAttributeSelectElement($_attribute) ?>
    </div>

and replace it by the following code:

    <?php if(in_array($_attribute->getAttributeCode(), array(‘manufacturer’))): ?>
    <div class=“input-box”>
        <?php
             $options = $_attribute->getSource()->getAllOptions(false);
             foreach($options as $_option):
                 $isChecked = in_array($_option['value'],
$this->getRequest()->getParam($_attribute->getAttributeCode())) ? ‘ checked=”checked”‘ : null;
                 ?>
        <input type=”checkbox” name=”<?php echo $_attribute->getAttributeCode(); ?>[]“ value=”<?php echo $_option['value']; ?>“<?php echo $isChecked; ?> /> <?php echo $_option['label']; ?><br />
        <?php
             endforeach;
        ?>
    </div>
    <?php else: ?>
    <div class=“input-box”>
        <?php echo $this->getAttributeSelectElement($_attribute); ?>
    </div>
    <?php endif; ?>

Note: Here we have customized the display for manufacturer attribute, similarly you can customize for other attributes. Just you need to add the attribute code(for example: color) in the array as:

    <?php if(in_array($_attribute->getAttributeCode(), array(‘manufacturer’, ‘color’))): ?>

 Step 3:

Try to refresh the advanced search page: http://your-magento-url/catalogsearch/advanced

You will see:



Note: You can use some css in order to break the checkboxes in multi-columns for better display.

Friday, July 5, 2013

getBaseUrl – Magento URL Path

When developing in Magento and playing arround with Magento Themes there is some functions you should know.

If you want to get the source url of an image, javascript or file, call one of this functions adding your own path at the end.

Under every function there is an example of the output value:

http://example.com/
<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB); ?>

http://example.com/js/
<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS); ?>

http://example.com/index.php/
<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK); ?>

http://example.com/media/
<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA); ?>

http://example.com/skin/
<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN); ?> 

SEO URL in Magento

When you installMagento probably your URLs would be like:

  • http://www.domain.com/index.php/about-us
There is an option to remove that index.php from the URL.

Go to your admin panel -> System -> General -> Web -> Search Engines Optimization.
Change Use Web Server Rewrites to Yes.



Now your URL will be cool enough  

How to create Downloadable Products in Magento

1. Go to Catalog -> Manage Products -> Add product.

Select the Downloadable Product type:


2. Basic set up. Name, price, stock, website, category…



3. Downloadable set up.


Here you can create optionally Samples and at least one Link.

You can upload files or put an URL.



Now we can see the product in our frontend store.

Once you buy the item it will show up in your customer account (My Account). After validate the order it will allow you to download the file.