Pages

Sunday, July 7, 2013

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.


Can’t log in Magento Admin

Look for the file:

/app/code/core/Mage/Core/Model/Session/Abstract/Varien.php

Arroud the line 100:
call_user_func_array('session_set_cookie_params', $cookieParams);

Add bars to comment the line:
//call_user_func_array('session_set_cookie_params', $cookieParams);

Done!

Now you will be able to login. 

Add links in Magento

Today we’re going to see how to add them to a Static Block or a Page (under your CMS panel).

Example:

If you want to add a link to the page About Us in your footer. How you do that?

If you try this code it will only work for the default lenguage. We need Magento to build a link with our lenguage tag.

<a href="/about-us">About us</a>

What we need is something like http://my_site.com/en/about-us

So we will use Magento shortcodes. You will need to write the page identifier after the direct_url:




<a href="{{store direct_url='about-us'}}">About us</a>

Using the structure above it will build links dinamically with everything it needs in the url.

Extra fee shopping cart price rules in Magento

Under the Promotions tab in your admin panel:



It’s cool but it’s not cool enough. What I wanted to do is to set a surcharge for one payment method. I needed to add a charge in PayaPal method.
So I found the way you can set negative discounts (what means a surcharge) in the cart total price.

Note: we are going to change Magento Core files. Please make a backup of your files before continuing.

1. Go to app/code/core/Mage/Rule/Model/Rule.php

Find the line:

//check if discount amount > 0
if ((int)$this->getDiscountAmount() < 0) {
Mage::throwException(Mage::helper(‘rule’)->__(‘Invalid discount amount.));
}
Just add some bars // to comment the code:

//check if discount amount > 0
//if ((int)$this->getDiscountAmount() < 0) {
//Mage::throwException(Mage::helper(‘rule’)->__(‘Invalid discount amount.’));
//}

2. Now go to: app/code/core/Mage/Adminhtml/Block/Promo/Quote/Edit/Tab/Actions.php

Look for:

$fieldset->addField(‘discount_amount’, ‘text’, array(
‘name’ => ‘discount_amount’,
‘required’ => true,
class=> ‘validate-not-negative-number’,
‘label’ => Mage::helper(’salesrule’)->__(‘Discount amount’),
And again comment like this:

$fieldset->addField(‘discount_amount’, ‘text’, array(
‘name’ => ‘discount_amount’,
‘required’ => true,
// ‘class’ => ‘validate-not-negative-number’,
‘label’ => Mage::helper(’salesrule’)->__(‘Discount amount’),

3. Translate the discount word.

Go to app/locale/es_ES (I’m using the Spainish translation so maybe yours would be en_US)
Find a file called Mage_Sales.csv. Inside look for the word discount you will find something like:

"Discount (%s)","Discount (%s)"
You can change the value in order to show your own text. For example:

"Discount (%s)","Extra Fee (%s)"

Done! Now you’re free to set negative values in the input field:



Fixed amount discount for whole cart

If you select this option the code above will not work entirely. 

How to change currency position in Magento


 I mean it doesn’t crash or something it’s just it doesn’t change the currency position.
Example:
For US Dollars have to be $10,00
But for Euro in Spain we use 10,00€
There is an easy way to change the currency position in the Locale (lenguage) you want.
In my case I wanted to change the € simbol for the Spanish locale on my Magento installation.

1. Go from your root folder to /lib/Zend/Locale/Data

2. Find the lenguage file. In my case es.xml

3. Look for <currencyFormat>

A line just below you will find the <pattern>
Change the side of the weird simbol and leave it like this:

Before
<currencyformatlength>
   <currencyformat>
    <pattern>¤ #,##0.00</pattern>
  </currencyformat>
</currencyformatlength>
After
<currencyformatlength>
   <currencyformat>
    <pattern>#,##0.00 ¤</pattern>
  </currencyformat>
</currencyformatlength>

Now this becomes interesting. Try to refresh the Magento Cache and the browser cache. In my case nothing happened. The change made effect itself an hour after this.
I’m not sure but I think its something about caching those XML files.
Anyway, just be patient until the change comes by itself.