It appears fieldsets are no longer an option for moving custom data fields around. Not clear if this is intentional, or a side-effect of other changes.
I just ran into the same problem with payments. I traced that back to \Magento\Quote\Model\Quote\Payment\ToOrderPayment::convert()
:
public function convert(Payment $object, $data = [])
{
$paymentData = $this->objectCopyService->getDataFromFieldset(
'quote_convert_payment',
'to_order_payment',
$object
);
$orderPayment = $this->orderPaymentFactory->create();
$this->dataObjectHelper->populateWithArray(
$orderPayment,
array_merge($paymentData, $data),
'\Magento\Sales\Api\Data\OrderPaymentInterface'
);
$orderPayment->setAdditionalInformation(
array_merge(
$object->getAdditionalInformation(),
[Substitution::INFO_KEY_TITLE => $object->getMethodInstance()->getTitle()]
)
);
// set directly on the model
$orderPayment->setCcNumber($object->getCcNumber());
$orderPayment->setCcCid($object->getCcCid());
return $orderPayment;
}
It starts by processing the fieldset to copy data from $object
into the $paymentData
array, but then feeds that array into \Magento\Framework\Api\DataObjectHelper::populateWithArray()
to actually set the values on the target object (here, $orderPayment
).
Rather than directly adding the data array to the object as in Magento 1, DataObjectHelper
looks for 'set__' or 'setIs__' methods corresponding to each key, and then calls those methods with the value. If such method does not exist, the data key is skipped entirely.
Quote addresses and items are processed the same way, in \Magento\Quote\Model\Quote\Address\ToOrderAddress
and \Magento\Quote\Model\Quote\Item\ToOrderItem
respectively.
What does that mean?
- Adding the key to a fieldset is not sufficient, unless a set method already exists on the object corresponding to that key.
- It appears that copying custom fields from quote to order will require either overriding the target class to add the necessary setter method(s) (via dependency injection), or using observers like
sales_model_service_quote_submit_before
to copy the data manually.
Also note that the quote module has its own fieldsets defined, including quote_convert_address
and quote_convert_item
. It does not appear that the sales_convert_quote* fieldsets are in use anymore.
Suppose package:Learning and module: Custom
Module.xml looks like
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Learning_Custom" setup_version="2.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
And then create a layout xml following path -> app/code/Learning/Custom/view/frontend/layout/catalog_product_prices.xml
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/layout_generic.xsd">
<referenceBlock name="render.product.prices">
<arguments>
<argument name="default" xsi:type="array">
<item name="prices" xsi:type="array">
<item name="tier_price" xsi:type="array">
<item name="render_template" xsi:type="string">Learning_Custom::product/price/tier_prices.phtml
</item>
</item>
</argument>
</arguments>
</referenceBlock>
</layout>
Create template inside your module or copy base template file inside your module path should be ->
app/code/Learning/Custom/view/frontend/templates/product/price/tier_prices.phtml
Best Answer
In Magento 2 without using fieldset you can also copy custom data from quote item to order item by using plugin.
create
di.xml
in your module etc folder.app/code/Vender/Yourmodule/etc/di.xml
Create a class to your module and define a function. app/code/Vender/Yourmodule/Model/Plugin/Quote
Create
QuoteToOrderItem.php
file