Overriding private method of Model file in Magento 2
-
30-01-2021 - |
Question
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.
Solution
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>
========================
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
OTHER TIPS
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;
}
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;
}