Browsing articles tagged with "order Archives - Kalpesh Mehta"
Aug 11, 2013
kalpesh

Magento how to remove order

First of all, removing live orders is not recommended. But if you are sure you want to remove orders (test orders?) then you can do so by custom script below. Get the order increment id(s) of the order you wish to delete from Magento. Remember, once order is deleted you can’t get any related information of that order.

Create a test PHP file in your Magento project root with below code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
require 'app/Mage.php';
Mage::app('admin')->setUseSessionInUrl(false);                                                                                      

$orderIncrementIDs = array('100000001','100000002'); //your order increment ids to delete, beware!
$rmvd = array();

foreach($orderIncrementIDs as $ordID){
    try{
        Mage::getModel('sales/order')->loadByIncrementId($ordID)->delete();
        $rmvd[] = $ordID;
    } catch(Exception $e){
        echo 'Error: '.$e->getMessage().'<br />';
    }
}
echo "Following Orders Removed!<br />" . implode(", ",$rmvd);

and run the file to delete the order(s).
Continue reading »

Jul 12, 2013
kalpesh

Magento convert quote item to order item

If you have custom product attribute(s), then you may need to convert them to quote item and then to order item in Magento when an order is placed. That helps the order get the product attributes details and saves it in order tables to reference in the backend Manage Orders screen. This allows you to know what values for the product is selected by the customer, so that you can consider it when dispatching the order items.

This is a two way process where first the product attribute converts to quote item, and then quote item (with your product attribute’s value) converts to order item.

To do this you will have to put the below code in your module’s config.xml file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<global>
    <!--convert your custom product attribute "myattribute" from quote item to order item-->
    <fieldsets>
        <sales_convert_quote_item>
            <myattribute>
                <to_order_item>*</to_order_item>
            </myattribute>
        </sales_convert_quote_item>
    </fieldsets>

    <!--add your custom product attribute "myattribute" to quote item-->
    <sales>
        <quote>
            <item>
                <product_attributes>
                    <myattribute></myattribute>
                </product_attributes>
            </item>
        </quote>
    </sales>

    <!--convert product attribute to quote item through event observer-->
    <events>
        <sales_quote_item_set_product>
            <observers>
                <mymodule>
                    <class>mymodule/observer</class>
                    <method>convertAttribute</method>
                </mymodule>
            </observers>
        </sales_quote_item_set_product>
    </events>
</global>

Continue reading »

May 10, 2013
kalpesh

Magento add attribute to order

Adding custom attribute to order in Magento is same as we do for customer and category. The difference is we will use different setup class AND we will not need attribute set, group and attribute input type now. We will create a quick module which will do exactly what we want and nothing more than that. So let’s start our new module.

1.) Create a file at app/etc/modules/ and name it whatever you want. I will name it Namespace_Module.xml
Paste this code in that file:

1
2
3
4
5
6
7
8
9
< ?xml version="1.0"?>
<config>
    <modules>
        <namespace_module>
            <active>true</active>
            <codepool>local</codepool>
        </namespace_module>
    </modules>
</config>

2.) Create necessary directories to reach to app/code/local/Namespace/Module/etc/, so that we can create our config.xml there. Paste below code in this config file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
< ?xml version="1.0"?>
<config>
    <modules>
        <namespace_module>
            <version>0.0.1</version>
        </namespace_module>
    </modules>
 
    <global>
        <resources>
            <modulename_setup>
                <setup>
                    <module>Namespace_Module</module>
                    <class>Mage_Sales_Model_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </modulename_setup>
            <modulename_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </modulename_write>
            <modulename_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </modulename_read>
        </resources>
    </global>
</config>

Continue reading »

Apr 28, 2013
kalpesh

Magento get all invoices and shipments of an order

Getting all the invoices of an order:

1
2
3
4
5
6
7
8
$order = Mage::getModel('sales/order')->load($orderID);
if ($order->hasInvoices()) {
    $invIncrementIDs = array();
    foreach ($order->getInvoiceCollection() as $inv) {
        $invIncrementIDs[] = $inv->getIncrementId();
    //other invoice details...
    } Mage::log($invIncrementIDs);
}

Getting all the shipments of an order:

1
2
3
4
5
$order = Mage::getModel('sales/order')->load($orderID);
foreach($order->getShipmentsCollection() as $shipment)
{
    Mage::log($shipment->getData()); //get each shipment data here...
}
Apr 28, 2013
kalpesh

Magento join EAV collection with Flat table

Joining tables in Magento when it comes to EAV with Flat table is quite complicated. Consider you want to join sales_flat_order table with customer EAV tables to get Customer’s firstname and lastname, it becomes difficult as customer’s name comes from customer_entity_varchar table.

Below code will join sales order flat table with customer EAV to get customer’s full name in the collection along with all the order details.

1
2
3
4
5
6
7
8
9
10
11
12
13
$coll = Mage::getModel('sales/order')->getCollection();

$fn = Mage::getModel('eav/entity_attribute')->loadByCode('1', 'firstname');
$ln = Mage::getModel('eav/entity_attribute')->loadByCode('1', 'lastname');

$coll->getSelect()
    ->join(array('ce1' => 'customer_entity_varchar'), 'ce1.entity_id=main_table.customer_id', array('firstname' => 'value'))
    ->where('ce1.attribute_id='.$fn->getAttributeId()) 
    ->join(array('ce2' => 'customer_entity_varchar'), 'ce2.entity_id=main_table.customer_id', array('lastname' => 'value'))
    ->where('ce2.attribute_id='.$ln->getAttributeId()) 
    ->columns(new Zend_Db_Expr("CONCAT(`ce1`.`value`, ' ',`ce2`.`value`) AS fullname"));

print_r($coll->getData());
Pages:12»

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications