1

I am overriding private method of model file of checkout module

below is my di.xml file.

<preference for="Magento\Checkout\Model\DefaultConfigProvider" type="Vendor\Module\Model\DefaultConfigProvider" />

An here is my model file.

 use Magento\Customer\Api\CustomerRepositoryInterface;
 class DefaultConfigProvider extends 
 \Magento\Checkout\Model\DefaultConfigProvider
{
  protected $customerRepository;
  public function __construct(
   CustomerRepositoryInterface $customerRepository
   ) {
     $this->customerRepository = $customerRepository;
}
 private function getCustomerData()
  {
    $customerData = [];
    if ($this->isCustomerLoggedIn()) {
        $customer = $this->customerRepository->getById($this->customerSession->getCustomerId());
        $customerMasterId = $customer->getMasterId();
        $masterCustomer = $this->customerRepository->getById($customerMasterId);
        $customerData = $customer->__toArray();
        foreach ($masterCustomer->getAddresses() as $key => $address) {
            $customerData['addresses'][$key]['inline'] = $this->getCustomerAddressInline($address);
        }
     }
     return $customerData;
  }

}

I am not able to override this function since it is private, I did research an found that we need to use plugin method for it, but not found which plugin and how to use in this scenario.

All i am looking is master customer have address so i need to read address from master customer instead of logged customer.

Please anyone suggest me how can i override that method to add my custom logic inside. Thanks in Advance.

7
  • A plugin can be used in public functions only. Commented Oct 9, 2018 at 10:26
  • so we can override private functions into custom modules? Commented Oct 9, 2018 at 10:27
  • Oh. I didn't notice that it is a private function. :) Commented Oct 9, 2018 at 10:28
  • yes, how can i achieve above functionality? Commented Oct 9, 2018 at 10:32
  • Wait. I am updating the answer. Commented Oct 9, 2018 at 10:32

3 Answers 3

7

Private function is not override:

so you have better option that you can override getConfig method of Magento\Checkout\Model\DefaultConfigProvider

and use

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\Checkout\Model\DefaultConfigProvider">
        <plugin name="plugin_getconfig" type="Vendor\Module\Plugin\DefaultConfigProvider" />
    </type>
</config>

Vendor\Module\Plugin\DefaultConfigProvider

private function getcustomCustomerData()
{
    $customerData = [];
    if ($this->isCustomerLoggedIn()) {
        $customer = $this->customerRepository->getById($this->customerSession->getCustomerId());
        $customerMasterId = $customer->getMasterId();
        $masterCustomer = $this->customerRepository->getById($customerMasterId);
        $customerData = $customer->__toArray();
        foreach ($masterCustomer->getAddresses() as $key => $address) 
        {
            $customerData['addresses'][$key]['inline'] = $this->getCustomerAddressInline($address);
        }
    }
    return $customerData;
}

public function aftergetConfig(\Magento\Checkout\Model\DefaultConfigProvider $result){
    $output['customerData'] =  ***$this->getcustomCustomerData();***
      
    return $output;
}

make getcustomCustomerData() method what you want to add logic add over here

11
  • hi @Rutvee, i am not cleared about your answer, can you please update me code, as per my question issue Commented Oct 9, 2018 at 10:41
  • @jafarpinjar You are trying to override getCustomerData method but it's private so you have to override getConfig() method and $output['customerData'] = $this->getcustomCustomerData(); and use her custom method instead of getCustomerData Commented Oct 9, 2018 at 10:43
  • hi @Rutvee, got something now, but what are the code changes i need to do in my di.xml as well as model file please Commented Oct 9, 2018 at 10:46
  • @jafarpinjar you have already write in your di.xml <preference for="Magento\Checkout\Model\DefaultConfigProvider" type="Vendor\Module\Model\DefaultConfigProvider" /> so no need any change overther Commented Oct 9, 2018 at 10:47
  • @jafarpinjar just add above code in your Vendor\Module\Model\DefaultConfigProvider Commented Oct 9, 2018 at 10:48
5

Instead of overriding getCustomerData function, create a plugin for getConfig function.

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\Checkout\Model\DefaultConfigProvider">
        <plugin name="customize_defaultconfig" type="Vendor\Module\Plugin\DefaultConfigProvider" />
    </type>
</config>

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

<?php

namespace Vendor\Module\Plugin;

class DefaultConfigProvider
{
    public function afterGetConfig(
        \Magento\Checkout\Model\DefaultConfigProvider $subject,
        $result
    ) {
        $result['customerData'] = $this->getCustomerData();
        return $result;
    }

}


public function getCustomerData()
{
    $customerData = [];
    // Get the current customer id here and add your logic.
    return $customerData;
}
4
  • hi @Dinesh, I want to add logic for customerData, please check my question Commented Oct 9, 2018 at 10:45
  • Yes but eventually it is going to be used in getConfig function. So you can add your logic here. Commented Oct 9, 2018 at 10:46
  • yes @Dinesh, but customerData, is empty now, because no customer have address, but his parent have address, so i need to alter the logic for customerData, Commented Oct 9, 2018 at 10:48
  • So you can just empty the variable $result['customerData']; and then add your custom function in this plugin and add your logic for customer address in that function. Please check my updated answer Commented Oct 9, 2018 at 11:12
0

No, you can't override private method in child class.

But you can override public method then call it to your custom private method.

private function _customGetCustomerData() {
    $customerData = [];
    if ($this->isCustomerLoggedIn()) {
        $customer = $this->customerRepository->getById($this->customerSession->getCustomerId());
        $customerMasterId = $customer->getMasterId();
        $masterCustomer = $this->customerRepository->getById($customerMasterId);
        $customerData = $customer->__toArray();
        foreach ($masterCustomer->getAddresses() as $key => $address) {
            $customerData['addresses'][$key]['inline'] = $this->getCustomerAddressInline($address);
        }
    }
    return $customerData;
}

Then override getConfig function, inside that function call to your custom function instead of $output['customerData'] = $this->getCustomerData();

public function getConfig() {
    $quoteId = $this->checkoutSession->getQuote()->getId();
    $output['formKey'] = $this->formKey->getFormKey();
    $output['customerData'] = $this->_customGetCustomerData(); // use your function instead
    $output['quoteData'] = $this->getQuoteData();
    $output['quoteItemData'] = $this->getQuoteItemData();
    $output['isCustomerLoggedIn'] = $this->isCustomerLoggedIn();
    $output['selectedShippingMethod'] = $this->getSelectedShippingMethod();
    $output['storeCode'] = $this->getStoreCode();
    $output['isGuestCheckoutAllowed'] = $this->isGuestCheckoutAllowed();
    $output['registerUrl'] = $this->getRegisterUrl();
    $output['checkoutUrl'] = $this->getCheckoutUrl();
    $output['defaultSuccessPageUrl'] = $this->getDefaultSuccessPageUrl();
    $output['pageNotFoundUrl'] = $this->pageNotFoundUrl();
    $output['forgotPasswordUrl'] = $this->getForgotPasswordUrl();
    $output['staticBaseUrl'] = $this->getStaticBaseUrl();
    $output['priceFormat'] = $this->localeFormat->getPriceFormat(
        null,
        $this->checkoutSession->getQuote()->getQuoteCurrencyCode()
    );
    $output['basePriceFormat'] = $this->localeFormat->getPriceFormat(
        null,
        $this->checkoutSession->getQuote()->getBaseCurrencyCode()
    );
    $output['postCodes'] = $this->postCodesConfig->getPostCodes();
    $output['imageData'] = $this->imageProvider->getImages($quoteId);
    $output['totalsData'] = $this->getTotalsData();
    $output['shippingPolicy'] = [
        'isEnabled' => $this->scopeConfig->isSetFlag(
            'shipping/shipping_policy/enable_shipping_policy',
            ScopeInterface::SCOPE_STORE
        ),
        'shippingPolicyContent' => nl2br(
            $this->scopeConfig->getValue(
                'shipping/shipping_policy/shipping_policy_content',
                ScopeInterface::SCOPE_STORE
            )
        )
    ];
    $output['activeCarriers'] = $this->getActiveCarriers();
    $output['originCountryCode'] = $this->getOriginCountryCode();
    $output['paymentMethods'] = $this->getPaymentMethods();
    $output['autocomplete'] = $this->isAutocompleteEnabled();
    $output['displayBillingOnPaymentMethod'] = $this->checkoutHelper->isDisplayBillingOnPaymentMethodAvailable();
    return $output;
}
2
  • Please update me code, how can i override as per my question issue? Commented Oct 9, 2018 at 10:43
  • updated code above
    – Draze
    Commented Oct 9, 2018 at 10:48

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.