nerdexam
Adobe

AD0-E716 · Question #18

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?

The correct answer is B. Create an extension attribute On Magento\Sales\Api\Data\OrderInterface and an after plugin On. 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…

Customization

Question

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?

Options

  • ACreate an extension attribute on Nagento\sales\Api\E)ata\orderinterface and an after plugin on
  • BCreate an extension attribute On Magento\Sales\Api\Data\OrderInterface and an after plugin On
  • CCreate a before plugin on Magento\sales\model\ResourceModel\order\collection: :load and alter

How the community answered

(31 responses)
  • A
    10% (3)
  • B
    74% (23)
  • C
    16% (5)

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: 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 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'); * 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'); Once the extension attribute and after plugin are created, the custom data will be added to orders fetched from the API.

Topics

#extension attributes#OrderInterface#API plugin#order data

Community Discussion

No community discussion yet for this question.

Full AD0-E716 Practice