Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 198d682

Browse files
authored
Merge pull request #3992 from eduard13/issue-2910
Fixing the documentation on how to correctly copy the fieldsets with custom attributes
2 parents bdeeb34 + 0c02dd2 commit 198d682

File tree

1 file changed

+56
-37
lines changed

1 file changed

+56
-37
lines changed

guides/v2.1/ext-best-practices/tutorials/copy-fieldsets.md

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ In this tutorial, you will learn to copy custom data from a {% glossarytooltip 7
1717

1818
The following code defines a simple [extension attribute][1] named `demo` for the Cart and Order objects.
1919

20-
**extension_attributes.xml**
20+
**etc/extension_attributes.xml**
2121

2222
{% highlight xml %}
2323
<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
3636
The following code adds the `demo` field to the `sales_convert_quote` fieldset with the `to_order` aspect.
3737
The code snippet in the next step uses the name of the fieldset and aspect to specify which fields to copy.
3838

39-
**fieldset.xml**
39+
**etc/fieldset.xml**
4040

41-
{% highlight xml %}
41+
```xml
4242
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="DataObject/etc/fieldset.xsd">
4343
<scope id="global">
4444
<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
4848
</fieldset>
4949
</scope>
5050
</config>
51-
{% endhighlight %}
51+
```
5252

5353
## Step 3: Copy the fieldset
5454
{:#step-3}
5555

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+
5666
The following code snippets highlight the code pieces needed to copy a fieldset using the `\Magento\Framework\DataObject\Copy` class.
5767

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;
7971

80-
...
72+
use Magento\Framework\Event\ObserverInterface;
8173

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
8775
{
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;
9282

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+
}
95113

114+
```
96115

97116
In the code, an instance of the `Copy` class is obtained from the constructor using [dependency injection][2].
98117
The `copyFieldsetToTarget` function call with the `$quote` and `$order` parameters copies the fieldset for the two objects..

0 commit comments

Comments
 (0)