baby_in_magento

vg

May 24th, 2024
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.41 KB | None | 0 0
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7.  
  8. namespace Froogal\Cart\Model;
  9.  
  10. use Magento\Framework\Exception\LocalizedException;
  11. use \Magento\Quote\Api\CouponManagementInterface;
  12. use Magento\Framework\Exception\CouldNotDeleteException;
  13. use Magento\Framework\Exception\CouldNotSaveException;
  14. use Magento\Framework\Exception\NoSuchEntityException;
  15.  
  16. /**
  17. * Coupon management object.
  18. */
  19. class CouponManagement implements CouponManagementInterface
  20. {
  21. /**
  22. * Quote repository.
  23. *
  24. * @var \Magento\Quote\Api\CartRepositoryInterface
  25. */
  26. protected $quoteRepository;
  27.  
  28. /**
  29. * Constructs a coupon read service object.
  30. *
  31. * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository Quote repository.
  32. */
  33. public function __construct(
  34. \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
  35. \Froogal\Magento\Model\LoyaltyCoupons $loyaltyCoupons,
  36. \Froogal\Magento\Model\Config $config
  37. ) {
  38. $this->quoteRepository = $quoteRepository;
  39. $this->loyaltyCoupons = $loyaltyCoupons;
  40. $this->config = $config;
  41. }
  42.  
  43. /**
  44. * @inheritDoc
  45. */
  46. public function get($cartId)
  47. {
  48. /** @var \Magento\Quote\Model\Quote $quote */
  49. $quote = $this->quoteRepository->getActive($cartId);
  50. $couponCode = $quote->getCouponCode();
  51. if($couponCode)
  52. {
  53. $code = $quote->getCouponCode();
  54. }
  55. else
  56. {
  57. $code = $quote->getData('loyalty_coupon_code');
  58. }
  59. return $code;
  60. }
  61.  
  62. /**
  63. * @inheritDoc
  64. */
  65. public function set($cartId, $couponCode)
  66. {
  67. $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/cartcoupon.log');
  68. $logger = new \Zend_Log();
  69. $logger->addWriter($writer);
  70. $logger->info('cart coupon code MODEL');
  71.  
  72. /** @var \Magento\Quote\Model\Quote $quote */
  73. $quote = $this->quoteRepository->getActive($cartId);
  74. if (!$quote->getItemsCount()) {
  75. throw new NoSuchEntityException(__('The "%1" Cart doesn\'t contain products.', $cartId));
  76. }
  77. if (!$quote->getStoreId()) {
  78. throw new NoSuchEntityException(__('Cart isn\'t assigned to correct store'));
  79. }
  80.  
  81.  
  82. $quote->getShippingAddress()->setCollectShippingRates(true);
  83.  
  84. try {
  85.  
  86. $CouponsModuleEnabled = $this->config->isLoyaltyCouponsModuleEnabled();
  87. $logger->info('couponModuleEnabled'.$CouponsModuleEnabled.'couponCode'.$couponCode);
  88. if ($CouponsModuleEnabled) {
  89. $response = $this->loyaltyCoupons->hold($quote->getId(),$couponCode);
  90.  
  91. $success = $response["success"] ?? false;
  92. $logger->info('success'.json_encode($success));
  93. if($success)
  94. {
  95. if ($quote->getData('loyalty_coupon_code') != $couponCode) {
  96. throw new NoSuchEntityException(__("The coupon code isn't valid. Verify the code and try again."));
  97. }
  98. $quote->setCouponCode($couponCode);
  99. $this->quoteRepository->save($quote->collectTotals());
  100.  
  101.  
  102. }
  103. else
  104. {
  105. $errors = $response['errors'] ?? [];
  106. $status = $errors['status'] ?? '';
  107. if($status == 'notFound')
  108. {
  109. $quote->setCouponCode($couponCode);
  110. $this->quoteRepository->save($quote->collectTotals());
  111.  
  112. $subtotalWithDiscount = $quote->getData('base_subtotal_with_discount');
  113. $calculateTax = $subtotalWithDiscount * 0.03;
  114. $grandTotal = $subtotalWithDiscount + $calculateTax;
  115. $logger->info('grandTotal'.$grandTotal.'subtotalWithDiscount='.$subtotalWithDiscount);
  116.  
  117.  
  118. $quote->setGrandTotal($grandTotal);
  119. $this->quoteRepository->save($quote->collectTotals());
  120.  
  121. if ($quote->getCouponCode() != $couponCode) {
  122. throw new NoSuchEntityException(__("The coupon code isn't valid. Verify the code and try again."));
  123. }
  124.  
  125. }
  126. else
  127. {
  128. $message = $response["message"] ?? 'Something went wrong';
  129. throw new NoSuchEntityException(__($message));
  130. }
  131. }
  132. }
  133. else
  134. {
  135. $logger->info('coupon'.$quote->getCouponCode());
  136. if ($quote->getCouponCode() != $couponCode) {
  137. throw new NoSuchEntityException(__("The coupon code isn't valid. Verify the code and try again."));
  138. }
  139. $quote->setCouponCode($couponCode);
  140. $this->quoteRepository->save($quote->collectTotals());
  141.  
  142.  
  143. }
  144.  
  145.  
  146. } catch (LocalizedException $e) {
  147. throw new CouldNotSaveException(__('The coupon code couldn\'t be applied: ' .$e->getMessage()), $e);
  148. } catch (\Exception $e) {
  149. throw new CouldNotSaveException(
  150. __("The coupon code couldn't be applied. Verify the coupon code and try again."),
  151. $e
  152. );
  153. }
  154.  
  155. return true;
  156. }
  157.  
  158. /**
  159. * @inheritDoc
  160. */
  161. public function remove($cartId)
  162. {
  163. /** @var \Magento\Quote\Model\Quote $quote */
  164. $quote = $this->quoteRepository->getActive($cartId);
  165. if (!$quote->getItemsCount()) {
  166. throw new NoSuchEntityException(__('The "%1" Cart doesn\'t contain products.', $cartId));
  167. }
  168. $quote->getShippingAddress()->setCollectShippingRates(true);
  169. try {
  170. $CouponsModuleEnabled = $this->config->isLoyaltyCouponsModuleEnabled();
  171. if ($CouponsModuleEnabled) {
  172. $response = $this->loyaltyCoupons->release($quote->getId());
  173. $success = $response["success"] ?? false;
  174. if($success)
  175. {
  176. $this->quoteRepository->save($quote->collectTotals());
  177. // return true;
  178. }
  179. else
  180. {
  181. $errors = $response['errors'] ?? [];
  182. $status = $errors['status'] ?? 'notFound';
  183. if($status == 'notFound')
  184. {
  185. $quote->setCouponCode('');
  186. $this->quoteRepository->save($quote->collectTotals());
  187. }
  188. else
  189. {
  190. $message = $response["message"] ?? 'Something went wrong';
  191. throw new NoSuchEntityException(__($message));
  192. }
  193. }
  194. }
  195. else
  196. {
  197. $quote->setCouponCode('');
  198. $this->quoteRepository->save($quote->collectTotals());
  199. }
  200.  
  201. } catch (\Exception $e) {
  202. throw new CouldNotDeleteException(
  203. __("The coupon code couldn't be deleted. Verify the coupon code and try again.")
  204. );
  205. }
  206. if ($quote->getCouponCode() != '') {
  207. throw new CouldNotDeleteException(
  208. __("The coupon code couldn't be deleted. Verify the coupon code and try again.")
  209. );
  210. }
  211. return true;
  212. }
  213. }
  214.  
Advertisement
Add Comment
Please, Sign In to add comment