1

I'm trying to get a configuration for my custom module

My Custom module overrides \Magento\Swatches\Block\Product\Renderer\Configurable

With the following Code:

namespace Vendor\swatchExtender\Block;


use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Helper\Product as CatalogProduct;
use Magento\Catalog\Model\Product;
use Magento\ConfigurableProduct\Helper\Data;
use Magento\ConfigurableProduct\Model\ConfigurableAttributeData;
use Magento\Customer\Helper\Session\CurrentCustomer;
use Magento\Framework\Json\EncoderInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\Stdlib\ArrayUtils;
use Magento\Store\Model\ScopeInterface;
use Magento\Swatches\Helper\Data as SwatchData;
use Magento\Swatches\Helper\Media;
use Magento\Swatches\Model\Swatch;
use Magento\Framework\App\ObjectManager;
use Magento\Swatches\Model\SwatchAttributesProvider;

class Configurable extends \Magento\Swatches\Block\Product\Renderer\Configurable
{
    protected function extractNecessarySwatchData(array $swatchDataArray)
    {
        $result['type'] = $swatchDataArray['type'];

        if ($result['type'] == Swatch::SWATCH_TYPE_VISUAL_IMAGE && !empty($swatchDataArray['value'])) {
            $result['value'] = $this->swatchMediaHelper->getSwatchAttributeImage(
                Swatch::SWATCH_IMAGE_NAME,
                $swatchDataArray['value']
            );
            $result['thumb'] = $this->swatchMediaHelper->getSwatchAttributeImage(
                Swatch::SWATCH_THUMBNAIL_NAME,
                $swatchDataArray['value']
            );
        } else {
            $result['value'] = $swatchDataArray['value'];
        }
        $result['custom_parameter'] = 'value';
    }

}

But now i want to read the configuation parameters from the admin-panel.

I have created a Helper data Class:

namespace Vendor\swatchExtender\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function getConfig($config_path)
    {
        return $this->scopeConfig->getValue(
        $config_path,
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
}

But i have no idea how to access the Helper/data, everything i tried will brake the Configurable class, because of the parent __Construct,

How can i Access my Helper/Data class ?

  • you want use helper function inside block file? – Abhishek Panchal Jan 10 '18 at 17:24
  • @AbhishekPanchal I have extend / replaced the original function, to add some extra output, but now i want to get some parameters from the configuration in the admin – Trickster Jan 10 '18 at 17:41
0

Your constructor should like this:

public function __construct(
    Context $context,
    ArrayUtils $arrayUtils,
    EncoderInterface $jsonEncoder,
    Data $helper,
    CatalogProduct $catalogProduct,
    CurrentCustomer $currentCustomer,
    PriceCurrencyInterface $priceCurrency,
    ConfigurableAttributeData $configurableAttributeData,
    SwatchData $swatchHelper,
    Media $swatchMediaHelper,
    array $data = [],
    SwatchAttributesProvider $swatchAttributesProvider = null,
    \Vendor\Module\Helper\Data $helper
) {
    $this->helper = $helper;
    parent::__construct(
        $context,
        $arrayUtils,
        $jsonEncoder,
        $helper,
        $catalogProduct,
        $currentCustomer,
        $priceCurrency,
        $configurableAttributeData,
        $swatchHelper,
        $swatchMediaHelper
        $data
        $swatchAttributesProvider
    );
}

Now you can use $this->helper to call helper function

class Configurable extends \Magento\Swatches\Block\Product\Renderer\Configurable
{
    protected function extractNecessarySwatchData(array $swatchDataArray)
    {
        /**
         * Yout Code
         */
        $this->helper->getConfig('my/config/path');
    }

}

Remove var/generation folder for Magento 2.2 remove /generated folder

  • Thank you, The Biggest problem for me with all the Trying was Removing the /generated folder – Trickster Jan 10 '18 at 18:00

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.