Guest User

Untitled

a guest
Jan 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. <?xml version="1.0"?>
  2. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  3. <type name="MagentoSalesApiOrderRepositoryInterface">
  4. <plugin name="save_custom" type="MyProjectPluginOrderSave"/>
  5. <plugin name="get_custom" type="MyProjectPluginOrderGet"/>
  6. </type>
  7. </config>
  8.  
  9. <?xml version="1.0"?>
  10. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
  11. <extension_attributes for="MagentoSalesApiDataOrderInterface">
  12. <attribute code="custom" type="MyProjectApiDataCustomAttributeInterface" />
  13. </extension_attributes>
  14. </config>
  15.  
  16. <?php
  17.  
  18. namespace MyProjectApiData;
  19.  
  20. interface CustomInterface
  21. {
  22. const NAME = 'custom';
  23.  
  24. /**
  25. * Return value.
  26. *
  27. * @return string|null
  28. */
  29. public function getValue();
  30.  
  31. /**
  32. * Set value.
  33. *
  34. * @param string|null $value
  35. * @return $this
  36. */
  37. public function setValue($value);
  38. }
  39.  
  40. <?php
  41.  
  42. // ... some code
  43.  
  44. class OrderGet
  45. {
  46.  
  47. // constructor and other code...
  48.  
  49. public function afterGet(
  50. OrderRepositoryInterface $subject,
  51. OrderInterface $resultOrder
  52. ) {
  53. $resultOrder = $this->getCustomAttribute($resultOrder);
  54.  
  55. return $resultOrder;
  56. }
  57.  
  58.  
  59. private function getCustomAttribute(OrderInterface $order)
  60. {
  61. try {
  62. $somwhere = $this->someFactory->create();
  63. $somwhere->load($order->getIncrementId(), 'order_id');
  64.  
  65. if (!$somwhere->getId()) {
  66. throw new NoSuchEntityException('No custom entity found for id' . $order->getIncrementId());
  67. }
  68.  
  69. // This will get the custom value from the database
  70. $customAttributeValue = $this->someHelper->getCustomConfig($somwhere->getData('some_id'));
  71.  
  72. } catch (NoSuchEntityException $e) {
  73. return $order;
  74. }
  75.  
  76. $extensionAttributes = $order->getExtensionAttributes();
  77. $orderExtension = $extensionAttributes ? $extensionAttributes : $this->orderExtensionFactory->create();
  78. $customAttribute = $this->customAttributeFactory->create();
  79. $customAttribute->setValue($customAttributeValue);
  80. $orderExtension->setCustomAttribute($customAttribute);
  81. $order->setExtensionAttributes($orderExtension);
  82.  
  83. return $order;
  84. }
  85. }
Add Comment
Please, Sign In to add comment