Magento 自定义“订单"属性/管理输入和显示 [英] Magento custom "order" attribute / admin input and display

查看:28
本文介绍了Magento 自定义“订单"属性/管理输入和显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要向订单模型(dynamics_ord")添加自定义属性.

在成功保存订单后,该属性需要由管理员定义,最好在主订单块中的 admin/sales_order/view 上.

它需要独立于发票/发货等 - 可能附加到订单edit"事件.

我为其他实体(特别是客户)制作了许多自定义属性.我意识到这是相同的过程.但我不清楚如何修改具有此属性的订单,因为没有明显的更新"操作.编辑"可能有意义,但我宁愿不要不必要地重复订单.

此属性到位后,我想将其显示在sales_order/index 网格中.(注意 = 查看 似乎非常接近我想做的事情.但不清楚我是否能够通过此工作流程创建可编辑的输入.

我会注意到我使用的是 Mage 1.11 - 我发现标准 EAV 模式不再用于订单模型.

干杯 -

<小时>

更新

(注意当前"模块内容.)

  • 该字段已添加到 sales_flat_order
  • WACI_SalesExt_Block_Adminhtml_Sales_Dynamicsord 块构建正确
  • 我有一些可以正确提取的测试数据

此时,我认为我的路由器设置不正确.在您的示例中,您调用了 <?php echo $this->getEditFormUrl();?> .我不确定这是否应该输出格式化的操作 url,但事实并非如此.我自己扔了进去,但它仍然没有正确击中路由器.

当前,表单中的值 action =

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

然后点击找不到页面".据我所知,路由器应该在这里是准确的.我尝试了许多不同的组合;我显然在管理路由器问题上遗漏了一些重要的东西.我了解 key/ee6... 可能是我的问题,但我不清楚如何适当地处理它.

<小时>

更新

路由问题是一个简单的错误,我不必要地将其复杂化:我有一个拼写错误的目录名称AdminHtml"与Adminhtml"-导致路由器丢失.

哦,Magento...

谢谢@R.S.寻求帮助.

解决方案

您可以尝试将可编辑输入"添加到订单详细信息页面的帐户信息"部分.

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

添加

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

在你的街区

setTemplate('salesext/edit_form.phtml');}公共函数 getOrder(){return Mage::registry('current_order');}公共函数 getFormUrl(){return Mage::helper("adminhtml")->getUrl('*/sales_order/editField', array('order_id'=>$this->getOrder()->getId());}...

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

getRequest()->getControllerName() : ?><div id="change-order-email" style="display:none"><form action="<?php echo $this->getEditFormUrl(); ?>"方法="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 cellpacing="4" class="form-list"><tr><td>字段</td><td><input .... class="required-entry validate-email"/></td></tr></表单><button onclick="editForm.submit()">更改</button>

<script type="text/javascript">editForm = new varienForm('edit_form');<?php endif;?>

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

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

在你的 config.xml

<代码>...<管理员><路由器><use>admin</use><参数><模块><CWACI_SalesExt after="Mage_Adminhtml_Sales">CWACI_SalesExt_Adminhtml</CWACI_SalesExt></模块></args></adminhtml></路由器></管理员>

.....

另请参阅从模块设置自定义管理主题以防止制作直接修改核心模板.

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 :

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.

解决方案

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

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.

这篇关于Magento 自定义“订单"属性/管理输入和显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆