Archive | June, 2013

Magento update order status and order tags programmatically

28 Jun

Hello Friends,

TGIF (Thank God Its Friday) but it was not at all  thank full for me, When I was told to update the order tags in magento admin. Yes right I am taking about the order_tag associated with every order status in magento.

Here is the piece of code which did the trick for me.

$order = Mage::getModel(‘sales/order’)->loadByIncrementId($oid);
// Here in place of $oid, use any order id which you want to update. if you have a list of order ids, then iterate it through foreach loop.

$order->setData(‘order_tag’, “Test”);
// The function setData passes the test tag that we are trying to update.

$order->save(); //It save the data to magento

Magento update order status programmatically:-

$order = Mage::getModel(‘sales/order’)->loadByIncrementId($oid);
$state = ‘new’;
$status = ‘any_order_status_comes_here’;
$comment = ‘Write you comment here’;
$isCustomerNotified = false;
//If you choose to set $isCustomerNotified then it would make the neccessary notification to customer else put it false.
$order->setState($state, $status, $comment, $isCustomerNotified);
$order->save();
$order->sendOrderUpdateEmail(true, $comment); //Send email to customer. If not reuqired leave this line.

Imp Note:- Magento doesn’t allow setState for complete and closed.

Do reply if you need any help or if it is not working for you. You can also reach me by email:- mukund002(at)gmail(com)

Responsive Design

27 Jun

Hi All, You might have came across lots of responsive website and thought to make even your website responsive.
There are lots of benifts of having a responsive site but thats not our topic for today. We will discuss it later.
Lets start with core of any Responsive Web Design framework,i.e the media query.

responsive_web_design-570x345

What is a Media Query?

>>A media query is a CSS declaration that is used as a parameter for when to call other style declarations based on the dimensions of the current viewing device.

Ways of calling a Media Query:-

Like CSS we can call a media query in two ways. External and Internal call. Lets see an example of both.

External Call:-
<link rel=”stylesheet” media=”screen and (min-width:320px) and (max-width:480px)” href=”css/mobile.css” />

Internal Call or CSS direct call:-

@media screen{

body{background: #ff0000;}

}

Both examples of two different ways to make CSS declarations that only are to be called when the viewing device is between 320px and 480px.

Width Parameter:-

If you’ve been following along you’ll realize that @media screen is what starts our query, and the min and max width attributes set the parameters. So with this in mind, you know how to call different style declarations for different viewing widths.Sometime a UI developer uses both device with and width.

Lets see the difference:-

The difference between these two here is that device width is going by the set width of your device, with any zoom type changes being ignored.

You can check example here to understand. Click Here

So far we have discussed about the syntax now its time to move to conditional things, the logical one:-

Logical Operator:-

Complex media queries using logical operators, including not, and, and only. The AND operator is used to combine multiple media query into a single one.

@media (min-width: 700px) and (orientation: landscape) { … }

The above media query will display only if minimum width is 700px and orientation is landscape.

We can also use comma separated logical operator like in below example:-

@media (min-width: 700px), handheld and (orientation: landscape) { … }

I hope this tutorial make sense and follow me on this blog regularly to get some more like this.

References:- https://developer.mozilla.org

Overriding controller class in magento

17 Jun
Sometime we have to run in the situation where we need to overwrite the core module. Note if you make changes in the core file then it will trouble you at the time of up-gradation.So I came up with the new blog which gives you the idea about how to override controllers in magento.Now suppose we want to override the checkout/cart controller. In this case, we will have to edit just two files in our custom module.
Suppose our custom module is present in the namespace ‘Donebynone‘ and our module is named as ‘Mukund‘.

First Step:-

Open the app\code\local\Donebynone\Mukund\etc\config.xml and edit it as follows:
The name of the core module to be overridden is written in between the router tags. Here we want to override the checkout module so checkout is to be wrapped in <routers> tags.

After this we will tell Magento to call our custom module before the Mage/Checkout module.

<?xml version=”1.0″ encoding=”UTF-8″?>
<config>
<modules>
<Donebynone_Mukund>
<version>0.0.1</version>
</Donebynone_Mukund>
</modules>
<frontend>
<routers>
<checkout><!– Name of core module to be overridden  –>
<args>
<modules>
<Donebynone_Mukund before=”Mage_Checkout”>Donebynone_Mukund</Donebynone_Mukund>
<!– Above line will tell Magento to call our custom module before the Mage/Checkout module –>
</modules>
</args>
</checkout>
</routers>
</frontend>
</config>

Final Step:-

Now we need to create the controller file to be overridden, CartController.php in our case.
(app\code\local\Donebynone\Mukund\controllers\CartController.php)

<?php

/*
Include the core file to be overridden
*/
require_once(“Mage/Checkout/controllers/CartController.php”);

/*
* Extend the core controller in our custom controller.
*/
class Donebynone_Mukund_CartController extends Mage_Checkout_CartController
{
//Write your fucntion here..

public function indexAction()
{
echo “This controller has been overridden.”;
}
}

?>

Now its your turn to do the job…go ahead with confidence.

Need any help on this….write your comments…

Custom Module in Magento

15 Jun
Hi Freinds,

Today I am posting about custom module in magento. Hope it will be fruitful.


Creating a new module in magento

Structure of sample file:
[root]\app\code\local\{Namespace}\{Modulename}
[root]\app\code\local\{Namespace}\{Modulename}\controllers
[root]\app\code\local\{Namespace}\{Modulename}\etc
[root]\app\code\local\{Namespace}\{Modulename}\etc\config.xml
[root]\app\code\local\{Namespace}\{Modulename}\Helper
[root]\app\code\local\{Namespace}\{Modulename}\Helper\Data.php
[root]\app\code\local\{Namespace}\{Modulename}\Model
[root]\app\code\local\{Namespace}\{Modulename}\{Modulename}.php

{Namespace} is a user defined variable. 
{Modulename} this is the name of your module.


For Example I am using Donebynone as the Namespace and Helloworld as my module name

First using controllers…all controllers goes here and name this file as it suits you .Below is controller code…just for example

####################################################################################################

<?php
//IndexController is the default controller
class Donebynone_Helloworld_IndexController extends Mage_Core_Controller_Front_Action {
//indexAction is the default Action for any controller
function indexAction() {
echo “indexAction”;
$helloworld = Mage::getModel(“donebynone_helloworld/helloworld”);
$helloworld->helloworld(“helloworld”);
}
}


####################################################################################################


Data.php is just a helper file for the Magento , I’m not exactly sure what it does but it appears to be exactly the same in every module except for its class definition.

###################################################################################################

<?php
class Donebynone_Helloworld_Helper_Data extends Mage_Core_Helper_Abstract {
}


###################################################################################################

Coming to the Model. It holds all your module.In Magento core you’ll notice that most modules contain many models.

###################################################################################################

<?php
class Donebynone_Helloworld_Model_Helloworld extends Varien_Object {
function __construct() {
parent::__construct();
}
function helloworld($arg) {
echo “<br>Hello World! My argument is : ” . $arg;
}
}

###################################################################################################

Next is important one…config.xml  where you connect all your code up to Magento.

####################################################################################################

<?xml version=”1.0″ encoding=”UTF-8″ ?>
<config>
<modules>
<Donebynone_Helloworld>
<version>0.1.0</version>
</Donebynone_Helloworld>
</modules>
<frontend>
<routers>
<Donebynone>    <!– I make this match my front name but I’m not sure it matters –>
<use>standard</use>    <!– Use standard routing as opposed to admin.  IE: frontend vs admin –>
<args>
<module>donebynone_Helloworld</module>    <!– The module to search for controllers –>
<frontName>donebynone</frontName>    <!– The first descriminator in the path.  “donebynone” in http://localhost/donebynone/ –>
</args>
</donebynone>
</routers>
</frontend>
<global>
<models>
<donebynone_helloworld>    <!– This is used when istanting your Model EG: Mage::getModel(“donebynone_helloworld/hellworld”) –>
<class>donebynone_Helloworld_Model</class>    <!– That class to use when isntanting objects of type above. –>
</donebynone_helloworld>
</models>
</global>
</config>

####################################################################################################


Now Finally [root]\app\etc\modules\{Namespace}_{Modulename}.xml in our case it is Donebynone_Helloworld.xml. This file basically tells magento that there is a new module and you have to load it.

#########################################################################################################

<?xml version=”1.0″?>
<config>
<modules>
<Donebynone_Helloworld>
<active>true</active>
<codePool>local</codePool>
</Donebynone_Helloworld>
</modules>
</config>

#########################################################################################################


Thanks no more hard work just go to  Admin->System -> Configuration -> Advanced and you should see your module enabled here.

Dont forget to flush your magento cache.

You are done!!

Also check this for reference purpose
(http://www.magentocommerce.com/wiki/custom_module_with_custom_database_table)