Ads

Tuesday, June 22, 2010

Override core block in magento

The simplest way to override a core block is to simply copy the file into the corresponding /app/code/local/ folder, and Magento automatically uses that version of the file without an XML/config changes needed.

So just copy /app/code/core/Mage/Checkout/Block/Cart/Item/Renderer.php into /app/code/local/Mage/Checkout/Block/Cart/Item/ and be sure to refresh the cache.

Magento: Redirect functions

The redirect functions are present in Mage_Core_Controller_Varien_Action class.

/* Redirect to certain url */
_redirectUrl($url)

/* Redirect to certain path */
_redirect($path, $arguments=array())

/* Redirect to success page */
_redirectSuccess($defaultUrl)

/* Redirect to error page */
_redirectError($defaultUrl)

/* Set referer url for redirect in response */
_redirectReferer($defaultUrl=null)

/* Identify referer url via all accepted methods (HTTP_REFERER, regular or base64-encoded request param) */
_getRefererUrl()

/* Check url to be used as internal */
_isUrlInternal($url)

Create custom module in magento

1) Create directory app\code\local\mymodulepack. Here ‘mymodulepack’ will be the new directory folder where you can create modules.
2) Create module named ‘MyModule’ in above defined directory path. This will be your module directory which will contain required files like models, blocks, controllers etc.
3) Now add your new module xml file in app/etc/modules/ mymodulepack _All.xml. Consider that _All.xml will be used to add all new module definitions to the same file instead of cluttering the etc/modules folder with many files.
4) Following defined code is used to inform Magento that there is an active module (you can turn it off from here by setting ‘active’ to false). Also that it is located in the ‘local’ code pool.
<?xml version="1.0"?>
<config>
<modules>
<mymodulepack_MyModule>
<active>true</active>
<codePool>local</codePool>
</mymodulepack_MyModule>
</modules>
</config>
5) Now you can enable or disable this module at Admin -> System -> Advanced. Here you can see your new module ‘Mymodulepack_MyModule’ listed there with status enable.
Note : Please disable the cache from admin section to view the module.
6) And now create the block php class file at app/code/local/mymodulepack/MyModule/Block/Myview.php which contains a function that returns the string ‘Hello World’.
<?php
class mymodulepack_MyModule_Block_Myview extends Mage_Core_Block_Template
{
public function __construct()
{
parent::__construct();
$this->setTemplate('mymodule/myview.phtml');
}
protected function _prepareLayout()
{
parent::_prepareLayout();
}
}
?>
7) Now make a config.xml file in app\code\local\mymodulepack\MyModule\etc directory. This is done to enter configuration information for the module.
<?xml version="1.0" encoding="iso-8859-1"?>
<config>
<modules>
<mymodulepack_MyModule>
<version>0.1.0</version>
</mymodulepack_MyModule>
</modules>
<blocks>
<MyModule>
<rewrite>
<MyModule>mymodulepack_MyModule_Block_Myview</MyModule>
</rewrite>
</MyModule>
</blocks>
<frontend>
<routers>
<MyModule>
<use>standard</use>
<args>
<module>mymodulepack_MyModule</module>
<frontName>MyModule</frontName>
</args>
</MyModule>
</routers>
<layout>
<updates>
<MyModule>
<file>MyModule.xml</file>
</MyModule>
</updates>
</layout>
</frontend>
<global>
<helpers>
<extras>
<class>mymodulepack_MyModule_Helper_Data</class>
</extras>
</helpers>
</global>
</config>
8) Create the php class file at app/code/local/mymodulepack/MyModule/ Helper / Data.php
<?PHP
class mymodulepack_MyModule_Helper_Data extends Mage_Core_Helper_Abstract {
}
?>
9) Now we need to create a template file which uses the View Block Class to display the message “Hello World”. Create a template (phtml) called myview.phtml in the following directory:
app/design/frontend/default/your theme/template/mymodule/myview.phtml.
<div>
<span style=" color:#0000FF; font:Arial, Helvetica, sans-serif;">
<bold><?php
echo "Hello World";
?></bold>
</span>
</div>
10)The module is now ready for use.
Where do I put this block? Should I create new mymodule.xml in theme/layout?
You can view the output by two ways.
1) {{block type=”mymodulepack_mymodule/view” template=”mymodule/myview.phtml” }}
call this block in page.xml file
2) Create xml file in app/design/frontend/default/your theme/layout/MyModule.xml
<?xml version="1.0"?>
<layout>
<default>
<reference name="content">
<action method="setTemplate"><template>page/2columns-right.phtml</template></action>
</reference>
<reference name="content">
<block type="core/template" name="mymodule" template="mymodule/myview.phtml" />
</reference>
</default>
</layout>
Note : Set the position of your module as per your requirement,
1. <reference name="right">
2. <reference name="left">
3. <reference name="content">

MVC pattern of Magento files & folder structure

/ app - is where the application resides

/ app / etc - is global configuration den

/ app / code - is where modules have installed their models and controllers

/ app / code / core - are core team developed or certified modules

/ app / code / community - are community contributed modules

/ app / code / local - are local customizations

/ app / code / core / Mage - magento default namespace

/ app / code / core / Mage / {Module} - module root

/ app / code / core / Mage / {Module} / etc - module configuration

/ app / code / core / Mage / {Module} / controllers - controllers provided by module

/ app / code / core / Mage / {Module} / Block - Block logic classes

/ app / code / core / Mage / {Module} / Model - Object Models provided by module

/ app / code / core / Mage / {Module} / Model / Mysql4 - Resource Models provided by module

/ app / code / core / Mage / {Module} / sql - sql installation and upgrade files between module versions

/ app / code / core / Mage / {Module} / sql / {resource} / - resource model specific upgrades

/ app / code / core / Mage / {Module} / sql / {resource} / {type}-{action}-{versions}.(sql|php) - resource update files. example: mysql4-upgrade-0.6.23-0.6.25.sql

/ app / design - is location of design packages (layouts, templates, translations)

/ app / design / frontend - frontend design

/ app / design / adminhtml - HTML admin panel design

/ app / design / {area} / {package} / {theme} - theme customizations

/ app / design / {area} / {package} / {theme} / layout - .xml files that define block structure for different cases in website flow

/ app / design / {area} / {package} / {theme} / template - .phtml (html with php tags) templates

/ app / design / {area} / {package} / {theme} / locale - Zend_Translate compatible translation files for package/theme

/ app / locale - locale files

/ app / locale / {locale (en_US)} - Zend_Translate compatible translation files for modules

/ skin / {area} / {package} / {theme} / - is where design package css and images are

/ lib - are libraries such as Zend and Varien

/ js - are javascripts smile

/ media - uploaded files (product images, pdf documents, etc)

/ tests - Unit tests (not done yet)

/ var - temporary files

/ includes - contains config.php

Getting Ordered Items and their Detail from Order ID in Magento

$order = Mage::getModel('sales/order')->load($order_id);
$items = $order->getAllItems();
$itemcount=count($items);
$name=array();
$unitPrice=array();
$sku=array();
$ids=array();
$qty=array();
foreach ($items as $itemId => $item)
{
$name[] = $item->getName();
$unitPrice[]=$item->getPrice();
$sku[]=$item->getSku();
$ids[]=$item->getProductId();
$qty[]=$item->getQtyToInvoice();
}

Extension for delete orders

By using this extension we can delete only cancel order, so we need to cancel the order before delete.

Link : http://www.magentocommerce.com/magento-connect/Boutik+Circus/extension/873/delete-orders

Get store information

Get store data
Mage::app()->getStore();

Store Id
Mage::app()->getStore()->getStoreId();

Store code
Mage::app()->getStore()->getCode();

Website Id
Mage::app()->getStore()->getWebsiteId();

Store Name
Mage::app()->getStore()->getName();

Is Active
Mage::app()->getStore()->getIsActive();

Store Home Url
Mage::app()->getStore()->getHomeUrl();

SMTP magento extension

Link : http://www.magentocommerce.com/extension/1865

Link :
http://www.aschroder.com/wp-content/uploads/2010/03/Aschroder_SMTPPro-1.0.1.zip

sql order query

SELECT * FROM sales_flat_order_item t inner join sales_order s on s.entity_id =t.order_id where customer_id='1';

Solution of value has to be numeric error in magento

Error : Value '65,000000' has to be numeric.

Solution :

It just appeared, when your locale number format was not similar to the English one.

Reason:
In app/code/core/Mage/Directory/Model/Currency.php(203) you will find the method 'formatTxt()'.
In there is a fix for a problem with formatted price-values coming in.
In line 211 you'll find the following code:

$price = sprintf("%f", $price);

This sprintf-function does not return the type of its content correctly (has to be double/numeric) if this content (a price-value) is formatted in non-english-style (e.g. '0,0000000 instead of 0.0000000).

In app/code/core/Mage/Directory/Model/Currency.php(212) the method Zend_Currency->toCurrency() is called with the output of the sprintf-function.
So, testing this incoming variable in the Zend-Method to be of numeric type causes the error described above.

Solution:
Replace the sprintf-function with the following code:

$price = (float) $price;

Disable modules in Magento [Disable module increases little bit speed of site]

Magento has a huge number of features and there are few businesses that will need to use all of them

If you want to disable some features completely for your site you can do this easily through the administration options.

System > Configuration > Advanced > Disable module output or You can easily enable the module.

Hi friends,

      The blog is for Magento users and developers, contains helpful links and hints for Magento developers.Hints are taken from other helpful sites and created and tested by me as per my practical experience and knowledge of Magento.

    In the OS world, Magento absolutely stands alone in functionality and configuration, but... it is also absolutely not for the inexperienced or impatient because Lack Of Proper Documentation.

   I am trying to document all the essential funcitonalities of magento in this blog. Hope this blog will help you.


Cheers  :)

Get category name and url from product

$product->getCategoryIds() gives array of category
ids which the product is associated to. We can loop through
this array and load each category to get the category name
and url.

getCategoryIds();
/**
* looping through the array of category ids
*/
foreach($categoryIds as $categoryId) {
$category =
Mage::getModel('catalog/category')->load($categoryId);
?>
Category: getCategoryUrl() ?>">getName() ?>