Magento2-如何将自定义数据从报价复制到订单和订单项目

时间:2015-08-08 02:05:02

标签: magento2

我正在Magento 2中构建一个具有自定义折扣的自定义模块。我正在尝试将折扣从报价,报价项目复制到订单和订单项目。 在Magento 1中,我声明了config.xml,如下所示:

    <fieldsets>
            <sales_convert_quote_address>
                <custom_discount_amount><to_order>*</to_order></custome_discount_amount>
                <base_custom_discount_amount><to_order>*</to_order></base_custome_discount_amount>
            </sales_convert_quote_address>
            <sales_convert_quote_item>
                <custome_discount_amount><to_order_item>*</to_order_item></custome_discount_amount>
                <base_custom_discount_amount><to_order_item>*</to_order_item></base_custom_discount_amount>
            </sales_convert_quote_item>
        </fieldsets> 

我的自定义折扣金额已按预期复制到表格sales_flat_ordersales_flat_order_item

在Magento 2中,我使用以下代码创建了一个名为fieldset.xml的文件:

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Object/etc/fieldset.xsd">
    <scope id="global">
        <fieldset id="sales_convert_quote_item">
            <field name="custom_discount_amount">
                <aspect name="to_order_item" />
            </field>
            <field name="base_custom_discount_amount">
                <aspect name="to_order_item" />
            </field>
        </fieldset>         
          <fieldset id="sales_convert_quote_address">
            <field name="custom_discount_amount">
                <aspect name="to_order" />
            </field>
            <field name="base_custom_discount_amount">
                <aspect name="to_order" />
            </field>
        </fieldset>   
    </scope>
</config>

但没有成功。 我还需要在Magento 2中做些什么来使它工作?你能帮帮我吗?

3 个答案:

答案 0 :(得分:4)

在不使用fieldset的Magento 2中,您还可以使用插件将自定义数据从引用项复制到订单项。

在您的模块等文件夹中创建 di.xml 应用/代码/销售者/ Yourmodule的/ etc / di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\Quote\Item\ToOrderItem">
        <plugin name="cedapi_quote_to_order_item" type="Vender\Yourmodule\Model\Plugin\Quote\QuoteToOrderItem"/>
    </type>
</config>

为模块创建一个类并定义一个函数。 应用/代码/销售者/ Yourmodule /型号/插件/报价
创建 QuoteToOrderItem.php 文件

<?php
namespace Vender\Yourmodule\Model\Plugin\Quote;

use Closure;

class QuoteToOrderItem
{
    /**
     * @param \Magento\Quote\Model\Quote\Item\ToOrderItem $subject
     * @param callable $proceed
     * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
     * @param array $additional
     * @return \Magento\Sales\Model\Order\Item
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
  public function aroundConvert(
        \Magento\Quote\Model\Quote\Item\ToOrderItem $subject,
        Closure $proceed,
        \Magento\Quote\Model\Quote\Item\AbstractItem $item,
        $additional = []
    ) {
        /** @var $orderItem \Magento\Sales\Model\Order\Item */
        $orderItem = $proceed($item, $additional);//result of function 'convert' in class 'Magento\Quote\Model\Quote\Item\ToOrderItem' 
        $orderItem->setCustomDesign($item->getCustomDesign());//set your required
        return $orderItem;// return an object '$orderItem' which will replace result of function 'convert' in class 'Magento\Quote\Model\Quote\Item\ToOrderItem'
    }

}

答案 1 :(得分:1)

花了一些时间和研究问题后,我坚持到这里: Magento的\报价\型号\ QuoteManagement.php 第446行

public function mergeDataObjects(
    $interfaceName,
    $firstDataObject,
    $secondDataObject
) {
    if (!$firstDataObject instanceof $interfaceName || !$secondDataObject instanceof $interfaceName) {
        throw new \LogicException('Wrong prototype object given. It can only be of "' . $interfaceName . '" type.');
    }
    $secondObjectArray = $this->objectProcessor->buildOutputDataArray($secondDataObject, $interfaceName);
    $this->_setDataValues($firstDataObject, $secondObjectArray, $interfaceName);
    return $this;
}

这会忽略转换后的属性,因为合并逻辑基于目标模型\接口的状态获取器和设置器。因此,如果您在目标模型中转换没有设置者和获取者的属性,则在此处将忽略它们:
Magento \ Framework \ Reflection \ DataObjectProcessor.php第75行

public function buildOutputDataArray($dataObject, $dataObjectType)
{
    $methods = $this->methodsMapProcessor->getMethodsMap($dataObjectType);
    $outputData = [];

    /** @var MethodReflection $method */
    foreach (array_keys($methods) as $methodName) {
        if (!$this->methodsMapProcessor->isMethodValidForDataField($dataObjectType, $methodName)) {
            continue;
        }

        $value = $dataObject->{$methodName}();
        $isMethodReturnValueRequired = $this->methodsMapProcessor->isMethodReturnValueRequired(
            $dataObjectType,
            $methodName
        );

也许你可能会使用观察者或插件来避免这个问题。 (在2.0.6 Magento版本中遇到的问题)

答案 2 :(得分:1)

对于今后看到这个问题的人来说,字段集XML不起作用的事实已被Magento承认为一个错误。机票中有一个核心补丁(此处未转载,因为可能需要使用新的Magento版本进行调整)。

https://github.com/magento/magento2/issues/5823