3

I want to change a file in Magento\Checkout\Model\DefaultConfigProvider file

I am trying to change data in function but I can not override funciton Where I am going to Wrong.

I have Created a File etc/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">
    <preference for="Magento\Checkout\Model\DefaultConfigProvider" type="Test\Sales\Model\Checkout\DefaultConfigProvider" />
</config>

I want to change or override function getTotalsData()

Created File in app/code/Test/Sales/Model/Checkout/DefaultConfigProvider.php

<?php
namespace Test\Sales\Model\Checkout;

class DefaultConfigProvider extends Magento\Checkout\Model\DefaultConfigProvider
{

    private function getTotalsData()
    {
        die('override');
       // Override stuff
    }

}
?>

4 Answers 4

2

As I understand your goal is to change config. You don't need to overwrite whole class for this. You can create after plugin for config modification.

Take a look onto \Magento\Persistent\Model\Checkout\ConfigProviderPlugin::afterGetConfig()

1
  • I tried but not working I want to add new product attribute in totals items array therefore I want to modify getConfig() function but not working using plugin concept Commented Dec 11, 2016 at 5:14
0

You are missing slash before Magento\Checkout\Model\DefaultConfigProvider class.

In your php class,

class DefaultConfigProvider extends Magento\Checkout\Model\DefaultConfigProvider
{

Replace with,

class DefaultConfigProvider extends \Magento\Checkout\Model\DefaultConfigProvider
{

Remove var folder and check again.

7
  • Ok let me check 1 min Commented Dec 8, 2016 at 13:21
  • please let me know if have issue, Commented Dec 8, 2016 at 13:25
  • I clear cache and remove var/generation folder but not working Commented Dec 8, 2016 at 13:27
  • I can override other model files but this one not working Commented Dec 8, 2016 at 13:29
  • You cant overrider private function so its not working for your case. Commented Dec 8, 2016 at 13:30
0

This is code i use to rewrite it, you can take a look

<?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\Checkout\Model\DefaultConfigProvider">
        <plugin name="custom_defaultconfigprovider" type="Test\Sales\Plugin\DefaultConfigProvider" />
    </type>
</config>

In file Test/Sales/Plugin/DefaultConfigProvider.php

<?php
namespace Test\Sales\Plugin;

class DefaultConfigProvider
{
    public function afterGetConfig($subject, $result)
    {
        if (isset($result['totalsData']) && $result['totalsData']) {
            $custom = // add your custom here;
            $result['totalsData'] = $custom;
        }
        return $result;
    }
}
2
  • so totalsData is the total summary? i have given some custom value there, but its not updated Commented Oct 8, 2018 at 7:30
  • totalsData is variable in vendor/magento/module-checkout/Model/DefaultConfigProvider.php function getConfig() , you can see in it
    – Init
    Commented Oct 8, 2018 at 8:44
0

Create the file app/code/Company/Module/etc/frontend/di.xml with content :

<?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\Checkout\Model\DefaultConfigProvider">
        <arguments>
            <argument name="defaultConfigProviders" xsi:type="array">
                <item name="custom" xsi:type="object">Company\Module\Model\DefaultConfigProvider</item>
            </argument>
        </arguments>
    </type>
</config>

Then create model file app/code/Company/Module/Model/DefaultConfigProvider.php :

namespace Company\Module\Model;

class DefaultConfigProvider
{
    public function afterGetConfig($subject, $result)
    {
        if (isset($result['totalsData']) && $result['totalsData']) {
            $custom = // add your custom here;
            $result['totalsData'] = $custom;
        }
        return $result;
    }
}

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.