I'm trying to add a few custom columns to the Magento sales order create customer grid (where you either select for a customer or add a new customer prior to adding an order in Magento Admin).

I've found a few examples on how to extend the standard customers grid which I have used to create what I have so far, but I'm stuck as nothing seems to be appearing on the grid itself.

/app/code/local/Asapsupplies/Newordergrid/etc/config.xml

<?xml version="1.0"?>
<config>
<modules>
    <Asapsupplies_Newordergrid>
        <version>1.0.0</version>
    </Asapsupplies_Newordergrid>
</modules>
<global>
    <models>
        <asapsupplies_newordergrid>
            <class>Asapsupplies_Newordergrid_Model_Adminhtml</class>
        </asapsupplies_newordergrid>
    </models>
</global>
<adminhtml>
    <events>
        <core_block_abstract_to_html_before>
            <observers>
                <customgrid_column_append>
                    <type>model</type>
                       <class>Asapsupplies_newordergrid_Model_Adminhtml_Observer</class>
                    <method>beforeBlockToHtml</method>
                </customgrid_column_append>
            </observers>
        </core_block_abstract_to_html_before>
        <eav_collection_abstract_load_before>
            <observers>
                <customgrid_collection_append>
                    <class>Asapsupplies_Newordergrid_Model_Adminhtml_Observer</class>
                    <method>beforeCollectionLoad</method>
                </customgrid_collection_append>
            </observers>
        </eav_collection_abstract_load_before>
    </events>
</adminhtml>
</config>

/app/code/local/Asapsupplies/Newordergrid/Model/Adminhtml/Observer.php

<?php
class Asapsupplies_Newordergrid_Model_Adminhtml_Observer
{
/**
 * Adds columns to admin create new order customers grid
 *
 * @param Varien_Event_Observer $observer
 * @return Asapsupplies_Newordergrid_Model_Adminhtml_Observer
 */
public function beforeBlockToHtml(Varien_Event_Observer $observer)
{
    $grid = $observer->getBlock();
    /**
     * Mage_Adminhtml_Block_Sales_Order_Create_Customer_Grid
     */
    if ($grid instanceof Mage_Adminhtml_Block_Sales_Order_Create_Customer_Grid) {
        $grid->addColumnAfter('account_code', array(
            'header'    => 'Account Code',
            'type'      => 'text',
            'index'     => 'account_code',
        ), 'website_id');

        $grid->addColumnAfter('company_name', array(
            'header'    => 'Company Name',
            'type'      => 'text',
            'index'     => 'company_name',
        ), 'account_code');
    }
}   
/**
 * Adds Custom Attributes to the create new order customer grid collection
 *
 * @param Varien_Event_Observer $observer
 * @return Asapsupplies_Newordergrid_Model_Adminhtml_Observer
 */
public function beforeCollectionLoad(Varien_Event_Observer $observer)
{
    $collection = $observer->getCollection();
    if (!isset($collection)) {
        return;
    }
    /**
     * Mage_Customer_Model_Resource_Customer_Collection
     */
    if ($collection instanceof Mage_Customer_Model_Resource_Customer_Collection) {
        /* @var $collection Mage_Customer_Model_Resource_Customer_Collection */
        $collection->addAttributeToSelect('account_code')->addAttributeToSelect('company_name')->addAttributeToSelect('cost_centre_code');
    }
}
Mage::log('Observer function beforeBlockToHtml called', null, 'mylog.log');
}

/app/etc/modules/Asapsupplies_Newordergrid.xml

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

If you notice something missing or any idea why this isn't updating the grid that would be great.

EDIT

Following the answers below the original issue has been resolved, however with the new module in place it's now not loading my previous edits to the customers grid.

/app/code/local/Asap/CustomerGridAdditions/etc/config.xml

    <?xml version="1.0"?>
    <config>
<modules>
    <Asap_CustomerGridAdditions>
        <version>1.0.0</version>
    </Asap_CustomerGridAdditions>
</modules>
<global>
    <models>
        <asap_customergridadditions>
            <class>Asap_CustomerGridAdditions_Model</class>
        </asap_customergridadditions>
    </models>
</global>
<adminhtml>
    <events>
        <core_block_abstract_prepare_layout_before>
            <observers>
                <customgrid_column_append>
                    <type>model</type>
                    <class>Asap_CustomerGridAdditions_Model_Observer</class>
                    <method>beforeBlockToHtml</method>
                </customgrid_column_append>
            </observers>
        </core_block_abstract_prepare_layout_before>
        <eav_collection_abstract_load_before>
            <observers>
                <customgrid_collection_append>
                    <class>Asap_CustomerGridAdditions_Model_Observer</class>
                    <method>beforeCollectionLoad</method>
                </customgrid_collection_append>
            </observers>
        </eav_collection_abstract_load_before>
    </events>
</adminhtml>
    </config>

/app/code/local/Asap/CustomerGridAdditions/Model/Observer.php

<?php

class Asap_CustomerGridAdditions_Model_Observer
{

/**
 * Adds column to admin customers grid
 *
 * @param Varien_Event_Observer $observer
 * @return Asap_CustomerGridAdditions_Model_Observer
 */

public function beforeBlockToHtml(Varien_Event_Observer $observer)
{
    $grid = $observer->getBlock();

    /**
     * Mage_Adminhtml_Block_Customer_Grid
     */
    if ($grid instanceof Mage_Adminhtml_Block_Customer_Grid) {
        $grid->addColumnAfter('account_code', array(
            'header'    => 'Account Code',
            'type'      => 'text',
            'index'     => 'account_code',
        ), 'website_id');

        $grid->addColumnAfter('company_name', array(
            'header'    => 'Company Name',
            'type'      => 'text',
            'index'     => 'company_name',
        ), 'account_code');
    }
}

/**
 * Adds Custom Attributes to the customer grid collection
 *
 * @param Varien_Event_Observer $observer
 * @return Asap_CustomerGridAdditions_Model_Observer
 */

public function beforeCollectionLoad(Varien_Event_Observer $observer)
{
    $collection = $observer->getCollection();
    if (!isset($collection)) {
        return;
    }

    /**
     * Mage_Customer_Model_Resource_Customer_Collection
     */
    if ($collection instanceof Mage_Customer_Model_Resource_Customer_Collection) {
        /* @var $collection Mage_Customer_Model_Resource_Customer_Collection */
        $collection->addAttributeToSelect('account_code')->addAttributeToSelect('company_name');
    }
}
}

/app/etc/modules/Asap_CustomerGridAdditions.xml

<?xml version="1.0"?>
<config>
<modules>
    <Asap_CustomerGridAdditions>
        <active>true</active>
        <codePool>local</codePool>
        <depends></depends>
    </Asap_CustomerGridAdditions>
</modules>
</config>
有帮助吗?

解决方案

I have tried your code on my local machine and its working. I can see columns in my Create order grid. Please review attached screenshot. enter image description here The only thing I can see in your code which is missing is the closing config tag </config> in both of your config.xml as well as in app/etc/modules/Grids_NewOrderGrid.xml
Please add those and check again. If you still can't see the columns, refer to the answer here

EDIT

There is also another thing you can do here, Put your Observer file in app/code/local/Grids/NewOrderGrid/Model/Adminhtml/Observer.php
Update your observer class with name Grids_NewOrderGrid_Model_Adminhtml_Observer and check if its working.
You can add a log entry in your observer function with Mage::log('Observer function beforeBlockToHtml called', null, 'mylog.log'); to check if your observer is being called.



EDIT

Please update your module name in config.xml as Asapsupplies_Newordergrid
Update your model class to Asapsupplies_Newordergrid_Model
In event core_block_abstract_to_html_before update class to Asapsupplies_Newordergrid_Model_Adminhtml_Observer
Now go to System > configuration > Advanced and check if you can see your module and its status is Enabled.
Clear your cache and then check.

许可以下: CC-BY-SA归因
scroll top