skip to Main Content

Here is what I need:
In file vendor/magento/module-sales-rule/Model/Rule/Action/Discount/CartFixed.php there is a public function calculate
I want to comment an If condition inside this function without modifying this core file.

if ($availableDiscountAmount > 0) {

I have created a preference inside a custom module but it is not working as expected. I really appreciate if anyone can help me on this.

5

Answers


  1. Use like this

    code/Magento/* any name or module you need from core file
    If you need some more details let me know it is just like Magento 1 but only folder is changed

    Login or Signup to reply.
  2. To customize Magento core file, you can use Preference or Plugin. More detail here

    In your case, you can use preference as the following steps:

    1. In the di.xml you can add the reference config:

    <preference for="MagentoSalesRuleModelRuleActionDiscountCartFixed" type="TrainingTestModelRuleActionDiscountCartFixed" />

    1. Create TrainingTestModelRuleActionDiscountCartFixed.php:

      • @author Bach Lee
        */

      namespace TrainingTestModelRuleActionDiscount;

      use MagentoFrameworkAppObjectManager;
      use MagentoFrameworkPricingPriceCurrencyInterface;
      use MagentoSalesRuleModelDeltaPriceRound;
      use MagentoSalesRuleModelValidator;
      use MagentoSalesRuleModelRuleActionDiscountFactory;

      class CartFixed extends MagentoSalesRuleModelRuleActionDiscountCartFixed
      {
      /**
      * @var string
      */
      private static $discountType = ‘CartFixed’;
      /**
      * @var DeltaPriceRound
      */
      private $deltaPriceRound;

      /**
       * @param Validator $validator
       * @param DataFactory $discountDataFactory
       * @param PriceCurrencyInterface $priceCurrency
       * @param DeltaPriceRound $deltaPriceRound
       */
      public function __construct(
          Validator $validator,
          DataFactory $discountDataFactory,
          PriceCurrencyInterface $priceCurrency,
          DeltaPriceRound $deltaPriceRound = null
      ) {
          $this->deltaPriceRound = $deltaPriceRound ?: ObjectManager::getInstance()->get(DeltaPriceRound::class);
      
          parent::__construct($validator, $discountDataFactory, $priceCurrency, $deltaPriceRound);
      }
      
      /**
       * @param MagentoSalesRuleModelRule $rule
       * @param MagentoQuoteModelQuoteItemAbstractItem $item
       * @param float $qty
       * @return MagentoSalesRuleModelRuleActionDiscountData
       * @throws MagentoFrameworkExceptionLocalizedException
       */
      public function calculate($rule, $item, $qty)
      {
          /** @var MagentoSalesRuleModelRuleActionDiscountData $discountData */
          $discountData = $this->discountFactory->create();
      
          $ruleTotals = $this->validator->getRuleItemTotalsInfo($rule->getId());
      
          $quote = $item->getQuote();
          $address = $item->getAddress();
      
          $itemPrice = $this->validator->getItemPrice($item);
          $baseItemPrice = $this->validator->getItemBasePrice($item);
          $itemOriginalPrice = $this->validator->getItemOriginalPrice($item);
          $baseItemOriginalPrice = $this->validator->getItemBaseOriginalPrice($item);
      
          /**
           * prevent applying whole cart discount for every shipping order, but only for first order
           */
          if ($quote->getIsMultiShipping()) {
              $usedForAddressId = $this->getCartFixedRuleUsedForAddress($rule->getId());
              if ($usedForAddressId && $usedForAddressId != $address->getId()) {
                  return $discountData;
              } else {
                  $this->setCartFixedRuleUsedForAddress($rule->getId(), $address->getId());
              }
          }
          $cartRules = $address->getCartFixedRules();
          if (!isset($cartRules[$rule->getId()])) {
              $cartRules[$rule->getId()] = $rule->getDiscountAmount();
          }
      
          $availableDiscountAmount = (float)$cartRules[$rule->getId()];
          $discountType = self::$discountType . $rule->getId();
      
          $store = $quote->getStore();
          if ($ruleTotals['items_count'] <= 1) {
              $quoteAmount = $this->priceCurrency->convert($availableDiscountAmount, $store);
              $baseDiscountAmount = min($baseItemPrice * $qty, $availableDiscountAmount);
              $this->deltaPriceRound->reset($discountType);
          } else {
              $ratio = $baseItemPrice * $qty / $ruleTotals['base_items_price'];
              $maximumItemDiscount = $this->deltaPriceRound->round(
                  $rule->getDiscountAmount() * $ratio,
                  $discountType
              );
      
              $quoteAmount = $this->priceCurrency->convert($maximumItemDiscount, $store);
      
              $baseDiscountAmount = min($baseItemPrice * $qty, $maximumItemDiscount);
              $this->validator->decrementRuleItemTotalsCount($rule->getId());
          }
      
          $baseDiscountAmount = $this->priceCurrency->round($baseDiscountAmount);
      
          $availableDiscountAmount -= $baseDiscountAmount;
          $cartRules[$rule->getId()] = $availableDiscountAmount;
          if ($availableDiscountAmount <= 0) {
              $this->deltaPriceRound->reset($discountType);
          }
      
          $discountData->setAmount($this->priceCurrency->round(min($itemPrice * $qty, $quoteAmount)));
          $discountData->setBaseAmount($baseDiscountAmount);
          $discountData->setOriginalAmount(min($itemOriginalPrice * $qty, $quoteAmount));
          $discountData->setBaseOriginalAmount($this->priceCurrency->round($baseItemOriginalPrice));
          $address->setCartFixedRules($cartRules);
      
          return $discountData;
      }
      

      }

    Or you can use cweagans/composer-patches following this question to edit core Magento files

    Regards

    Login or Signup to reply.
  3. App/Code/Magento/SalesRule/model/ *

    Please use it like this

    Login or Signup to reply.
  4. After adding this please use compile command

    Login or Signup to reply.
  5. It works for me it will work for you also

    First add folders
    App/Code/Magento/SalesRule/model/folder/file.php

    And thank use “setup:upgrade” to update the files.
    Than command “setup:di:compile”.

    Regards
    Naseem

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search