系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》
本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。
在Magento 2的“管理订单视图”页面中添加可编辑字段的步骤:
步骤1:转到以下文件路径
app\code\Vendor\Extension\view\adminhtml\layout\sales_order_view.xml
然后添加以下代码
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="order_additional_info"> <block class="Vendor\Extension\Block\Adminhtml\Order\Checkoutcustomformedit" name="admin.chekoutcustomfield" template="Vendor_Extension::checkoutcustomformedit.phtml" /> </referenceBlock> </body> </page>
步骤2:接下来,移动到以下文件路径
app\code\Vendor\Extension\Block\Adminhtml\Order\Checkoutcustomformedit.php
现在添加下面提到的代码片段
<?php namespace Vendor\Extension\Block\Adminhtml\Order; use \Magento\Backend\Block\Template; use \Magento\Backend\Block\Template\Context; use \Magento\Sales\Model\Order; class Checkoutcustomformedit extends Template { protected $order; public function __construct( Context $context, Order $order, array $data = []) { $this->order = $order; parent::__construct($context, $data); } public function getOrderId() { $orderId = $this->getRequest()->getParam('order_id'); return $orderId; } public function getCustomfieldvalue() { $orderId = $this->getOrderId(); $customfieldvalue = $this->order->load($orderId)->getData('customfield1'); return $customfieldvalue; } }
步骤3:现在导航到以下文件路径
app\code\Vendor\Extension\view\adminhtml\templates\Checkoutcustomformedit.phtml
现在添加代码,如下所示
<section class="admin__page-section"> <div class="admin__page-section-title"> <span class="title"> <?php /* @escapeNotVerified */ echo __('Buyer Order Information') ?> </span> <div id="checkoutfield-edit-link" class="actions block-edit-link"> <a href="#" id="edit-checkoutfield-info"> <?= $block->escapeHtml(__('Edit')); ?> </a> </div> </div> <div class="admin__page-section-content"> <div class="order-information textfied" id="textfiedcustomfield"> <div class="box"> <div class="box-content"> <?php echo $block->getCustomfield1(); ?> </div> </div> </div> </div> <div class="admin__page-section-item-content edit-checkoutfield-date" id="edit-checkoutfield-info-form" style="display: none;"> <fieldset class="admin__fieldset"> <input type="hidden" id="orderid" value="<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $request = $objectManager->get('Magento\Framework\App\Request\Http'); echo $param = $request->getParam('order_id');?>"/> <input type="text" id="customfield1" class="customfield1" value="<?php echo $block->getCustomfield1(); ?>"> </fieldset> <button id="customfield-submit">Submit</button> </div> </section> <script type="text/javascript"> require([ 'jquery', 'mage/validation', 'mage/translate', 'jquery/ui' ], function ($) { document.getElementById('edit-checkoutfield-info').addEventListener('click', function (e, t) { e.preventDefault(); $('#edit-checkoutfield-info-form').toggle(); }); $("#customfield-submit").click(function() { var orderid = $('#orderid').val(); if($('#customfield1').length){ var customfield1 = $('#customfield1').val(); } var url = '<?= /** @noEscape */ $block->getUrl("groupproduct/queue/update")?>'; jQuery.ajax({ url: url, data: {orderid: orderid,customfield1value: customfield1}, type: "POST", async: false, }).done(function (response) { $("#textfiedcustomfield").load(location.href + " #textfiedcustomfield"); }); }); }); </script>
步骤4:最后,移动到以下文件路径
app\code\Vendor\Extension\Controller\Adminhtml\Queue\Update.php
并添加下面提到的代码
<?php namespace Vendor\Extension\Controller\Adminhtml\Queue; use Magento\Framework\App\ResponseInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Backend\App\Action; use Magento\Sales\Api\OrderRepositoryInterface; class Update extends Action { protected $quoteFactory protected $orderinterface; protected $request; public function __construct( Action\Context $context, \Magento\Quote\Model\QuoteFactory $quoteFactory, \Magento\Sales\Api\Data\OrderInterface $orderinterface, \Magento\Framework\App\Request\Http $request) { parent::__construct($context); $this->orderinterface = $orderinterface; $this->request = $request; $this->quoteFactory = $quoteFactory; } public function execute() { $customfield1value = $this->getRequest()->getPostValue('customfield1value'); $orderid = $this->getRequest()->getPostValue('orderid'); $order = $this->orderinterface->load($orderid); $order->setData('customfield1', $customfield1value); $order->save(); } }
结论:
这样,您可以在Magento 2的管理订单视图页面中添加可编辑字段。