ExamGecko
Question list
Search
Search

List of questions

Search

Related questions











Question 18 - AD0-E716 discussion

Report
Export

An Adobe Commerce developer is tasked with adding custom data to orders fetched from the API. While keeping best practices in mind, how would the developer achieve this?

A.
Create an extension attribute on Nagento\sales\Api\E)ata\orderinterface and an after plugin on Magento\Sales\Model\Order: :getExtensionAttributes() to add the custom data.
Answers
A.
Create an extension attribute on Nagento\sales\Api\E)ata\orderinterface and an after plugin on Magento\Sales\Model\Order: :getExtensionAttributes() to add the custom data.
B.
Create an extension attribute On Magento\Sales\Api\Data\OrderInterface and an after plugin On Magento\Sales\Api\OrderRepositoryInterface On geto and getListo to add the custom data.
Answers
B.
Create an extension attribute On Magento\Sales\Api\Data\OrderInterface and an after plugin On Magento\Sales\Api\OrderRepositoryInterface On geto and getListo to add the custom data.
C.
Create a before plugin on Magento\sales\model\ResourceModel\order\collection: :load and alter the query to fetch the additional data. Data will then be automatically added to the items fetched from the API.
Answers
C.
Create a before plugin on Magento\sales\model\ResourceModel\order\collection: :load and alter the query to fetch the additional data. Data will then be automatically added to the items fetched from the API.
Suggested answer: B

Explanation:

The developer should create an extension attribute on the Magento\Sales\Api\Data\OrderInterface interface and an after plugin on the Magento\Sales\Api\OrderRepositoryInterface::get() and Magento\Sales\Api\OrderRepositoryInterface::getList() methods.

The extension attribute will store the custom data. The after plugin will be used to add the custom data to the order object when it is fetched from the API.

Here is the code for the extension attribute and after plugin:

PHP

namespace MyVendor\MyModule\Api\Data;

interface OrderExtensionInterface extends \Magento\Sales\Api\Data\OrderInterface

{

/**

* Get custom data.

*

* @return string|null

*/

public function getCustomData();

/**

* Set custom data.

*

* @param string $customData

* @return $this

*/

public function setCustomData($customData);

}

namespace MyVendor\MyModule\Model;

class OrderRepository extends \Magento\Sales\Api\OrderRepositoryInterface

{

/**

* After get order.

*

* @param \Magento\Sales\Api\OrderRepositoryInterface $subject

* @param \Magento\Sales\Api\Data\OrderInterface $order

* @return \Magento\Sales\Api\Data\OrderInterface

*/

public function afterGetOrder($subject, $order)

{

if ($order instanceof OrderExtensionInterface) {

$order->setCustomData('This is custom data');

}

return $order;

}

/**

* After get list.

*

* @param \Magento\Sales\Api\OrderRepositoryInterface $subject

* @param \Magento\Sales\Api\Data\OrderInterface[] $orders

* @return \Magento\Sales\Api\Data\OrderInterface[]

*/

public function afterGetList($subject, $orders)

{

foreach ($orders as $order) {

if ($order instanceof OrderExtensionInterface) {

$order->setCustomData('This is custom data');

}

}

return $orders;

}

}

Once the extension attribute and after plugin are created, the custom data will be added to orders fetched from the API.

asked 02/10/2024
Karesa Potts
28 questions
User
Your answer:
0 comments
Sorted by

Leave a comment first