cancel
Showing results for 
Search instead for 
Did you mean: 

Magento EE 2.3.5-P2: Customer Store Credit Remove After Selecting Payment Method On Checkout

Magento EE 2.3.5-P2: Customer Store Credit Remove After Selecting Payment Method On Checkout

I'm using Magento EE 2.3.5-P2, The Main issue I'm getting is when customer use Store Credit for their order, goes to checkout, select shipping method, proceed to payment, when customer select payment Store Credit remove, This is all happening because for Custom Shipping Method, I made Plugin Magento\Shipping\Model\Rate\Result for rendering Custom Shipping Method and their Rates. When I disable this Plugin Store Credit works fine. I debug code and found that $quote = $this->session->getQuote(); is the reason for this issue but I didn't found any alternative for this. It's been a week, I'm trying to solve this issue, Please help me out.

 

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <type name="Magento\Shipping\Model\Rate\Result">
        <plugin name="shippingrules_update_rate_result"
                type="Vendor\Flatshipping\Plugin\Shipping\Rate\Result\Append"
                sortOrder="10"
                disabled="false"/>
        <plugin name="shippingrules_update_disabled_or_enabled_rates"
                type="Vendor\Flatshipping\Plugin\Shipping\Rate\Result\GetAllRates"
                sortOrder="11"
                disabled="false"/>
    </type>
</config>

app/code/Vendor/Flatshipping/Plugin/Shipping/Rate/Result/GetAllRates

 

<?php

namespace Vendor\Flatshipping\Plugin\Shipping\Rate\Result;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ProductFactory;
use Magento\Checkout\Exception;
use Magento\Framework\Exception\StateException;
use \Magento\Store\Model\ScopeInterface;
use Plumtree\Flatshipping\Helper\GetCoupon;
use Plumtree\Flatshipping\Helper\Shipping;
/**
 * Class Append
 * @package Vendor\Flatshipping\Plugin\Shipping\Rate\Result
 */
class Append
{
    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $session;

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $_scopeConfig;

    /**
     * @var \Plumtree\Flatshipping\Helper\GetCoupon
     */
    protected $_getCoupon;

    protected $_shipping;

  /**
   * @var \Swarming\SubscribePro\Helper\QuoteItem
   */
  protected $_quoteItem;

  /**
   * @var \Magento\Framework\Message\ManagerInterface
   */
  protected $_messageManager;

  /**
   * @var ProductFactory
   */
  protected $_productFactory;

  /**
   * Append constructor.
   * @param GetCoupon $_getCoupon
   * @param ProductFactory $productFactory
   * @param \Magento\Framework\Message\ManagerInterface $messageManager
   * @param \Swarming\SubscribePro\Helper\QuoteItem $quoteItem
   * @param GetCoupon $getCoupon
   * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
   * @param \Magento\Checkout\Model\Session $checkoutSession
   * @param \Magento\Backend\Model\Session\Quote $backendQuoteSession
   * @param \Magento\Framework\App\State $state
   * @throws \Magento\Framework\Exception\LocalizedException
   */
  public function __construct(
            \Plumtree\Flatshipping\Helper\GetCoupon $_getCoupon,
            \Magento\Catalog\Model\ProductFactory $productFactory,
            \Magento\Framework\Message\ManagerInterface $messageManager,
            \Swarming\SubscribePro\Helper\QuoteItem $quoteItem,
            \Plumtree\Flatshipping\Helper\GetCoupon $getCoupon,
            \Plumtree\Flatshipping\Helper\Shipping $shipping,
            \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
            \Magento\Checkout\Model\Session $checkoutSession,
            \Magento\Backend\Model\Session\Quote $backendQuoteSession,
            \Magento\Framework\App\State $state
    ) {
        $this->_productFactory = $productFactory;
        $this->_messageManager = $messageManager;
        $this->_quoteItem   = $quoteItem;
        $this->_getCoupon   = $getCoupon;
        $this->_shipping   = $shipping;
        $this->_scopeConfig = $scopeConfig;
        $state->getAreaCode() == \Magento\Framework\App\Area::AREA_ADMINHTML
                ? $this->session = $backendQuoteSession
                : $this->session = $checkoutSession;
    }

  /**
   * Validate each shipping method before append.
   * Apply the rules action if validation was successful.
   * Can mark some rules as disabled. The disabled rules will be removed in the class
   * @see \Vendor\Flatshipping\Plugin\Shipping\Rate\Result\GetAllRates
   * by checking the value of this mark in the rate object.
   *
   * NOTE: If you have some problems with the rules and the shipping methods, start debugging from here.
   * @param $subject
   * @param $result
   * @return array
   * @throws \Exception
   */
  public function beforeAppend($subject, $result)
    {
        if (!$result instanceof \Magento\Quote\Model\Quote\Address\RateResult\Method) {
            return [$result];
        }

        $filtableMethods = [
                'flatrate_flatrate',
                'ups_XDM',
                'ups_XPR',
                'ups_WXS',
                'carrier_method',
                'flatrate10_flatrate10',
                'flatrate9_flatrate9',
                'flatrate8_flatrate8',
                'flatrate7_flatrate7',
                'flatrate6_flatrate6',
                'flatrate5_flatrate5',
                'flatrate4_flatrate4',
                'flatrate3_flatrate3',
                'flatrate2_flatrate2',
                'flatrate1_flatrate1',
                'freeshipping_freeshipping'
        ];
        $methodCode = $result->getCarrier() . '_' . $result->getMethod();
        if (!in_array($methodCode, $filtableMethods)) {
            return [$result];
        }

        /** @var \Magento\Quote\Model\Quote $quote */
        $quote                          = $this->session->getQuote();
        $items                          = $quote->getAllVisibleItems();
        $subTotal                       = $quote->getSubtotal();
        $streetAddress                  = $quote->getShippingAddress()->getStreetFull();
        $countryCode                    = $quote->getShippingAddress()->getCountry();
        $regionId                       = $quote->getShippingAddress()->getRegionId();
        $region                         = $quote->getShippingAddress()->getRegion();
        $freeShippingAllowSpecific      = $this->scopeConfig('carriers/freeshipping/sallowspecific');
        $freeShipCartRuleAllowSpecific  = $this->scopeConfig('carriers/freeship/sallowspecific');
        $freeShippingCountryCode        = $this->scopeConfig('carriers/freeshipping/specificcountry');
        $flatRate1CountryCode           = $this->scopeConfig('carriers/flatrate1/specificcountry');
        $flatRate2CountryCode           = $this->scopeConfig('carriers/flatrate2/specificcountry');
        $flatRate4CountryCode           = $this->scopeConfig('carriers/flatrate4/specificcountry');
        $flatRate6CountryCode           = $this->scopeConfig('carriers/flatrate6/specificcountry');
        $freeShipCartRuleCountryCode    = $this->scopeConfig('carriers/freeship/specificcountry');
        $flatRate4RegionId              = $this->scopeConfig('carriers/flatrate4/specificregion');
        $freeShippingRateActive         = $this->scopeConfig('carriers/freeshipping/active');
        $flatRate1Active                = $this->scopeConfig('carriers/flatrate1/active');
        $flatRate2Active                = $this->scopeConfig('carriers/flatrate2/active');
        $flatRate4Active                = $this->scopeConfig('carriers/flatrate4/active');
        $flatRate6Active                = $this->scopeConfig('carriers/flatrate6/active');
        $freeShippingRateName           = $this->scopeConfig('carriers/freeshipping/name');
        $flatRate1Name                  = $this->scopeConfig('carriers/flatrate1/name');
        $flatRate2Name                  = $this->scopeConfig('carriers/flatrate2/name');
        $flatRate4Name                  = $this->scopeConfig('carriers/flatrate4/name');
        $flatRate6Name                  = $this->scopeConfig('carriers/flatrate6/name');
        $arrayOfSelectedRegionIdFlat4                 = explode(",", $flatRate4RegionId);
        $arrayOfSelectedCountryCodeFreeShip           = explode(",", $freeShippingCountryCode);
        $arrayOfSelectedCountryCodeFlat1              = explode(",", $flatRate1CountryCode);
        $arrayOfSelectedCountryCodeFlat2              = explode(",", $flatRate2CountryCode);
        $arrayOfSelectedCountryCodeFlat4              = explode(",", $flatRate4CountryCode);
        $arrayOfSelectedCountryCodeFlat6              = explode(",", $flatRate6CountryCode);
        $arrayOfSelectedCountryCodeFreeShipCarrier    = explode(",", $freeShipCartRuleCountryCode);
        $armedForceStateSelected = false;

        $breakedAddress = explode(' ', strtolower($streetAddress));
      if ((in_array('po', $breakedAddress) !== false || in_array('p.o.', $breakedAddress) !== false || in_array('p.o', $breakedAddress) !== false || in_array('p.o.box', $breakedAddress) !== false)
              && strpos( strtolower($result->getMethodTitle()) , 'express' ) !== false){
        $result->setIsDisabled(true);
      }

      if ($flatRate4Active
                && $flatRate4Name == 'FPO/APO'
                && in_array($countryCode, $arrayOfSelectedCountryCodeFlat4)
                && in_array($regionId, $arrayOfSelectedRegionIdFlat4)){
            $armedForceStateSelected = true;
            $methodCode == 'flatrate1_flatrate1' ? $result->setIsDisabled(true) : null;
            $methodCode == 'flatrate2_flatrate2' ? $result->setIsDisabled(true) : null;
            $methodCode == 'flatrate3_flatrate3' ? $result->setIsDisabled(true) : null;
            $methodCode == 'flatrate5_flatrate5' ? $result->setIsDisabled(true) : null;
            $methodCode == 'flatrate6_flatrate6' ? $result->setIsDisabled(true) : null;
            $methodCode == 'freeshipping_freeshipping' ? $result->setIsDisabled(true) : null;
        } elseif ($methodCode == 'flatrate4_flatrate4') {
            $result->setIsDisabled(true);
        }

        if ($countryCode          == 'US'
                &&!$armedForceStateSelected
                && $flatRate1Active
                && $flatRate2Active
                && $flatRate6Active
                && $flatRate1Name == 'Express Service'
                && $flatRate2Name == 'Priority Service'
                && $flatRate6Name == 'Standard'
                && in_array($countryCode, $arrayOfSelectedCountryCodeFlat1)
                && in_array($countryCode, $arrayOfSelectedCountryCodeFlat2)
                && in_array($countryCode, $arrayOfSelectedCountryCodeFlat6)
                && $subTotal < 50) {
            $methodCode == 'flatrate3_flatrate3' ? $result->setIsDisabled(true) : null;
            $methodCode == 'flatrate4_flatrate4' ? $result->setIsDisabled(true) : null;
            $methodCode == 'flatrate5_flatrate5' ? $result->setIsDisabled(true) : null;
            $methodCode == 'freeshipping_freeshipping' ? $result->setIsDisabled(true) : null;
        } elseif ($countryCode              == 'US'
                && !$armedForceStateSelected
                && $freeShippingRateActive
                && $flatRate1Active
                && $flatRate2Active
                && $flatRate1Name           == 'Express Service'
                && $freeShippingRateName    == 'Standard'
                && $flatRate2Name           == 'Priority Service'
                && in_array($countryCode, $arrayOfSelectedCountryCodeFlat1)
                && (in_array($countryCode, $arrayOfSelectedCountryCodeFreeShip) || !$freeShippingAllowSpecific)
                && in_array($countryCode, $arrayOfSelectedCountryCodeFlat2)
                && $subTotal >= 50) {
            $methodCode == 'flatrate2_flatrate2' ? $result->setPrice(15.95) : null;
            $methodCode == 'flatrate3_flatrate3' ? $result->setIsDisabled(true) : null;
            $methodCode == 'flatrate4_flatrate4' ? $result->setIsDisabled(true) : null;
            $methodCode == 'flatrate5_flatrate5' ? $result->setIsDisabled(true) : null;
            $methodCode == 'flatrate6_flatrate6' ? $result->setIsDisabled(true) : null;
        }

        if ($countryCode == 'CA'
                && $subTotal >= 50) {
            $methodCode == 'flatrate3_flatrate3' ? $result->setPrice(18.95) : null;
            $methodCode == 'flatrate7_flatrate7' ? $result->setPrice(24.95) : null;
            $methodCode == 'flatrate_flatrate'   ? $result->setPrice(29.95) : null;
        }

        if ($countryCode == 'GU'
                && $subTotal >= 50) {
            $methodCode == 'flatrate8_flatrate8'     ? $result->setPrice(0) : null;
            $methodCode == 'flatrate9_flatrate9'     ? $result->setPrice(15.95) : null;
            $methodCode == 'flatrate10_flatrate10'   ? $result->setPrice(24.95) : null;
        }

        if ($countryCode == 'AU') {
            $methodCode == 'flatrate_flatrate'   ? $result->setCarrierTitle('2-5 business days') : null;
            $methodCode == 'flatrate_flatrate'   ? $result->setMethodTitle('Standard') : null;
        }

        if ($countryCode == 'AU'
                && $subTotal >= 50) {
            $methodCode == 'flatrate_flatrate'   ? $result->setPrice(39.95) : null;
        }

        foreach ($items as $item){
            $isSubscribed = $this->_quoteItem->hasSubscription($item);
            $sku      = $item->getSku();
            $skuArray = explode('-', $sku);
            $childSku = isset($skuArray[1]) ? $skuArray[1] : $skuArray[0];
            $product  = $this->_productFactory->create()->loadByAttribute('sku', $childSku);
            $hasMat       = $product->getHazMat();
            if (isset($hasMat)
                    && $hasMat
                    ) {
              if ($region == 'Hawaii'
                      || $region == 'Alaska'
                      || $region == 'Puerto Rico'
                      || $region == 'Guam'
                      || $region == 'Armed Forces Africa'
                      || $region == 'Armed Forces Americas'
                      || $region == 'Armed Forces Canada'
                      || $region == 'Armed Forces Europe'
                      || $region == 'Armed Forces Middle East'
                      || $region == 'Armed Forces Pacific'
              ) {
                $methodCode == 'flatrate1_flatrate1' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate2_flatrate2' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate3_flatrate3' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate4_flatrate4' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate5_flatrate5' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate6_flatrate6' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate7_flatrate7' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate8_flatrate8' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate9_flatrate9' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate10_flatrate10' ? $result->setIsDisabled(true) : null;
                $methodCode == 'freeshipping_freeshipping' ? $result->setIsDisabled(true) : null;
                throw new StateException(__('Due to alcohol content, one or more products in your bag are only available to be shipped via ground with standard shipping and cannot be shipped to Alaska, Hawaii, Puerto Rico or military addresses. If priority/ express shipping is needed, please contact our customer care team at 1-888-983-6788 so we can help you.'));
              } else {
                $methodCode == 'flatrate_flatrate' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate1_flatrate1' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate2_flatrate2' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate4_flatrate4' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate5_flatrate5' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate7_flatrate7' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate8_flatrate8' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate9_flatrate9' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate10_flatrate10' ? $result->setIsDisabled(true) : null;
              }
            }
            if($isSubscribed == 1) {
                $methodCode == 'flatrate1_flatrate1' ? $result->setIsDisabled(false) : null;
                $methodCode == 'flatrate2_flatrate2' ? $result->setIsDisabled(false) : null;
                $methodCode == 'flatrate2_flatrate2' ? $result->setPrice(15.95) : null;
                $methodCode == 'flatrate3_flatrate3' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate4_flatrate4' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate5_flatrate5' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate6_flatrate6' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate7_flatrate7' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate8_flatrate8' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate9_flatrate9' ? $result->setIsDisabled(true) : null;
                $methodCode == 'flatrate10_flatrate10' ? $result->setIsDisabled(true) : null;
                $methodCode == 'freeshipping_freeshipping' ? $result->setIsDisabled(false) : null;
            }
        }     
      return [$result];
    }

    /**
     * @param $path
     * @return mixed
     */
    public function scopeConfig($path){
        return $this->_scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE);
    }
}

 

MODIFICATION IN app/code/Vendor/Flatshipping/Plugin/Shipping/Rate/Result/GetAllRates

//$quote                          = $this->session->getQuote();
//$items                          = $quote->getAllVisibleItems();
$subTotal                       = 100;
$streetAddress                  = 'po p.o. p.o';
$countryCode                    = 'US';
$regionId                       = 43;
//$region                         = $quote->getShippingAddress()->getRegion();

When I remove quote->session->getQuote() everything get to normal.

1 REPLY 1

Re: Magento EE 2.3.5-P2: Customer Store Credit Remove After Selecting Payment Method On Checkout

@Partab Saif 

You need to add the below line in the controller file that has been called when we choose the payment method:

$quote->collectTotals()->save();
Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.