baby_in_magento

ghufife

May 24th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.87 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. \Magento\Quote\Model\QuoteRepository $quoteModelRepository
  38. ) {
  39. $this->quoteRepository = $quoteRepository;
  40. $this->loyaltyCoupons = $loyaltyCoupons;
  41. $this->config = $config;
  42. $this->quoteModelRepository = $quoteModelRepository;
  43. }
  44.  
  45. /**
  46. * @inheritDoc
  47. */
  48. public function get($cartId)
  49. {
  50. /** @var \Magento\Quote\Model\Quote $quote */
  51. $quote = $this->quoteRepository->getActive($cartId);
  52. $couponCode = $quote->getCouponCode();
  53. if($couponCode)
  54. {
  55. $code = $quote->getCouponCode();
  56. }
  57. else
  58. {
  59. $code = $quote->getData('loyalty_coupon_code');
  60. }
  61. return $code;
  62. }
  63.  
  64. /**
  65. * @inheritDoc
  66. */
  67. public function set($cartId, $couponCode)
  68. {
  69. $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/cartcoupon.log');
  70. $logger = new \Zend_Log();
  71. $logger->addWriter($writer);
  72. $logger->info('cart coupon code MODEL');
  73.  
  74. /** @var \Magento\Quote\Model\Quote $quote */
  75. $quote = $this->quoteRepository->getActive($cartId);
  76. if (!$quote->getItemsCount()) {
  77. throw new NoSuchEntityException(__('The "%1" Cart doesn\'t contain products.', $cartId));
  78. }
  79. if (!$quote->getStoreId()) {
  80. throw new NoSuchEntityException(__('Cart isn\'t assigned to correct store'));
  81. }
  82.  
  83.  
  84. $quote->getShippingAddress()->setCollectShippingRates(true);
  85.  
  86. try {
  87.  
  88. $CouponsModuleEnabled = $this->config->isLoyaltyCouponsModuleEnabled();
  89. $logger->info('couponModuleEnabled'.$CouponsModuleEnabled.'couponCode'.$couponCode);
  90. if ($CouponsModuleEnabled) {
  91. $response = $this->loyaltyCoupons->hold($quote->getId(),$couponCode);
  92.  
  93. $success = $response["success"] ?? false;
  94. $logger->info('success'.json_encode($success));
  95. if($success)
  96. {
  97. if ($quote->getData('loyalty_coupon_code') != $couponCode) {
  98. throw new NoSuchEntityException(__("The coupon code isn't valid. Verify the code and try again."));
  99. }
  100. $quote->setCouponCode($couponCode);
  101. $this->quoteRepository->save($quote->collectTotals());
  102.  
  103.  
  104. }
  105. else
  106. {
  107. $errors = $response['errors'] ?? [];
  108. $status = $errors['status'] ?? '';
  109. if($status == 'COUPON_NOT_FOUND')
  110. {
  111. $quote->setCouponCode($couponCode);
  112. $this->quoteRepository->save($quote->collectTotals());
  113.  
  114. $subtotalWithDiscount = $quote->getData('base_subtotal_with_discount');
  115. $calculateTax = $subtotalWithDiscount * 0.03;
  116. $grandTotal = $subtotalWithDiscount + $calculateTax;
  117. $logger->info('grandTotal'.$grandTotal.'subtotalWithDiscount='.$subtotalWithDiscount.'calculateTax'.$calculateTax);
  118.  
  119. $quote = $this->quoteModelRepository->get($cartId);
  120.  
  121. $quote->setData('grand_total',$grandTotal);
  122. $quote->setData('base_grand_total',$grandTotal);
  123. $quote->save();
  124. $this->quoteRepository->save($quote->collectTotals());
  125.  
  126. $quote->setTotalsCollectedFlag(false)->collectTotals();
  127.  
  128.  
  129. if ($quote->getCouponCode() != $couponCode) {
  130. throw new NoSuchEntityException(__("The coupon code isn't valid. Verify the code and try again."));
  131. }
  132.  
  133. }
  134. else
  135. {
  136. $message = $response["message"] ?? 'Something went wrong';
  137. throw new NoSuchEntityException(__($message));
  138. }
  139. }
  140. }
  141. else
  142. {
  143. $logger->info('coupon'.$quote->getCouponCode());
  144. if ($quote->getCouponCode() != $couponCode) {
  145. throw new NoSuchEntityException(__("The coupon code isn't valid. Verify the code and try again."));
  146. }
  147. $quote->setCouponCode($couponCode);
  148. $this->quoteRepository->save($quote->collectTotals());
  149.  
  150.  
  151. }
  152.  
  153.  
  154. } catch (LocalizedException $e) {
  155. $logger->info('error-'.$e->getMessage());
  156. throw new CouldNotSaveException(__('The coupon code couldn\'t be applied: ' .$e->getMessage()), $e);
  157. } catch (\Exception $e) {
  158. throw new CouldNotSaveException(
  159. __("The coupon code couldn't be applied. Verify the coupon code and try again."),
  160. $e
  161. );
  162. }
  163.  
  164. return true;
  165. }
  166.  
  167. /**
  168. * @inheritDoc
  169. */
  170. public function remove($cartId)
  171. {
  172. /** @var \Magento\Quote\Model\Quote $quote */
  173. $quote = $this->quoteRepository->getActive($cartId);
  174. if (!$quote->getItemsCount()) {
  175. throw new NoSuchEntityException(__('The "%1" Cart doesn\'t contain products.', $cartId));
  176. }
  177. $quote->getShippingAddress()->setCollectShippingRates(true);
  178. try {
  179. $CouponsModuleEnabled = $this->config->isLoyaltyCouponsModuleEnabled();
  180. if ($CouponsModuleEnabled) {
  181. $response = $this->loyaltyCoupons->release($quote->getId());
  182. $success = $response["success"] ?? false;
  183. if($success)
  184. {
  185. $this->quoteRepository->save($quote->collectTotals());
  186. // return true;
  187. }
  188. else
  189. {
  190. $errors = $response['errors'] ?? [];
  191. $status = $errors['status'] ?? 'notFound';
  192. if($status == 'notFound')
  193. {
  194. $quote->setCouponCode('');
  195. $this->quoteRepository->save($quote->collectTotals());
  196. }
  197. else
  198. {
  199. $message = $response["message"] ?? 'Something went wrong';
  200. throw new NoSuchEntityException(__($message));
  201. }
  202. }
  203. }
  204. else
  205. {
  206. $quote->setCouponCode('');
  207. $this->quoteRepository->save($quote->collectTotals());
  208. }
  209.  
  210. } catch (\Exception $e) {
  211. throw new CouldNotDeleteException(
  212. __("The coupon code couldn't be deleted. Verify the coupon code and try again.")
  213. );
  214. }
  215. if ($quote->getCouponCode() != '') {
  216. throw new CouldNotDeleteException(
  217. __("The coupon code couldn't be deleted. Verify the coupon code and try again.")
  218. );
  219. }
  220. return true;
  221. }
  222. }
  223.  
Advertisement
Add Comment
Please, Sign In to add comment