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.

  • I would suggest around or after method. – kunj Aug 22 '18 at 12:17
  • can you please post answer? – Magecode Aug 22 '18 at 12:28
  • Plugins will not work here, plugins are for modifying the parameters or results or both. – Anshu Mishra Aug 22 '18 at 12:31
  • @Anshu then what you suggest? – Magecode Aug 22 '18 at 12:34
  • May I know the requirement. – Anshu Mishra Aug 22 '18 at 12:38
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;
    }
}
| improve this answer | |
  • 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 Aug 24 '18 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! – Shawn Abramson Aug 24 '18 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 Aug 27 '18 at 10:22
  • protected $_helper; public function __construct(\Vendor\Module\Helper\Data $helper) { $this->_helper = $helper; } – Magecode Aug 27 '18 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.

| improve this answer | |
  • 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 Aug 24 '18 at 9:26
  • I have to change minimum amount and pass my custom minimum amount value there. – Magecode Aug 24 '18 at 9:29
  • It's just an example. Your should implement your logic there. Try to replace return $result to return 1.00. – Willian Levinski Keller Sep 3 '18 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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