Pages

Wednesday, August 21, 2013

How to Override controller in magento / Override controller’s action in magento

Requirement : Some times you may run into situation where you want to override the core functionality of Magento core controllers. So in this case, The best practise is override the controller then override actions or add new actions for custom requirement.

For example you want to override OnepageController.php file and overwride indexAction.

In this example my custom module name space is “MyBlog” and i am going to overwride indexAction Mage_Checkout_OnepageController

Base File Path : app/code/core/Mage/Checkout/controllers/OnepageController.php

To Do the same we have to follow bellow steps.

Step1: We need to create a custom module

File Path : app/etc/modules/

File Name: MyBlog_Checkout.xml

File Content:

<?xml version="1.0"?>
<config>
    <modules>
        <MyBlog_Checkout>
            <active>true</active>
            <codePool>local</codePool>
        </MyBlog_Checkout>
    </modules>
</config>

Step2: Now we have to create bellow files in our custom module

File Path: app/code/local/MyBlog/Checkout/etc/config.xml

Bellow is content for config.xml file

<?xml version="1.0"?>
<config>
     <modules>
       <MyBlog_Checkout>
         <version>0.0.1</version>
       </MyBlog_Checkout>
     </modules>

    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <MyBlog_Checkout before="Mage_Checkout">MyBlog_Checkout</MyBlog_Checkout>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>
</config>

Bellow is content for OnepageController.php

File Path : app/code/local/MyBlog/Checkout/controllers/OnepageController.php

<?php
 
require_once 'Mage/Checkout/controllers/OnepageController.php';
class MyBlog_Checkout_OnepageController extends Mage_Checkout_OnepageController {

     public function indexAction()
    {
        echo "Great! You are in MyBlog checkout controller section";
        exit;
    }

}
?>

Monday, August 19, 2013

How to delete a quote in Magento

Use the code below to delete a quote:

<?php
$quoteID = Mage::getSingleton("checkout/session")->getQuote()->getId();

if($quoteID)
{
    try {
        $quote = Mage::getModel("sales/quote")->load($quoteID);
        $quote->setIsActive(false);
        $quote->delete();

        return "cart deleted";
    } catch(Exception $e) {
        return $e->getMessage();
    }
}else{
    return "no quote found";
}

How to get shipping and handling cost, tax, discount in magento on shopping cart page

We can simply use the following code

// To get the shipping and handling charge on shopping cart page

Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingAmount();

 // To get discount and tax  on shopping cart page

$totals = Mage::getSingleton('checkout/session')->getQuote()->getTotals(); //Total object

 // To get the discount
if(isset($totals['discount']) && $totals['discount']->getValue()) {
echo 'Discount :'. $discount = $totals['discount']->getValue(); //Discount value if applied
} else {
$discount = '';
}

 // To get tax
if(isset($totals['tax']) && $totals['tax']->getValue()) {
echo 'Tax :'.$tax = $totals['tax']->getValue(); //Tax value if present
} else {
$tax = '';
}

Sunday, August 18, 2013

Create Catalog Price Rule Programmatically in Magento

Here, you will see how to create Catalog Price Rule in Magento through code.

Catalog Rules are applied on products before they are added to the cart.

To create a Catalog Price Rule from Admin Panel, we go to Promotions -> Catalog Price Rules and select Add New Rule.

Basically, there are three main parts for Catalog Price Rule, i.e. Rule Information, Conditions, and Actions.

Here is the code to create Catalog Price Rule. In this code example, I have created Catalog Price Rule with the following information:-

- The rule is applied to particular product with the particular SKU (in our case: ‘chair’)
- The rule is applied as Fixed Amount Discount To certain amount (in our case: 20) of currency amount

$name = "My Catalog Price Rule"; // name of Catalog Price Rule
$websiteId = 1;
$customerGroupId = 2;
$actionType = 'to_fixed'; // discount to fixed amount
//(other options are: by_fixed, by_percent, to_percent)
$discount = 20; // discount amount
$sku = 'chair'; // product sku

$catalogPriceRule = Mage::getModel('catalogrule/rule');

$catalogPriceRule->setName($name)
                 ->setDescription('')
                 ->setIsActive(1)
                 ->setWebsiteIds(array($websiteId))
                 ->setCustomerGroupIds(array($customerGroupId))
                 ->setFromDate('')
                 ->setToDate('')
                 ->setSortOrder('')
                 ->setSimpleAction($actionType)
                 ->setDiscountAmount($discount)
                 ->setStopRulesProcessing(0);

$skuCondition = Mage::getModel('catalogrule/rule_condition_product')
                    ->setType('catalogrule/rule_condition_product')
                    ->setAttribute('sku')
                    ->setOperator('==')
                    ->setValue($sku);

try {
    $catalogPriceRule->getConditions()->addCondition($skuCondition);
    $catalogPriceRule->save();
    $catalogPriceRule->applyAll();
} catch (Exception $e) {
    Mage::getSingleton('core/session')->addError(Mage::helper('catalog')->__($e->getMessage()));
    return;
}

A new Catalog Price Rule with the name “My Catalog Price Rule” has been created. You can view the rule from Promotions -> Catalog Price Rules in admin.

Thursday, August 15, 2013

How to move top links in Content in Magento

If local.xml does not exist,create it /you_theme/layout/ and add the following code Ctalog.xml
{{block type=”catalog/navigation” name=”catalog.topnav” template=”catalog/navigation/top.phtml”}}

Wednesday, August 14, 2013

Get creditcard type in Magento

$order_id = 100; //order id goes here
$_order = Mage::getModel('sales/order')->load($order_id);      
$_cctype = '';
if(!empty($_order))
{          
    $_cctype = $_order->getPayment()->getCcTypeName();
}
echo $_cctype;

Remove Top Links in Magento

Just comment the top link block in the layout file

My Account
/app/design/frontend/Your-Interface/Your-Theme/layout/customer.xml

My cart and Checkout
/app/design/frontend/Your-Interface/Your-Theme/layout/checkout.xml

My Wishlist
/app/design/frontend/Your-Interface/Your-Theme/layout/wishlist.xml

To Display The Currency In Frontend in Magento

Currency In Dropdown format

In page.xml add the Block if not exist

<block type=”directory/currency” name=”currency” as=”currency” template=”directory/currency.phtml”/>

Template File

/app/design/frontend/template/Your_Interface/YOUR-TEMPLATE-NAME/template/directory/currency.phtml

To get the Currency Symbol in front end

<?php if($this->getCurrencyCount()>1): ?>

<?php foreach ($this->getCurrencies() as $_code => $_name): ?>

<?php echo Mage::app()->getLocale()->currency($_code)->getSymbol();?>

<?php endforeach; ?>

<?php endif; ?>

How to create Magento Module

In this tutorial, we will introduce the coding of Magento in the form of a “Hello World”-style module. The goal of the module will be simply to write some information to a log file every time a product is saved. This very basic module will allow us to cover a number of interesting topics, including:
The app/code directories,
The structure and creation of a Magento module,
Event observers,
Logging.
Before We Begin

This tutorial assumes that you already have an installation of Magento up and running, either locally or on a development server, that you can add new files to. The version of Magento that you use doesn’t really matter, because we will be covering fundamental aspects that exist across all versions and editions: Community, Professional and Enterprise.
Disable the Cache

This is one of the first lessons a Magento developer should learn: disable the cache! You can do this by going to Admin Panel > System > Cache Management > Select All > Actions: Disable > Submit.
While very good at boosting performance in a production environment, the cache is a developer’s enemy. Leave it enabled at your peril! Every Magento developer I have met has on more than one occasion spent an hour or so wondering why their latest update is not showing up, only to find that Magento is still displaying the version of the website that it conveniently cached earlier that day.
The app/code Directory

How to create Magento mobile site

Recent studies have shown that in the near future (by 2014) more people will use mobile devices ( iPod, iPhone, Tablet …) rather than computers, which can be a problem for the advancement of your business. Magento offers you an ideal solution how to overcome that problem. This tutorial will help you to install a mobile theme on your web site.
You can quickly and very easily create a storefront, which is suitable for mobile devices, so users can shop even when they aren´t near their computers.
Magento 1.7  uses HTML5 technology, which is supported by iPhone , Android, Opera Mini, iPod, Blackberry and many more.
HTML5 technology includes many out-of-the-box features, which give you many advantages such as:
• Specific video and audio media capabilities
• Simple search and results display
• Your display will be clean of product detail pages
• Swiping product images is very easy
• You can zoom in and zoom out
• Cross-sell and up-sell capabilities
• Drag-and-drop capabilities
If you want to activate the HTML5 mobile theme for Magento 1.7, you need to follow these instructions. There are only four steps in how to setup a mobile theme on your web site.