0

I want to override a protected function getOptionPrices() of class {root_folder}\vendor\magento\module-configurable-product\Block\Product\View\Type\Configurable.php. I have written following code.

{root_folder}\app\code\Company\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="Magento\ConfigurableProduct\Block\Product\View\Type\Configurable" type="Company\Module\Block\Product\View\Type\Configurable" />
</config>

{root_folder}\app\code\Company\Module\Block\Product\View\Type\Configurable.php

<?php
/**
 * Catalog super product configurable part block
 *
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Company\Module\Block\Product\View\Type;

use Magento\ConfigurableProduct\Model\ConfigurableAttributeData;
use Magento\Customer\Helper\Session\CurrentCustomer;
use Magento\Framework\Pricing\PriceCurrencyInterface;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Configurable extends \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable
{
    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
        \Magento\Framework\Json\EncoderInterface $jsonEncoder,
        \Magento\ConfigurableProduct\Helper\Data $helper,
        \Magento\Catalog\Helper\Product $catalogProduct,
        CurrentCustomer $currentCustomer,
        PriceCurrencyInterface $priceCurrency,
        ConfigurableAttributeData $configurableAttributeData,
        array $data = []
    ) {
        parent::__construct(
            $context,
            $arrayUtils,
            $jsonEncoder,
            $helper,
            $catalogProduct,
            $currentCustomer,
            $priceCurrency,
            $configurableAttributeData,
            $data
        );
    }

    protected function getOptionPrices()
    {
        $customHelper = \Magento\Framework\App\ObjectManager::getInstance()->get('\Company\Module\Helper\CustomHelper');

        $prices = [];
        foreach ($this->getAllowProducts() as $product) {
            $priceInfo = $product->getPriceInfo();
            $stock = $customHelper->getStockData($product->getId());
            $prices[$product->getId()] =
                [
                    'oldPrice' => [
                        'amount' => $this->_registerJsPrice(
                            $priceInfo->getPrice('regular_price')->getAmount()->getValue()
                        ),
                    ],
                    'basePrice' => [
                        'amount' => $this->_registerJsPrice(
                            $priceInfo->getPrice('final_price')->getAmount()->getBaseAmount()
                        ),
                    ],
                    'finalPrice' => [
                        'amount' => $this->_registerJsPrice(
                            $priceInfo->getPrice('final_price')->getAmount()->getValue()
                        ),
                    ],
                    'min_qty' => $stock['minQty'],
                    'max_qty' => $stock['maxQty']
                ];
        }
        return $prices;
    }
}

Above code is not working. Please help me.

1 Answer 1

0

Try overriding Magento\Swatches\Block\Product\Renderer\Listing\Configurable instead. This should work.

2
  • Do you mean to replace class Configurable extends \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable with class Configurable extends \Magento\Swatches\Block\Product\Renderer\Configurable ? This did not work. Sep 12, 2017 at 10:54
  • did you change class in di.xml as well? Sep 13, 2017 at 8:28

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.