Overriding private method of Model file in Magento 2

by jafar pinjar   Last Updated October 09, 2018 11:09 AM

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.



Answers 3


Private function is not override:

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

and use

in di.xml

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

========================

and in Vendor\Module\Model\DefaultConfigProvider class add below code

   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 getConfig()
{
    $quoteId = $this->checkoutSession->getQuote()->getId();
    $output['formKey'] = $this->formKey->getFormKey();
    $output['customerData'] =  ***$this->getcustomCustomerData();***
    $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;
}

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

Rutvee Sojitra
Rutvee Sojitra
October 09, 2018 10:33 AM

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;
}
Draze
Draze
October 09, 2018 10:40 AM

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
    ) {
        $customerData = $result['customerData'];
        // You can get the customer data here.
        // Add your customization here.

        return $result;
    }

}
Dinesh Yadav
Dinesh Yadav
October 09, 2018 10:44 AM

Related Questions


Customer model file override issue in Magento2

Updated October 09, 2018 06:09 AM

How to override customer resource module customer.php

Updated November 22, 2017 07:09 AM

Magento 2 Override Private method

Updated October 08, 2018 15:09 PM

In Magento2 what is the place to override the quote?

Updated December 05, 2017 09:09 AM

Magento2 : Can't Override Model File

Updated March 28, 2018 10:09 AM