Guest User

Untitled

a guest
Jun 25th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. $quote = $this->quoteRepository->getActive($cartId);
  2.  
  3. // Extension Attributes
  4. $quoteExtension = $quote->getExtensionAttributes();
  5. $quoteExtension->setFromApp(true);
  6.  
  7. $this->quoteRepository->save($quote);
  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="MagentoQuoteApiDataCartInterface">
  12. <attribute code="from_app" type="string"/>
  13. </extension_attributes>
  14.  
  15. <?xml version="1.0" encoding="UTF-8"?>
  16. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  17. <type name="MagentoQuoteApiCartRepositoryInterface">
  18. <plugin name="vendor_namespace_plugin_cart_repository" type="VendorNamespacePluginCartRepositoryPlugin"/>
  19. </type>
  20.  
  21. $quote->setFromApp(true);
  22. $this->quoteRepository->save($quote);
  23.  
  24. $quote = $this->quoteRepository->getActive($cartId);
  25.  
  26. // Extension Attributes
  27. $quoteExtension = $quote->getExtensionAttributes();
  28. $quoteExtension->setFromApp(true);
  29. $quote->setExtensionAttributes(quoteExtension);
  30.  
  31. $this->quoteRepository->save($quote);
  32.  
  33. <?php
  34. namespace VendorNamespacePlugin;
  35.  
  36. use MagentoQuoteApiCartRepositoryInterface;
  37. use MagentoQuoteApiDataCartExtension;
  38. use MagentoQuoteApiDataCartExtensionFactory;
  39. use MagentoQuoteApiDataCartInterface;
  40. use MagentoQuoteApiDataCartSearchResultsInterface;
  41.  
  42. /**
  43. * Class CartRepositoryPlugin
  44. */
  45. class CartRepositoryPlugin
  46. {
  47. /**
  48. * @var CartExtensionFactory
  49. */
  50. private $extensionFactory;
  51.  
  52. /**
  53. * CartRepositoryPlugin constructor.
  54. *
  55. * @param CartExtensionFactory $orderExtensionFactory
  56. */
  57. public function __construct(
  58. CartExtensionFactory $orderExtensionFactory
  59. ) {
  60. $this->extensionFactory = $orderExtensionFactory;
  61. }
  62.  
  63. /**
  64. * @param CartRepositoryInterface $subject
  65. * @param CartInterface $resultEntity
  66. * @return CartInterface
  67. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  68. */
  69. public function afterGet(
  70. CartRepositoryInterface $subject,
  71. CartInterface $resultEntity
  72. ) {
  73. /** @var CartExtension $extensionAttributes */
  74. $extensionAttributes = $resultEntity->getExtensionAttributes() ?: $this->extensionFactory->create();
  75.  
  76. $extensionAttributes->setFromApp($resultEntity->getData('from_app'));
  77.  
  78. $resultEntity->setExtensionAttributes($extensionAttributes);
  79.  
  80. return $resultEntity;
  81. }
  82.  
  83. /**
  84. * @param CartRepositoryInterface $subject
  85. * @param CartSearchResultsInterface $resultCart
  86. * @return CartSearchResultsInterface
  87. */
  88. public function afterGetList(
  89. CartRepositoryInterface $subject,
  90. CartSearchResultsInterface $resultCart
  91. ) {
  92. /** @var CartInterface $order */
  93. foreach ($resultCart->getItems() as $order) {
  94. $this->afterGet($subject, $order);
  95. }
  96.  
  97. return $resultCart;
  98. }
  99.  
  100. /**
  101. * @param CartRepositoryInterface $subject
  102. * @param CartInterface $result
  103. * @return array
  104. */
  105. public function beforeSave(
  106. CartRepositoryInterface $subject,
  107. CartInterface $quote
  108. ) {
  109. $extensionAttributes = $quote->getExtensionAttributes() ?: $this->extensionFactory->create();
  110. if ($extensionAttributes !== null && $extensionAttributes->getFromApp() !== null) {
  111. $quote->setFromApp($extensionAttributes->getFromApp());
  112. }
  113.  
  114. return [$quote];
  115. }
  116. }
  117.  
  118. /**
  119. * @param CartRepositoryInterface $subject
  120. * @param CartInterface $result
  121. * @return CartInterface
  122. */
  123. public function afterSave(
  124. CartRepositoryInterface $subject,
  125. CartInterface $result
  126. ) {
  127. $extensionAttributes = $result->getExtensionAttributes() ?: $this->storeExtensionFactory->create();
  128. if ($extensionAttributes !== null && $extensionAttributes->getFromApp() !== null) {
  129. /** @var CustomEntity $customEntity */
  130. $customEntity = $this->customEntityFactory->create();
  131. $customEntity->setQuoteId($result->getId());
  132. $customEntity->setFromApp($result->getFromApp());
  133. $this->customEntityRepository->save($customEntity);
  134. }
  135.  
  136. return $result;
  137. }
Add Comment
Please, Sign In to add comment