Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0"?>
- <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
- <type name="MagentoSalesApiOrderRepositoryInterface">
- <plugin name="save_custom" type="MyProjectPluginOrderSave"/>
- <plugin name="get_custom" type="MyProjectPluginOrderGet"/>
- </type>
- </config>
- <?xml version="1.0"?>
- <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
- <extension_attributes for="MagentoSalesApiDataOrderInterface">
- <attribute code="custom" type="MyProjectApiDataCustomAttributeInterface" />
- </extension_attributes>
- </config>
- <?php
- namespace MyProjectApiData;
- interface CustomInterface
- {
- const NAME = 'custom';
- /**
- * Return value.
- *
- * @return string|null
- */
- public function getValue();
- /**
- * Set value.
- *
- * @param string|null $value
- * @return $this
- */
- public function setValue($value);
- }
- <?php
- // ... some code
- class OrderGet
- {
- // constructor and other code...
- public function afterGet(
- OrderRepositoryInterface $subject,
- OrderInterface $resultOrder
- ) {
- $resultOrder = $this->getCustomAttribute($resultOrder);
- return $resultOrder;
- }
- private function getCustomAttribute(OrderInterface $order)
- {
- try {
- $somwhere = $this->someFactory->create();
- $somwhere->load($order->getIncrementId(), 'order_id');
- if (!$somwhere->getId()) {
- throw new NoSuchEntityException('No custom entity found for id' . $order->getIncrementId());
- }
- // This will get the custom value from the database
- $customAttributeValue = $this->someHelper->getCustomConfig($somwhere->getData('some_id'));
- } catch (NoSuchEntityException $e) {
- return $order;
- }
- $extensionAttributes = $order->getExtensionAttributes();
- $orderExtension = $extensionAttributes ? $extensionAttributes : $this->orderExtensionFactory->create();
- $customAttribute = $this->customAttributeFactory->create();
- $customAttribute->setValue($customAttributeValue);
- $orderExtension->setCustomAttribute($customAttribute);
- $order->setExtensionAttributes($orderExtension);
- return $order;
- }
- }
Add Comment
Please, Sign In to add comment