0

What i'm trying to do is to rewrite a core function of ( MagentoMagento\ConfigurableProduct\Block\Product\View\Type\Configurable )

The function is called getJsonConfig()

 public function getJsonConfig()
{
    $store = $this->getCurrentStore();
    $currentProduct = $this->getProduct();
    // original Code
    //  $options = $this->helper->getOptions($currentProduct, $this->getAllowProducts());
   
    // New code 
    $options = $this->helper->getOptions($currentProduct, $this->getProduct()->getTypeInstance()->getUsedProducts($this->getProduct(), null) ); 
    $attributesData = $this->configurableAttributeData->getAttributesData($currentProduct, $options);

    $config = [
        'attributes' => $attributesData['attributes'],
        'template' => str_replace('%s', '<%- data.price %>', $store->getCurrentCurrency()->getOutputFormat()),
        'currencyFormat' => $store->getCurrentCurrency()->getOutputFormat(),
        'optionPrices' => $this->getOptionPrices(),
        'priceFormat' => $this->localeFormat->getPriceFormat(),
        'prices' => $this->variationPrices->getFormattedPrices($this->getProduct()->getPriceInfo()),
        'productId' => $currentProduct->getId(),
        'chooseText' => __('Choose an Option...'),
        'images' => $this->getOptionImages(),
        'index' => isset($options['index']) ? $options['index'] : [],
    ];


    if ($currentProduct->hasPreconfiguredValues() && !empty($attributesData['defaultValues'])) {
        $config['defaultValues'] = $attributesData['defaultValues'];
    }

    $config = array_merge($config, $this->_getAdditionalConfig());

    return $this->jsonEncoder->encode($config);
}

So i did some research how i could do this and what i found was that using a preference is the best way to do this because i have to rewrite a pice of the core code.

I have create a module with the di.xlm in the etc/frontend folder and also extend the Block with the original class but that did not work

I my module.xml i also added a sequence but that also did not work.

I have done setup:upgrade/di:compile /cache:clean /removed generated also var

bit i still does not work. I was first looking in to using a plugin or events & observers but those can't rewrite the core code. So now i don't know what to try else i will post my code below.

Registraion.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Maerschalk_ConfigurableProductAttributes',
    __DIR__
);

Module.xml

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Maerschalk_ConfigurableProductAttributes" setup_version="1.0.0">
    <sequence>
    <module name="Magento_ConfigurableProduct" />
    </sequence>
    </module>

</config>

di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<preference for="Magento\ConfigurableProduct\Block\Product\View\Type\Configurable" type="Maerschalk\ConfigurableProductAttributes\Block\Product\View\Type\Configurable" />

Configurable

    <?php 

namespace Maerschalk\ConfigurableProductAttributes\Block\Product\View\Type;

class Configurable extends \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable
{
    /**
     * Composes configuration for js
     *
     * @return string
     */
    public function getJsonConfig()
    {
        $store = $this->getCurrentStore();
        $currentProduct = $this->getProduct();
  
        //  $options = $this->helper->getOptions($currentProduct, $this->getAllowProducts());
        $options = $this->helper->getOptions($currentProduct, $this->getProduct()->getTypeInstance()->getUsedProducts($this->getProduct(), null) ); 
        $attributesData = $this->configurableAttributeData->getAttributesData($currentProduct, $options);

        $config = [
            'attributes' => $attributesData['attributes'],
            'template' => str_replace('%s', '<%- data.price %>', $store->getCurrentCurrency()->getOutputFormat()),
            'currencyFormat' => $store->getCurrentCurrency()->getOutputFormat(),
            'optionPrices' => $this->getOptionPrices(),
            'priceFormat' => $this->localeFormat->getPriceFormat(),
            'prices' => $this->variationPrices->getFormattedPrices($this->getProduct()->getPriceInfo()),
            'productId' => $currentProduct->getId(),
            'chooseText' => __('Choose an Option...'),
            'images' => $this->getOptionImages(),
            'index' => isset($options['index']) ? $options['index'] : [],
        ];


        if ($currentProduct->hasPreconfiguredValues() && !empty($attributesData['defaultValues'])) {
            $config['defaultValues'] = $attributesData['defaultValues'];
        }

        $config = array_merge($config, $this->_getAdditionalConfig());

        return $this->jsonEncoder->encode($config);
    }
    
}

1 Answer 1

0

Try to put di.xml into etc and not etc/frontend.

Anyway, depending on what you are trying to do, almost everything can be achieved by a plugin. This is a public function, so with after, before or even around plugin you can hook into it.

And by the way, your di.xml does not have a closing tag, but I guess this is a copy&paste error?!

1
  • Yes it was a copy and past error. but i have move the di.xml file but that did not work. How can i do this with a plugin since there is no variable being passed to the function Jun 10, 2021 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.