InsideDarkWeb.com

Override method in LayoutProcessor

I want to make changes(PHP code) to the method processPaymentConfiguration, but I don’t know how I can override the method so my changes have a higher priority. I can alter the LayoutProcessor file but it’s never good to apply changes to a core file.

So my question; is this possible and what do I need to do to achieve this?

Vendor/Module/Block/Checkout/LayoutProcessor.php

<?php
namespace VendorModuleBlockCheckout;

use MagentoFrameworkAppObjectManager;
use MagentoCheckoutBlockCheckoutLayoutProcessorInterface;
use MagentoCustomerModelAttributeMetadataDataProvider;
use MagentoUiComponentFormAttributeMapper;
use MagentoCheckoutBlockCheckoutAttributeMerger;
use MagentoCustomerModelOptions;

class LayoutProcessor implements LayoutProcessorInterface
{
    private $attributeMetadataDataProvider;
    protected $attributeMapper;
    protected $merger;
    private $options;

    public function __construct(
        AttributeMetadataDataProvider $attributeMetadataDataProvider,
        AttributeMapper $attributeMapper,
        AttributeMerger $merger
    ) {
        $this->attributeMetadataDataProvider = $attributeMetadataDataProvider;
        $this->attributeMapper               = $attributeMapper;
        $this->merger                        = $merger;
    }

    private function getOptions()
    {
        //same code as Magento/Checkout/Block/LayoutProcessor::getOptions()
    }


    private function getAddressAttributes()
    {
        //same code as Magento/Checkout/Block/LayoutProcessor::getAddressAttributes()
    }

    private function convertElementsToSelect($elements, $attributesToConvert)
    {
        //same code as Magento/Checkout/Block/LayoutProcessor::convertElementsToSelect()
    }

    public function process($jsLayout)
    {
        //same code as Magento/Checkout/Block/LayoutProcessor::process()
    }

    private function processPaymentConfiguration(array &$configuration, array $elements)
    {
        /*
        code from Magento/Checkout/Block/LayoutProcessor::processPaymentConfiguration()
        with a couple changes.
        It works when I apply the changes in code file (vendor/magento/magento-checkout/...)
        */
    }
}

When I var_dump processPaymentConfigurationfrom the core and my custom code, I get the same results. So it looks like the code executes and returns the right thing but nothing is happening.

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">
    <preference for="MagentoCheckoutBlockCheckoutLayoutProcessor" type="VendorModuleBlockCheckoutLayoutProcessor" />
</config>

Magento Asked by creativename on November 25, 2021

2 Answers

2 Answers

  1. Create your own module if you do not have created it yet.
  2. Implement MagentoCheckoutBlockCheckoutLayoutProcessorInterface::process in your module through YourModule/etc/frontend/di.xml. Check implementation in MagentoCheckoutBlockCheckoutLayoutProcessor::process as a reference

  3. In your new implementation of LayoutProcessorInterface::process method get all payment methods by

    $paymentList = $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['payments-list']['children'] ?? null;

  4. Iterate through this payment list and modify layout configuration as needed.

  5. Return modified $jsLayout from your implementation of LayoutProcessorInterface::process method.

Answered by transversus on November 25, 2021

you have to "extend" the class not "implement" interface..

class LayoutProcessor extends MagentoCheckoutBlockCheckoutLayoutProcessor

Answered by Naveed Asim on November 25, 2021

Add your own answers!

Related Questions

Magento 2 Access-Control-Allow-Origin

2  Asked on September 9, 2020 by borche-glafche

   

Magento 2: what is the catalog_attributes.xml file?

2  Asked on September 7, 2020 by raphael-at-digital-pianism

       

magento 2 – modify db_schema.xml field length

1  Asked on September 4, 2020 by sairaj-gadekar

 

Magento 2.3.2 Upgrade to 2.4

2  Asked on September 4, 2020 by hatim

 

Magento 2: Make sales without stock

1  Asked on August 9, 2020 by hudson-alves-triunfante

     

Magento 2 : print_r or var_dump is not working

4  Asked on July 30, 2020 by zed-blackbeard

   

Create quick order form at product page

0  Asked on July 29, 2020 by faraz-bashir

   

How to Special price based on attribute?

2  Asked on July 26, 2020 by user90560

     

is creating block within template in magento 2 good practice

0  Asked on July 24, 2020 by herve-tribouilloy

       

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.