Have created form in .phtml file which is display in sales_order_view page in admin. So how to save the form values in sales_order_item table from .phtml file?

[Vendor]/[module]/view/adminhtml/layout/sales_order_view.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <body>
       <!-- block -->
       <referenceBlock name="order_additional_info">
           <block class="Magento\Sales\Block\Adminhtml\Order\View\Tab\Info" name="sales_custom_view" template="Vendor_Module::order/view/tab/info.phtml"/>
       </referenceBlock>

   </body>
</page>

[Vendor]/[module]/view/adminhtml/templates/order/view/tab/info.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/** @var $block \Magento\Sales\Block\Adminhtml\Order\View\Tab\Info */
?>
<?php $_order = $block->getOrder() ?>


<section class="admin__page-section">
   <form id="custom_info_form" action="<?php echo $this->getUrl(''); ?>">
<div class="grid np" id="custom_info">
  <div class="hor-scroll">
    <table class="data-grid data-grid-draggable">
        <thead>
            <tr class="headings">
                <th class="data-grid-th _sortable _draggable"><span><?=__('Product') ?></th>
                <th class="data-grid-th _sortable _draggable"><span><?= __('Item Status') ?></span></th>
            </tr>
        </thead>
    <?php foreach($_order->getAllItems() as $_item):?>
       <tr>
           <td>
               <?php echo $_item->getName(); ?>
           </td>    

            <td>
               <?php $shippingStatus= "Complete"; ?>
            

               <select class="admin__control-select" name="shipping_status[<?php echo $_item->getItemId()?> ]" id="shipping_status" value="" title="shipping_status">
   
                <option selected="selected"><?= __('Choose one') ?></option>
                    <?php $options = array("Pending","Confirmed","Complete","Dues 7 – 10 days","Dues 2 – 3 weeks","Dues no date","Refunded","Cancelled","Failed");?>

                    <?php foreach($options as $option) : ?>
                <option value="<?php echo strtolower($option); ?>"><?php echo $option; ?></option>
                    <?php endforeach; ?>
               </select>
           </td>
       </tr>
       <?php endforeach; ?>


        </table>
            <button type="button" name="button_shipstatus" value=""  onclick="submitCustomform()" class="action-default scalable action-save action-secondary">
          <span><?= __('Submit') ?></span>
        </button>
       </div>
      </div>
    </form>
</section>

[Vendor]/[module]/etc/adminhtml/routes.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route frontName="orderitem" id="order_item_manager">
            <module before="Magento_Backend" name="Vendor_Module"/>
        </route>
    </router>
</config>
有帮助吗?

解决方案

I have done with Ajax ontroller. Here I am sharing the code which worked for me.[Vendor]/[module]/view/adminhtml/templates/order/view/tab/info.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/** @var $block \Magento\Sales\Block\Adminhtml\Order\View\Tab\Info */
?>
<?php $_order = $block->getOrder() ?>


<section class="admin__page-section">
   <form id="custom_info_form" action="<?php echo $this->getUrl('orderitem/test/save'); ?>">
<div class="grid np" id="custom_info">
  <div class="hor-scroll">
    <table class="data-grid data-grid-draggable">
        <thead>
            <tr class="headings">
                <th class="data-grid-th _sortable _draggable"><span><?=__('Product') ?></th>
                <th class="data-grid-th _sortable _draggable"><span><?= __('Item Status') ?></span></th>
            </tr>
        </thead>
    <?php foreach($_order->getAllItems() as $_item):?>
       <tr>
           <td>
               <?php echo $_item->getName(); ?>
           </td>    

            <td>
               <?php $shippingStatus= "Complete"; ?>
            

               <select class="admin__control-select" name="shipping_status[<?php echo $_item->getItemId()?> ]" id="shipping_status" value="" title="shipping_status">
   
                <option selected="selected"><?= __('Choose one') ?></option>
                    <?php $options = array("Pending","Confirmed","Complete","Dues 7 – 10 days","Dues 2 – 3 weeks","Dues no date","Refunded","Cancelled","Failed");?>

                    <?php foreach($options as $option) : ?>
                <option value="<?php echo strtolower($option); ?>"><?php echo $option; ?></option>
                    <?php endforeach; ?>
               </select>
           </td>
       </tr>
       <?php endforeach; ?>


        </table>
            <button type="button" name="button_shipstatus" value=""  onclick="submitCustomform()" class="action-default scalable action-save action-secondary">
          <span><?= __('Submit') ?></span>
        </button>
       </div>
      </div>
    </form>
</section>
<?php $order_id=$_order->getId(); ?>
      <script>
            function submitSupplierInfo()
            {
                var saveUrl = '<?php echo $this->getUrl('orderitem/test/save'); ?>';
                var request = new Ajax.Request(
                    saveUrl,
                    {
                        method: 'post',
                        onSuccess: supplierInfoResp,
                        parameters: Form.serialize($('custom_info_form')),


                    }
                );
            }

            function supplierInfoResp(transport)
            {
                if (transport && transport.responseText) {
                    try {
                        response = eval('(' + transport.responseText + ')');
                    }
                    catch (e) {
                        response = {};
                    }
                }
            }

        </script>

[Vendor]/[module]/Controller/Adminhtml/Test/Save.php

<?php

namespace [Vendor]\[module]\Controller\Adminhtml\Test;

class Save extends \Magento\Backend\App\Action
{
    protected $orderFactory;

    protected $resultPageFactory;
    protected $messageManager;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $resultPageFactory,
        \Magento\Sales\Model\Order\ItemFactory $orderFactory,
        \Magento\Framework\Message\ManagerInterface $messageManager
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
        $this->orderFactory = $orderFactory;
        $this->_messageManager = $messageManager;


    }

    public function execute()
    {
        try{

                $status = $this->getRequest()->getParam('shipping_status');
                foreach($status as $itemId => $stat ){
                $orderItem  = $this->orderFactory->create()
                                        ->getCollection()
                                        ->addFieldToFilter('item_id',$itemId);

                    $orderItem->getFirstItem()->setShippingStatus($stat);
                    $orderItem->save();
                }

        }
        catch (\Exception $e) {
        error_log($e->getMessage());
       }
       


        

    }
}

[Vendor]/[module]/view/adminhtml/layout/routes.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route frontName="orderitem" id="orderitem">
            <module name="[Vendor]_[module]"/>
        </route>
    </router>
</config>

[Vendor]/[module]/view/adminhtml/layout/sales_order_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <body>
       <!-- block -->
       <referenceBlock name="order_additional_info">
           <block class="Magento\Sales\Block\Adminhtml\Order\View\Tab\Info" name="sales_custom_view" template="[Vendor]_[module]::order/view/tab/info.phtml"/>
       </referenceBlock>

   </body>
</page>
许可以下: CC-BY-SA归因
scroll top