@@ -17,7 +17,7 @@ In this tutorial, you will learn to copy custom data from a {% glossarytooltip 7
17
17
18
18
The following code defines a simple [ extension attribute] [ 1 ] named ` demo ` for the Cart and Order objects.
19
19
20
- ** etc/ extension_attributes.xml**
20
+ ** extension_attributes.xml**
21
21
22
22
{% highlight xml %}
23
23
<config xmlns:xsi =" http://www.w3.org/2001/XMLSchema-instance " xsi:noNamespaceSchemaLocation =" Api/etc/extension_attributes.xsd " >
@@ -36,9 +36,9 @@ The following code defines a simple [extension attribute][1] named `demo` for th
36
36
The following code adds the ` demo ` field to the ` sales_convert_quote ` fieldset with the ` to_order ` aspect.
37
37
The code snippet in the next step uses the name of the fieldset and aspect to specify which fields to copy.
38
38
39
- ** etc/ fieldset.xml**
39
+ ** fieldset.xml**
40
40
41
- ``` xml
41
+ {% highlight xml %}
42
42
<config xmlns:xsi =" http://www.w3.org/2001/XMLSchema-instance " xsi:noNamespaceSchemaLocation =" DataObject/etc/fieldset.xsd " >
43
43
<scope id =" global " >
44
44
<fieldset id="sales_convert_quote">
@@ -48,66 +48,50 @@ The code snippet in the next step uses the name of the fieldset and aspect to sp
48
48
</fieldset>
49
49
</scope >
50
50
</config >
51
- ```
51
+ {% endhighlight %}
52
52
53
53
## Step 3: Copy the fieldset
54
54
{:#step-3}
55
55
56
- For copying the fieldset, we'll observe the ` sales_model_service_quote_submit_before ` event by using the following code in our ` events.xml ` file:
57
-
58
- ``` xml
59
- <config xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" xsi : noNamespaceSchemaLocation =" urn:magento:framework:Event/etc/events.xsd" >
60
- <event name =" sales_model_service_quote_submit_before" >
61
- <observer name =" [vendor]_[module]_sales_model_service_quote_submit_before" instance =" Vendor\Module\Observer\SaveOrderBeforeSalesModelQuoteObserver" />
62
- </event >
63
- </config >
64
- ```
65
-
66
56
The following code snippets highlight the code pieces needed to copy a fieldset using the ` \Magento\Framework\DataObject\Copy ` class.
67
57
68
- ``` php
69
- <?php
70
- namespace Vendor\Module\Observer;
58
+ {% highlight php startinline %}
59
+ ...
71
60
72
- use Magento\Framework\Event\ObserverInterface;
61
+ /**
62
+ * @var \Magento\Framework\DataObject\Copy
63
+ * /
64
+ protected $objectCopyService;
73
65
74
- class SaveOrderBeforeSalesModelQuoteObserver implements ObserverInterface
75
- {
76
66
...
77
- /**
78
- * @var \Magento\Framework\DataObject\Copy
79
- */
80
- protected $objectCopyService;
81
67
68
+ /**
69
+ * @param \Magento\Framework\DataObject\Copy $objectCopyService
70
+ ...
71
+ * /
72
+ public function __ construct(
73
+ \Magento\Framework\DataObject\Copy $objectCopyService,
74
+ ...
75
+ ) {
76
+ $this->objectCopyService = $objectCopyService;
82
77
...
78
+ }
79
+
80
+ ...
83
81
84
- /**
85
- * @param \Magento\Framework\DataObject\Copy $objectCopyService
86
- ...
87
- */
88
- public function __construct(
89
- \Magento\Framework\DataObject\Copy $objectCopyService,
90
- ...
91
- ) {
92
- $this->objectCopyService = $objectCopyService;
93
- ...
94
- }
95
-
96
- /**
97
- * @param \Magento\Framework\Event\Observer $observer
98
- */
99
- private function execute(\Magento\Framework\Event\Observer $observer)
100
- {
101
- /* @var Magento\Sales\Model\Order $order */
102
- $order = $observer->getEvent()->getData('order');
103
- /* @var Magento\Quote\Model\Quote $quote */
104
- $quote = $observer->getEvent()->getData('quote');
105
- $this->objectCopyService->copyFieldsetToTarget('sales_convert_quote', 'to_order', $quote, $order);
106
- ...
107
- }
82
+ /**
83
+ * @param $quote \Magento\Quote\Api\Data\CartInterface
84
+ * @param $order \Magento\Sales\Api\Data\Order
85
+ * /
86
+ private function copyQuoteToOrder($quote, $order)
87
+ {
88
+ ...
89
+ $copy->copyFieldsetToTarget('sales_convert_quote', 'to_order', $quote, $order);
90
+ ...
108
91
}
109
92
110
- ```
93
+ ...
94
+ {% endhighlight %}
111
95
112
96
113
97
In the code, an instance of the ` Copy ` class is obtained from the constructor using [ dependency injection] [ 2 ] .
0 commit comments