0

I have to override magento 2 code model file in the custom module.

I have to override below method from Magento\Quote\Model\Quote\Address to my custom module and I have to change value of $amount.

public function validateMinimumAmount()
{
    $storeId = $this->getQuote()->getStoreId();
    $validateEnabled = $this->_scopeConfig->isSetFlag(
        'sales/minimum_order/active',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
        $storeId
    );
    if (!$validateEnabled) {
        return true;
    }

    if (!$this->getQuote()->getIsVirtual() xor $this->getAddressType() == self::TYPE_SHIPPING) {
        return true;
    }

    $amount = $this->_scopeConfig->getValue(
        'sales/minimum_order/amount',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
        $storeId
    );
    $taxInclude = $this->_scopeConfig->getValue(
        'sales/minimum_order/tax_including',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
        $storeId
    );
    $taxes = $taxInclude ? $this->getBaseTaxAmount() : 0;

    return ($this->getBaseSubtotalWithDiscount() + $taxes >= $amount);
} 

app/code/vendor/module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\Quote\Address">
        <plugin name="my_address_plugin" type="vendor\module\Plugin\AddressPlugin" sortOrder="1" />
    </type>    
</config>

can anyone please help? I'm not sure which method to use before, after or around and how to set custom value for $amount using my plugin.

13
  • I would suggest around or after method.
    – kunj
    Commented Aug 22, 2018 at 12:17
  • can you please post answer?
    – Magecode
    Commented Aug 22, 2018 at 12:28
  • Plugins will not work here, plugins are for modifying the parameters or results or both. Commented Aug 22, 2018 at 12:31
  • @Anshu then what you suggest?
    – Magecode
    Commented Aug 22, 2018 at 12:34
  • May I know the requirement. Commented Aug 22, 2018 at 12:38

3 Answers 3

0

Ok, here is how to accomplish this with argument replacement. Essentially, we are replacing the current instance of Magento\Framework\App\Config\ScopeConfigInterface but ONLY for the one specific class. This is the preferred method because we don't want to do a full class override like some commenters suggest, that would just be greedy. In this case, we accomplish our goal with minimal interference to the rest of the code.

In your di.xml

<type name="Magento\Quote\Model\Quote\Address">
    <arguments>
        <argument name="scopeConfig" xsi:type="object">Vendorname\Modulename\Model\ScopeConfig</argument>
    </arguments>
</type>

Create a model at Model/ScopeConfig.php

<?php

namespace Vendorname\Modulename\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;

class ScopeConfig extends \Magento\Framework\App\Config
{
    /**
     * @param null|string $path
     * @param string      $scope
     * @param null|string $scopeCode
     *
     * @return mixed|string
     */
    public function getValue(
        $path = null,
        $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
        $scopeCode = null
    ) {
        $result = parent::getValue($path, $scope, $scopeCode);

        if ($path === 'sales/minimum_order/amount') {
            $result = 'do your logic and return some special value here';
        }

        return $result;
    }
}
4
  • This Model code is executing but some issue I have changed $result = 60. In cart product with $59 and tax is $5.90 so total is $64.90. And in admin Include Tax to Amount is set to Yes, but although "Proceed to checkout" is disabled.
    – Magecode
    Commented Aug 24, 2018 at 6:11
  • I can't write the entire module for you. However, I did show you the best way to modify the $amount variable as requested in your question. If you have the ability to modify that value as requested please accept the answer. Thank you! Commented Aug 24, 2018 at 6:15
  • I need one help. when I'm going to call helper in this code. it's not working. Fatal error: Uncaught Error: Call to a member function resolve() on null in magento225\vendor\magento\framework\App\Config.php:69
    – Magecode
    Commented Aug 27, 2018 at 10:22
  • protected $_helper; public function __construct(\Vendor\Module\Helper\Data $helper) { $this->_helper = $helper; }
    – Magecode
    Commented Aug 27, 2018 at 10:22
0

I suggest you use after method to change the $amount response. Please follow:

app/code/Vendor/Module/etc/di.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\Quote\Address">
        <plugin name="my_address_plugin" type="vendor\Module\Plugin\AddressPlugin" sortOrder="1" />
    </type>    
</config>

app/code/Vendor/Module/Plugin/AddressPlugin

use Magento\Quote\Model\Quote\Address as QuoteAddress;

class AddressPlugin
{

    /**
     * @param QuoteAddress $subject
     * @param boolean $result
     * @return boolean
     */
    public function afterValidateMinimumAmount(QuoteAddress $subject, $result)
    {
        if ($handle) {
            return ($handle >= $result);
        }
        return $result;
    }
}

The $result is the current method response. So you can manipulate as you want, for example, using a $handle to do something you need.

3
  • Can you please write code snippet in brief. I have tried but it is not working. It's taking minimum amount from default magento not from plugin file
    – Magecode
    Commented Aug 24, 2018 at 9:26
  • I have to change minimum amount and pass my custom minimum amount value there.
    – Magecode
    Commented Aug 24, 2018 at 9:29
  • It's just an example. Your should implement your logic there. Try to replace return $result to return 1.00. Commented Sep 3, 2018 at 11:32
0

In your case, you want to override the validateMinimumAmount method in the Magento\Quote\Model\Quote\Address class. You can achieve this using a plugin.

Create a plugin class in your module:

app/code/Vendor/Module/Plugin/AddressPlugin.php

namespace Vendor\Module\Plugin;

class AddressPlugin
{
 /**
 * @param \Magento\Quote\Model\Quote\Address $subject
 * @param \Closure $proceed
 * @return bool
 */
public function aroundValidateMinimumAmount(
    \Magento\Quote\Model\Quote\Address $subject,
    \Closure $proceed
) {
    // Your custom logic to set a new value for $amount
    $newAmount = 100; // Set your custom value

    // Override the $amount value
    $this->setCustomAmount($newAmount);

    // Call the original method
    return $proceed();
}

/**
 * Set a custom value for $amount
 *
 * @param float $amount
 */
 private function setCustomAmount($amount)
 {
     // Implement your logic to set the custom value for $amount
  }
}

app/code/Vendor/Module/etc/di.xml

     <?xml version="1.0"?>
     <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
          <type name="Magento\Quote\Model\Quote\Address">
          <plugin name="my_address_plugin" type="Vendor\Module\Plugin\AddressPlugin" sortOrder="1" />
          </type>
     </config>

This plugin uses the around method to modify the behavior of the validateMinimumAmount method. The around method allows you to execute custom code before and after the original method, and you can modify the input parameters and return value as needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.