Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

model - Magento custom "order" attribute / admin input and display

I need to add a custom attribute to the order model ("dynamics_ord").

This attr needs to be defined by admin staff, after the order has been successfully saved, ideally on the admin/sales_order/view, in the main order block.

It needs to be independent of invoice / shipment etc - maybe attached to an order "edit" event.

I've made a number of custom attributes for other entities (notably - customers). And I realize this is the same process. But I'm not clear how to go about amending an order with this attribute, as there is no obvious "update" action. "Edit" may make sense, but I'd rather not duplicate orders needlessly.

Once this attr is in place, I want to display it in the sales_order/index grid. (Note = check out this related thread for a working solution.)

Final State of this module (and for anyone else in need of this solution):

/sales_order/view/ screenshot : Screenshot

WACI_SalesExt

app/local/WACI/SalesExt
 /Block
   /Adminhtml
     /Sales
       -Dynamicsord.php
 /controllers
   /Adminhtml
     /Sales
       - OrderController.php
 /etc
  - config.xml
 /Helper
   - Data.php
 /sql
   /waci_salesext_setup
     -mysql4-install-0.2.0.php

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <WACI_SalesExt>
            <version>0.2.0</version>
        </WACI_SalesExt>
    </modules>
    <global>

        <helpers>
            <WACI_SalesExt>
                <class>WACI_SalesExt_Helper</class>
            </WACI_SalesExt>
        </helpers>

        <blocks>
            <WACI_SalesExt>
                <class>WACI_SalesExt_Block</class>
            </WACI_SalesExt>

            <adminhtml> <!-- unclear if i need this second declaration for admin-->
                <WACI_SalesExt>
                    <class>WACI_SalesExt_Block</class>
                </WACI_SalesExt>
            </adminhtml>

        </blocks>

        <models>
            <WACI_SalesExt>
                <class>WACI_SalesExt_Model</class>
                <resourceModel>salesext_mysql4</resourceModel>
            </WACI_SalesExt>

        </models>

        <resources>
            <waci_salesext_setup>
                <setup>
                    <module>WACI_SalesExt</module>
                    <class>Mage_Sales_Model_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </waci_salesext_setup>
            <waci_salesext_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </waci_salesext_write>
            <waci_salesext_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </waci_salesext_read>
        </resources>

    </global>

<admin>
    <routers>
        <adminhtml>
            <use>admin</use>
            <args>
                <modules>
                    <WACI_SalesExt after="Mage_Adminhtml_Sales">WACI_SalesExt_Adminhtml</WACI_SalesExt>
                </modules>
            </args>
        </adminhtml>    
    </routers>
</admin>
</config>

WACI / SalesExt / sql / waci_salesext_setup / mysql4-install-0.2.0.php

    $installer = $this;
    $installer->startSetup();

    $installer->addAttribute('order', 'dynamics_ord', array(
                                'type'              =>'varchar', 
                                'visible'           => true, 
                                'required'          => false, 
                                'is_user_defined'   => false, 
                                'note'              => 'Dynamics ORD')
                            );

$installer->getConnection()->addColumn($installer->getTable('sales_flat_order'), 'dynamics_ord','VARCHAR(255) NULL DEFAULT NULL');
$installer->getConnection()->addColumn($installer->getTable('sales_flat_order_grid'), 'dynamics_ord','VARCHAR(255) NULL DEFAULT NULL');

    $installer->endSetup();

template/sales/order/view/info.phtml

<?php echo $this->getLayout()->createBlock('WACI_SalesExt/adminhtml_sales_dynamicsord')->setOrderId($_order->getId())->toHtml() ?>

template/sales/ord_form.phtml

<?php if ('sales_order' == Mage::app()->getRequest()->getControllerName() ): ?>
<tr>
    <td class="label"><label for="dynamics_ord">Dynamics ORD</label></td>
    <td class="value">
        <form action="<?php echo $this->getFormUrl(); ?>" method="post" id="ord_form">
            <input type="hidden" name="form_key" value="<?php echo $this->getFormKey(); ?>" />
            <input type="hidden" name="token" value="<?php echo $this->getToken(); ?>" />  
            <div class="">
                <input type="text" class="input-text" name="dynamics_ord" value="<?php echo $this->getOrder()->getData('dynamics_ord'); ?>" style="width:150px; float:left; display:inline;" />
                <button onclick="editForm.submit()" style="float:left;margin:3px 10px 0;">Update</button>  
            </div> 
        </form>
        <script type="text/javascript">
            editForm = new varienForm('ord_form');
        </script>
    </td>
</tr>
<?php endif; ?>

WACI_SalesExt_Block_Adminhtml_Sales_Dynamicsord

class WACI_SalesExt_Block_Adminhtml_Sales_Dynamicsord extends Mage_Adminhtml_Block_Template
{
    public function __construct()
    {
        parent::__construct();
        $this->setTemplate('sales/ord_form.phtml');
    }

    public function getOrder(){
        return Mage::registry('current_order');
    }

    public function getFormUrl(){
        return Mage::helper("adminhtml")->getUrl('*/sales_order/editord', array('order_id'=>$this->getOrder()->getId()) );
    }

}

WACI_SalesExt_Adminhtml_Sales_OrderController

include_once Mage::getModuleDir('controllers', 'Mage_Adminhtml') . DS . 'Sales' . DS . 'OrderController.php';

class WACI_SalesExt_Adminhtml_Sales_OrderController extends Mage_Adminhtml_Sales_OrderController
{

    public function editordAction()
    {

        $postData = $this->getRequest()->getPost();

        Mage::log('WACI_SalesExt_Adminhtml_Sales_OrderController::ordEditAction'.print_r($postData, true) );

        $id = $this->getRequest()->getParam('order_id');
        $order = Mage::getModel('sales/order')->load($id);
        $order->setDynamicsOrd($postData['dynamics_ord']);

        $order->save();

        // return to sales_order view
        Mage::app()->getResponse()->setRedirect(Mage::helper('adminhtml')->getUrl("adminhtml/sales_order/view", array('order_id'=> $id)));

    }

}

Been looking around for some info on this issue, and this SO thread seems pretty close to what I want to do. But it's not clear that I'll be able to create an editable input via this workflow.

I'll note that I'm on Mage 1.11 - and I've gathered that the standard EAV pattern is no longer used for the order model.

Cheers -


Update

(note the "current" module contents.)

  • The field has been added to the sales_flat_order
  • WACI_SalesExt_Block_Adminhtml_Sales_Dynamicsord block is getting constructed correctly
  • i have some test data in place that is getting pulled in correctly

At this point, I don't think i have my router set up correctly. In your example, you called <?php echo $this->getEditFormUrl(); ?> . I'm not sure if that is supposed to output a formatted action url, but it wasn't. I dropped my own in, but its still not hitting the router correctly.

Currently, the value in the form action =

   action ="http://my.domain.com/index.php/my_admin/sales_order/editord/key/2e56d7f560dc4d356e3ef9519764b8a84858e5cd40d3c1f5821a423b949b9a6a/"

And just hits a "page not found". As far as I can tell, the router should be accurate here. I've tried a number of different combinations; I'm apparently missing something important on the admin router issue. I understand the key/ee6... is probably messing with me, but I'm not clear how to deal with it appropriately.


Update

Routing problem was simple error I was needlessly making complex: I had a miss-spelled directory name "AdminHtml" vs "Adminhtml" - causing the router to get lost.

Oh Magento...

Thanks @R.S. for teh help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could try adding the 'editable input' to the 'Account Information' section on your order detail page.

In /app/design/adminhtml/default/default/template/sales/order/view/info.phtml

enter image description here

Add

<?php echo $this->getLayout()->createBlock('salesext/adminhtml_editform')->setOrderId($_order->getId())->toHtml() ?>

In your block

<?php

class WACI_SalesExt_Block_Adminhtml_Editform extends Mage_Adminhtml_Block_Template
{
    public function __construct()
    {
        parent::__construct();
       $this->setTemplate('salesext/edit_form.phtml');
    }

    public function getOrder(){
        return Mage::registry('current_order');
    }

   public function getFormUrl(){
       return Mage::helper("adminhtml")->getUrl('*/sales_order/editField', array('order_id'=>$this->getOrder()->getId());
  }
  ...

in /app/design/adminhtml/default/default/template/salesext/edit_form.phtml

<?php if ('sales_order' == Mage::app()->getRequest()->getControllerName() : ?>
<div id="change-order-email" style="display:none">
    <form action="<?php echo $this->getEditFormUrl(); ?>" method="post" id="edit_form">
        <input type="hidden" name="form_key" value="<?php echo $this->getFormKey(); ?>" />
        <input type="hidden" name="token" value="<?php echo $this->getToken(); ?>" />  
        <table cellspacing="4" class="form-list">
            <tr>
                <td>Field</td>
                <td><input .... class="required-entry validate-email" /></td>
            </tr>
        </table>
    </form>
    <button onclick="editForm.submit()">Change</button> 
</div>
<script type="text/javascript">
    editForm = new varienForm('edit_form');
</script>
<?php endif; ?>

In /app/code/local/CWACI/SalesExt/controllers/Adminhtml/Sales/OrderController.php

<?php
include_once Mage::getModuleDir('controllers', 'Mage_Adminhtml') . DS . 'Sales' . DS . 'OrderController.php';

class CWACI_SalesExt_Adminhtml_Sales_OrderController extends Mage_Adminhtml_Sales_OrderController
{

    public function editFieldAction()
    {
        $postData = $this->getRequest()->getPost();
        $id = $this->getRequest()->getParam('order_id');
        $order = Mage::getModel('sales/order')->load($id);
        $order->setFieldName($postData[field_name]);
        .....
        $order->save();

In your config.xml

...
<admin>
    <routers>
        <adminhtml>
            <use>admin</use>
            <args>
                <modules>
                    <CWACI_SalesExt after="Mage_Adminhtml_Sales">CWACI_SalesExt_Adminhtml</CWACI_SalesExt>
                </modules>
            </args>
        </adminhtml>    
    </routers>
</admin>

.....

Also see Setting custom Admin theme from a Module to prevent making modification directly to core templates.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...