如何在Magento 2管理员创建订单页面上显示和保存扩展属性

时间:2019-03-27 16:06:12

标签: php magento2 adminhtml

我已经创建了扩展属性来订购Magento 2中的其余API服务。此外,我能够在订单网格上显示属性。

如何在管理员创建的订单表单上显示相同的属性并将其保存?

任何人都可以提出最好的方法吗?

预先感谢

这是我的extension_attributes.xml文件

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Checkout\Api\Data\ShippingInformationInterface">
        <attribute code="delivery_day_slot" type="string"/>
    </extension_attributes>

    <extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
        <attribute code="delivery_day_slot" type="string"/>
    </extension_attributes>
</config>

而且,我正在使用插件保存数据

<type name="Magento\Checkout\Model\ShippingInformationManagement">
    <plugin name="NM_save_delivery_slot_in_quote" type="<Namespace>\<ModuleName>\Plugin\Checkout\Model\ShippingInformationManagement" sortOrder="1"/>
</type>

命名空间\ ModuleName \ Plugin \ Checkout \ Model \ ShippingInformationManagement

namespace <Namespace>\<ModuleName>\Plugin\Checkout\Model;

class ShippingInformationManagement
{
    protected $quoteRepository;

    public function __construct(
        \Magento\Quote\Model\QuoteRepository $quoteRepository,
        \Magento\Checkout\Api\Data\ShippingInformationExtensionFactory $shpExtensionFactory,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->quoteRepository = $quoteRepository;
        $this->shpExtensionFactory = $shpExtensionFactory;
        $this->logger = $logger;
    }

    /**
     * @param \Magento\Checkout\Model\ShippingInformationManagement $subject
     * @param $cartId
     * @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
     */
    public function beforeSaveAddressInformation(
        \Magento\Checkout\Model\ShippingInformationManagement $subject,
        $cartId,
        \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
    ) {

        $extensionAttributes = $addressInformation->getExtensionAttributes();
        $shipExtension = $extensionAttributes ? $extensionAttributes : $this->shpExtensionFactory->create();

        $payload = \file_get_contents("php://input");
        $attr = json_decode($payload, true);
        if(isset($attr["extension_attributes"])){
            $deliveryDaySlot = $attr["extension_attributes"]["delivery_day_slot"];

            $addressInformation->setDeliveryDaySlot($deliveryDaySlot);
            $quote = $this->quoteRepository->getActive($cartId);
            $quote->setDeliveryDaySlot($deliveryDaySlot);

        }

        return;
    }
}

0 个答案:

没有答案