Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Froogal\Cart\Model;
- use Magento\Framework\Exception\LocalizedException;
- use \Magento\Quote\Api\CouponManagementInterface;
- use Magento\Framework\Exception\CouldNotDeleteException;
- use Magento\Framework\Exception\CouldNotSaveException;
- use Magento\Framework\Exception\NoSuchEntityException;
- /**
- * Coupon management object.
- */
- class CouponManagement implements CouponManagementInterface
- {
- /**
- * Quote repository.
- *
- * @var \Magento\Quote\Api\CartRepositoryInterface
- */
- protected $quoteRepository;
- /**
- * Constructs a coupon read service object.
- *
- * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository Quote repository.
- */
- public function __construct(
- \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
- \Froogal\Magento\Model\LoyaltyCoupons $loyaltyCoupons,
- \Froogal\Magento\Model\Config $config,
- \Magento\Quote\Model\QuoteRepository $quoteModelRepository
- ) {
- $this->quoteRepository = $quoteRepository;
- $this->loyaltyCoupons = $loyaltyCoupons;
- $this->config = $config;
- $this->quoteModelRepository = $quoteModelRepository;
- }
- /**
- * @inheritDoc
- */
- public function get($cartId)
- {
- /** @var \Magento\Quote\Model\Quote $quote */
- $quote = $this->quoteRepository->getActive($cartId);
- $couponCode = $quote->getCouponCode();
- if($couponCode)
- {
- $code = $quote->getCouponCode();
- }
- else
- {
- $code = $quote->getData('loyalty_coupon_code');
- }
- return $code;
- }
- /**
- * @inheritDoc
- */
- public function set($cartId, $couponCode)
- {
- $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/cartcoupon.log');
- $logger = new \Zend_Log();
- $logger->addWriter($writer);
- $logger->info('cart coupon code MODEL');
- /** @var \Magento\Quote\Model\Quote $quote */
- $quote = $this->quoteRepository->getActive($cartId);
- if (!$quote->getItemsCount()) {
- throw new NoSuchEntityException(__('The "%1" Cart doesn\'t contain products.', $cartId));
- }
- if (!$quote->getStoreId()) {
- throw new NoSuchEntityException(__('Cart isn\'t assigned to correct store'));
- }
- $quote->getShippingAddress()->setCollectShippingRates(true);
- try {
- $CouponsModuleEnabled = $this->config->isLoyaltyCouponsModuleEnabled();
- $logger->info('couponModuleEnabled'.$CouponsModuleEnabled.'couponCode'.$couponCode);
- if ($CouponsModuleEnabled) {
- $response = $this->loyaltyCoupons->hold($quote->getId(),$couponCode);
- $success = $response["success"] ?? false;
- $logger->info('success'.json_encode($success));
- if($success)
- {
- if ($quote->getData('loyalty_coupon_code') != $couponCode) {
- throw new NoSuchEntityException(__("The coupon code isn't valid. Verify the code and try again."));
- }
- $quote->setCouponCode($couponCode);
- $this->quoteRepository->save($quote->collectTotals());
- }
- else
- {
- $errors = $response['errors'] ?? [];
- $status = $errors['status'] ?? '';
- if($status == 'COUPON_NOT_FOUND')
- {
- $quote->setCouponCode($couponCode);
- $this->quoteRepository->save($quote->collectTotals());
- $subtotalWithDiscount = $quote->getData('base_subtotal_with_discount');
- $calculateTax = $subtotalWithDiscount * 0.03;
- $grandTotal = $subtotalWithDiscount + $calculateTax;
- $logger->info('grandTotal'.$grandTotal.'subtotalWithDiscount='.$subtotalWithDiscount.'calculateTax'.$calculateTax);
- $quote = $this->quoteModelRepository->get($cartId);
- $quote->setData('grand_total',$grandTotal);
- $quote->setData('base_grand_total',$grandTotal);
- $quote->save();
- $this->quoteRepository->save($quote->collectTotals());
- $quote->setTotalsCollectedFlag(false)->collectTotals();
- if ($quote->getCouponCode() != $couponCode) {
- throw new NoSuchEntityException(__("The coupon code isn't valid. Verify the code and try again."));
- }
- }
- else
- {
- $message = $response["message"] ?? 'Something went wrong';
- throw new NoSuchEntityException(__($message));
- }
- }
- }
- else
- {
- $logger->info('coupon'.$quote->getCouponCode());
- if ($quote->getCouponCode() != $couponCode) {
- throw new NoSuchEntityException(__("The coupon code isn't valid. Verify the code and try again."));
- }
- $quote->setCouponCode($couponCode);
- $this->quoteRepository->save($quote->collectTotals());
- }
- } catch (LocalizedException $e) {
- $logger->info('error-'.$e->getMessage());
- throw new CouldNotSaveException(__('The coupon code couldn\'t be applied: ' .$e->getMessage()), $e);
- } catch (\Exception $e) {
- throw new CouldNotSaveException(
- __("The coupon code couldn't be applied. Verify the coupon code and try again."),
- $e
- );
- }
- return true;
- }
- /**
- * @inheritDoc
- */
- public function remove($cartId)
- {
- /** @var \Magento\Quote\Model\Quote $quote */
- $quote = $this->quoteRepository->getActive($cartId);
- if (!$quote->getItemsCount()) {
- throw new NoSuchEntityException(__('The "%1" Cart doesn\'t contain products.', $cartId));
- }
- $quote->getShippingAddress()->setCollectShippingRates(true);
- try {
- $CouponsModuleEnabled = $this->config->isLoyaltyCouponsModuleEnabled();
- if ($CouponsModuleEnabled) {
- $response = $this->loyaltyCoupons->release($quote->getId());
- $success = $response["success"] ?? false;
- if($success)
- {
- $this->quoteRepository->save($quote->collectTotals());
- // return true;
- }
- else
- {
- $errors = $response['errors'] ?? [];
- $status = $errors['status'] ?? 'notFound';
- if($status == 'notFound')
- {
- $quote->setCouponCode('');
- $this->quoteRepository->save($quote->collectTotals());
- }
- else
- {
- $message = $response["message"] ?? 'Something went wrong';
- throw new NoSuchEntityException(__($message));
- }
- }
- }
- else
- {
- $quote->setCouponCode('');
- $this->quoteRepository->save($quote->collectTotals());
- }
- } catch (\Exception $e) {
- throw new CouldNotDeleteException(
- __("The coupon code couldn't be deleted. Verify the coupon code and try again.")
- );
- }
- if ($quote->getCouponCode() != '') {
- throw new CouldNotDeleteException(
- __("The coupon code couldn't be deleted. Verify the coupon code and try again.")
- );
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment