Exam4Training

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?

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 NagentosalesApiE)ataorderinterface and an after plugin on MagentoSalesModelOrder: :getExtensionAttributes() to add the custom data.
B. Create an extension attribute On MagentoSalesApiDataOrderInterface and an after plugin On MagentoSalesApiOrderRepositoryInterface On geto and getListo to add the custom data.
C. Create a before plugin on MagentosalesmodelResourceModelordercollection: :load and alter the query to fetch the additional data. Data will then be automatically added to the items fetched from the API.

Answer: B

Explanation:

The developer should create an extension attribute on the MagentoSalesApiDataOrderInterface interface and an after plugin on the MagentoSalesApiOrderRepositoryInterface::get() and MagentoSalesApiOrderRepositoryInterface::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 MyVendorMyModuleApiData;

interface OrderExtensionInterface extends MagentoSalesApiDataOrderInterface {

/**

* Get custom data.

*

* @return string|null */

public function getCustomData();

/**

* Set custom data.

*

* @param string $customData

* @return $this

*/

public function setCustomData($customData);

}

namespace MyVendorMyModuleModel;

class OrderRepository extends MagentoSalesApiOrderRepositoryInterface {

/**

* After get order.

*

* @param MagentoSalesApiOrderRepositoryInterface $subject

* @param MagentoSalesApiDataOrderInterface $order

* @return MagentoSalesApiDataOrderInterface

*/

public function afterGetOrder($subject, $order)

{

if ($order instanceof OrderExtensionInterface) {

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

}

return $order;

}

/**

* After get list.

*

* @param MagentoSalesApiOrderRepositoryInterface $subject

* @param MagentoSalesApiDataOrderInterface[] $orders

* @return MagentoSalesApiDataOrderInterface[]

*/

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.

Exit mobile version