0

I get some data and save it to the sales_flat_order and sales_flat_order_address . My issue is that I can't see my order created on the Magento grid order. Which data I have to fill or add to see my order into the grid?

This is the attribute I fill in both tables.

foreach($array as $value){
    $array = array(
        'status' => $value['status'],
        'base_grand_total' => $value['price'],
        'grand_total' => $value['price'],
        'increment_id' => $value['number'],
        'base_currency_code' => 'MXN',
        'customer_email' => $value['email'],
        'customer_firstname' => $value['name'],
        'customer_lastname' => $value['apellido'],
        'global_currency_code' => 'MXN',
        'order_currency_code' => 'MXN',
        'store_name' => 'linio marketplace',
        'created_at' => $value['created'],
        'updated_at' => $value['created'],
        'total_item_count' => $value['item']
    );

    $modele = Mage::getModel('thorleif/order')->setData($array);
    $modele->save();

    $id = $modele->getId();
    $array = array(
        'parent_id' => $id,
        'region' => $value['region'],
        'postcode' => $value['zip'],
        'street' => $value['address'].'.'.$value['country'],
        'city' => $value['city'],
        'email' => $value['email'],
        'telephone' => $value['tel'],
        'firstname' => $value['name'],
        'lastname' => $value['apellido'],
        'address_type' => 'shipping'
    );

    $modele = Mage::getModel('thorleif/address')->setData($array);
    $modele->save();
    $id = $modele->getId();
}
0

The sales/order grid uses the table sales_flat_order_grid to display its information, this is done to list all orders without adding joins and speeding-up the loading. In order to add a column to the grid you would need to add a field to this table and update the information in that field when an order is created/updated. Below the steps needed to add this to the grid:

I called the extension Example_SalesGrid, but you can change it to your own needs.

Let's start by creating the module init xml in /app/etc/modules/Example_SalesGrid.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--
 Module bootstrap file
-->
<config>
    <modules>
        <Example_SalesGrid>
            <active>true</active>
            <codePool>community</codePool>
            <depends>
                <Mage_Sales />
            </depends>
        </Example_SalesGrid>
    </modules>
</config>

Next we create our module config xml in /app/code/community/Example/SalesGrid/etc/config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Example_SalesGrid>
            <version>0.1.0</version> <!-- define version for sql upgrade -->
        </Example_SalesGrid>
    </modules>
    <global>
        <models>
            <example_salesgrid>
                <class>Example_SalesGrid_Model</class>
            </example_salesgrid>
        </models>
        <blocks>
            <example_salesgrid>
                <class>Example_SalesGrid_Block</class>
            </example_salesgrid>
        </blocks>
        <events>
            <!-- Add observer configuration -->
            <sales_order_resource_init_virtual_grid_columns>
                <observers>
                    <example_salesgrid>
                        <model>example_salesgrid/observer</model>
                        <method>addColumnToResource</method>
                    </example_salesgrid>
                </observers>
            </sales_order_resource_init_virtual_grid_columns>
        </events>
        <resources>
            <!-- initialize sql upgrade setup -->
            <example_salesgrid_setup>
                <setup>
                    <module>Example_SalesGrid</module>
                    <class>Mage_Sales_Model_Mysql4_Setup</class>
                </setup>
            </example_salesgrid_setup>
        </resources>
    </global>
    <adminhtml>
        <layout>
            <!-- layout upgrade configuration -->
            <updates>
                <example_salesgrid>
                    <file>example/salesgrid.xml</file>
                </example_salesgrid>
            </updates>
        </layout>
    </adminhtml>
</config>

Now we create the sql upgrade script in /app/code/community/Example/SalesGrid/sql/example_salesgrid_setup/install-0.1.0.php:

<?php
/**
 * Setup scripts, add new column and fulfills
 * its values to existing rows
 *
 */
$this->startSetup();
// Add column to grid table

$this->getConnection()->addColumn(
    $this->getTable('sales/order_grid'),
    'customer_city',
    'varchar(255) DEFAULT NULL'
);

// Add key to table for this field,
// it will improve the speed of searching & sorting by the field
$this->getConnection()->addKey(
    $this->getTable('sales/order_grid'),
    'customer_city',
    'customer_city'
);

// Now you need to fullfill existing rows with data from address table

$select = $this->getConnection()->select();
$select->join(
    array('address'=>$this->getTable('sales/order_address')),
    $this->getConnection()->quoteInto(
        'address.parent_id = order_grid.entity_id AND address.address_type = ?',
        Mage_Sales_Model_Quote_Address::TYPE_BILLING
    ),
    array('customer_city' => 'city')
);
$this->getConnection()->query(
    $select->crossUpdateFromSelect(
        array('order_grid' => $this->getTable('sales/order_grid'))
    )
);

$this->endSetup();

Next we create the layout update file in /app/design/adminhtml/default/default/layout/example/salesgrid.xml:

<?xml version="1.0"?>
<layout>
    <!-- main layout definition that adds the column -->
    <add_order_grid_column_handle>
        <reference name="sales_order.grid">
            <action method="addColumnAfter">
                <columnId>customer_city</columnId>
                <arguments module="sales" translate="header">
                    <header>Customer City</header>
                    <index>customer_city</index>
                    <type>text</type>
                    <width>200</width>
                </arguments>
                <after>shipping_name</after>
            </action>
        </reference>
    </add_order_grid_column_handle>
    <!-- order grid action -->
    <adminhtml_sales_order_grid>
        <!-- apply the layout handle defined above -->
        <update handle="add_order_grid_column_handle" />
    </adminhtml_sales_order_grid>
    <!-- order grid view action -->
    <adminhtml_sales_order_index>
        <!-- apply the layout handle defined above -->
        <update handle="add_order_grid_column_handle" />
    </adminhtml_sales_order_index>
</layout>

The last file needed is needed to update the data from a table other than sales/order (sales_flat_order). All fields in sales/order_grid matching the column name from sales/order is automatically updated in the sales/order_grid table.

/app/code/community/Example/SalesGrid/Model/Observer.php:

<?php
/**
 * Event observer model
 *
 *
 */
class Example_SalesGrid_Model_Observer {

    public function addColumnToResource(Varien_Event_Observer $observer) {
        // Only needed if you use a table other than sales/order (sales_flat_order)

        $resource = $observer->getEvent()->getResource();
        $resource->addVirtualGridColumn(
            'customer_city',
            'sales/order_address',
            array('billing_address_id' => 'entity_id'),
            'city'
        );
    }
}
  • No credits to magento.stackexchange.com/questions/4191/…? – sv3n Jul 14 '17 at 23:05
  • @sv3n please, I don't need you to comment my posts. He asked a question I answered him no matter the source, if you have a better answer the keyboard is in front of you. You are not the moderator of SE and sincerely there are many interesting things in life to do more better than leaving comments in people posts. So please do not leave me more comments Thank you. – PЯINCƏ Jul 14 '17 at 23:25
  • I guess it's not required to be a moderator to express some incomprehension. If I copy someone elses work, I leave a comment at least - as usual. – sv3n Jul 14 '17 at 23:33
  • I would like to invite you to chat, but i dont know how, so ... this was not offend! This is a community driven platform, so credits should go to authors first. I even had upvoted your answer if there would be a link to origin source! Please remember, you've downvoted a correct answer of me, after I downvoted your incorrect answer ... why? This makes no sense. At the other hand I've upvoted some of you answer for being correct and helpful. Just want to ask ... do not copy&paste already given answers with minimal improvement? Leave some credits? Stay fair? Thanks. Regards. – sv3n Jul 17 '17 at 0:03
  • Dear @sv3n, I am here in SE to help people and clench my knowledge and skills and in no case bickering with people. Regarding this answer I didn't added the ref because simply I forgot it, I invite you to look at my other answers I put it every time when it's necessary.Regarding the votes, I upvoted also for you without you knowing it because I dont left comment. And look on this question http://urlz.fr/5A9J you answered me with a wrong answer Fatal error and I don't downvoted your vote you see ? So sorry if I vexed you, it was not my goal. take my skype if you ever need me:amirmiroooo – PЯINCƏ Jul 17 '17 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.