diff --git a/guides/v2.1/ext-best-practices/tutorials/copy-fieldsets.md b/guides/v2.1/ext-best-practices/tutorials/copy-fieldsets.md index f403bfaf7d3..f7de12f9df7 100644 --- a/guides/v2.1/ext-best-practices/tutorials/copy-fieldsets.md +++ b/guides/v2.1/ext-best-practices/tutorials/copy-fieldsets.md @@ -17,7 +17,7 @@ In this tutorial, you will learn to copy custom data from a {% glossarytooltip 7 The following code defines a simple [extension attribute][1] named `demo` for the Cart and Order objects. -**extension_attributes.xml** +**etc/extension_attributes.xml** {% highlight xml %} @@ -36,9 +36,9 @@ The following code defines a simple [extension attribute][1] named `demo` for th The following code adds the `demo` field to the `sales_convert_quote` fieldset with the `to_order` aspect. The code snippet in the next step uses the name of the fieldset and aspect to specify which fields to copy. -**fieldset.xml** +**etc/fieldset.xml** -{% highlight xml %} +```xml
@@ -48,51 +48,70 @@ The code snippet in the next step uses the name of the fieldset and aspect to sp
-{% endhighlight %} +``` ## Step 3: Copy the fieldset {:#step-3} +For copying the fieldset, we'll observe the `sales_model_service_quote_submit_before` event by using the following code in our `etc/events.xml`: + +```xml + + + + + +``` + The following code snippets highlight the code pieces needed to copy a fieldset using the `\Magento\Framework\DataObject\Copy` class. -{% highlight php startinline %} -... - -/** - * @var \Magento\Framework\DataObject\Copy - */ -protected $objectCopyService; - -... - -/** - * @param \Magento\Framework\DataObject\Copy $objectCopyService - ... - */ -public function __construct( - \Magento\Framework\DataObject\Copy $objectCopyService, - ... -) { - $this->objectCopyService = $objectCopyService; - ... - } +```php +copyFieldsetToTarget('sales_convert_quote', 'to_order', $quote, $order); - ... -} + ... + + /** + * @var \Magento\Framework\DataObject\Copy + */ + protected $objectCopyService; -... -{% endhighlight %} + ... + + /** + * @param \Magento\Framework\DataObject\Copy $objectCopyService + * ... + */ + public function __construct( + \Magento\Framework\DataObject\Copy $objectCopyService, + ... + ) { + $this->objectCopyService = $objectCopyService; + ... + } + + /** + * @param \Magento\Framework\Event\Observer $observer + */ + private function execute(\Magento\Framework\Event\Observer $observer) + { + /* @var Magento\Sales\Model\Order $order */ + $order = $observer->getEvent()->getData('order'); + /* @var Magento\Quote\Model\Quote $quote */ + $quote = $observer->getEvent()->getData('quote'); + + $this->objectCopyService->copyFieldsetToTarget('sales_convert_quote', 'to_order', $quote, $order); + ... + + return $this; + } +} +``` In the code, an instance of the `Copy` class is obtained from the constructor using [dependency injection][2]. The `copyFieldsetToTarget` function call with the `$quote` and `$order` parameters copies the fieldset for the two objects..