@@ -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
- ** extension_attributes.xml**
20
+ ** etc/ 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
- ** fieldset.xml**
39
+ ** etc/ fieldset.xml**
40
40
41
- {% highlight xml %}
41
+ ``` 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,51 +48,70 @@ 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
- {% endhighlight %}
51
+ ```
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 ` etc/events.xml ` :
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
+
56
66
The following code snippets highlight the code pieces needed to copy a fieldset using the ` \Magento\Framework\DataObject\Copy ` class.
57
67
58
- {% highlight php startinline %}
59
- ...
60
-
61
- /**
62
- * @var \Magento\Framework\DataObject\Copy
63
- * /
64
- protected $objectCopyService;
65
-
66
- ...
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;
77
- ...
78
- }
68
+ ``` php
69
+ <?php
70
+ namespace Vendor\Module\Observer;
79
71
80
- ...
72
+ use Magento\Framework\Event\ObserverInterface;
81
73
82
- /**
83
- * @param $quote \Magento\Quote\Api\Data\CartInterface
84
- * @param $order \Magento\Sales\Api\Data\Order
85
- * /
86
- private function copyQuoteToOrder($quote, $order)
74
+ class SaveOrderBeforeSalesModelQuoteObserver implements ObserverInterface
87
75
{
88
- ...
89
- $copy->copyFieldsetToTarget('sales_convert_quote', 'to_order', $quote, $order);
90
- ...
91
- }
76
+ ...
77
+
78
+ /**
79
+ * @var \Magento\Framework\DataObject\Copy
80
+ */
81
+ protected $objectCopyService;
92
82
93
- ...
94
- {% endhighlight %}
83
+ ...
84
+
85
+ /**
86
+ * @param \Magento\Framework\DataObject\Copy $objectCopyService
87
+ * ...
88
+ */
89
+ public function __construct(
90
+ \Magento\Framework\DataObject\Copy $objectCopyService,
91
+ ...
92
+ ) {
93
+ $this->objectCopyService = $objectCopyService;
94
+ ...
95
+ }
96
+
97
+ /**
98
+ * @param \Magento\Framework\Event\Observer $observer
99
+ */
100
+ private function execute(\Magento\Framework\Event\Observer $observer)
101
+ {
102
+ /* @var Magento\Sales\Model\Order $order */
103
+ $order = $observer->getEvent()->getData('order');
104
+ /* @var Magento\Quote\Model\Quote $quote */
105
+ $quote = $observer->getEvent()->getData('quote');
106
+
107
+ $this->objectCopyService->copyFieldsetToTarget('sales_convert_quote', 'to_order', $quote, $order);
108
+ ...
109
+
110
+ return $this;
111
+ }
112
+ }
95
113
114
+ ```
96
115
97
116
In the code, an instance of the ` Copy ` class is obtained from the constructor using [ dependency injection] [ 2 ] .
98
117
The ` copyFieldsetToTarget ` function call with the ` $quote ` and ` $order ` parameters copies the fieldset for the two objects..
0 commit comments