magento2把quote表的字段值copy到order表里
magento2把quote表的字段值copy到order表里
在开发过程中,我们经常要在运输方式下面加一些自定义的字段,如何把这个自定义的字段保存到order表里去呢?
需要经过quote表来中转,也就是说要先保存到quote表里,再把quote表的字段值复制到order表里。
所以 我们需要先给quote和order表添加字段。
比如字段名为 delivery_time_additional_info
假设你的插件已经写好了,比如我的插件是Zou_Demo
一,ZouDemoSetupUpgradeData.php里添加创建字段的代码
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); if (version_compare($context->getVersion(), &#39;0.1.1&#39;, &#39;<&#39;)) { $this->createCustomShippintMethodFields($setup); } $setup->endSetup(); } protected function createCustomShippintMethodFields($setup){ $installer = $setup; $installer->startSetup(); $eavTable1 = $installer->getTable(&#39;quote&#39;); $eavTable2 = $installer->getTable(&#39;sales_order&#39;); $columns = [ &#39;desired_delivery_time_additional_info&#39; => [ &#39;type&#39; => MagentoFrameworkDBDdlTable::TYPE_TEXT, &#39;length&#39;=>&#39;2k&#39;, &#39;nullable&#39; => true, &#39;comment&#39; => &#39;Desired Delivery Time / Additional Information&#39;, ] ]; $connection = $installer->getConnection(); foreach ($columns as $name => $definition) { $connection->addColumn($eavTable1, $name, $definition); $connection->addColumn($eavTable2, $name, $definition); } $installer->endSetup(); }
你需要改下module.xml的版本号,再执行php bin/magento setup:upgrade 才会执行添加表字段。
二,监听sales_model_service_quote_submit_before事件
在etc/events.xml里添加
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nOnamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd" <event name="sales_model_service_quote_submit_before"> <observer name="custom_fields_sales_address_save" instance="ZouDemoObserverQuoteCopyFiledToOrder" /> </event> </config>
这段代码的作用就是: 当order保存时,会触发sales_model_service_quote_submit_before这个事件,我们监听这个事件 就能对order对象操作。从quote里取出数据,然后再赋值给order。
我们接着写逻辑实现代码
创建ZouDemoObserverQuoteCopyFiledToOrder.php
<?php namespace ZouDemoObserverQuote; /** * Class SaveCustomFieldsInOrder * @package DckapCustomFieldsObserver */ class CopyFiledToOrder implements MagentoFrameworkEventObserverInterface { /** * @param MagentoFrameworkEventObserver $observer * @return $this */ public function execute(MagentoFrameworkEventObserver $observer) { $order = $observer->getEvent()->getOrder(); $quote = $observer->getEvent()->getQuote(); $order->setData("delivery_time_additional_info",$quote->getDeliveryTimeAdditionalInfo()); return $this; } }
这样就行了。
你可以对数据库看下order表的delivery_time_additional_info有没有变化,是不是跟quote表的delivery_time_additional_info一样。
之前2.1.x版本里 不需要这么麻烦 直接在etc/fieldset.xml里这样写
<?xml version="1.0"?> <!-- /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nOnamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd"> <scope id="global"> <fieldset id="sales_convert_quote"> <field name="delivery_time_additional_info"> <aspect name="to_order" /> </field> </fieldset> </scope> </config>
就会把quote表的delivery_time_additional_info值 复制到order表里。但是2.2.x里不行。
最后于 6月前 被admin编辑 ,原因: