Magento2 Around Plugin Usage – How to Implement and Configure

around-pluginconfigurable-productmagento2plugin

I have to override the file Magento\ConfigurableProduct\Helper\Data.php into my custom module

I have few changes in the function getOptions

 public function getOptions($currentProduct, $allowedProducts)
{
    $options = [];
    $allowAttributes = $this->getAllowAttributes($currentProduct);

    foreach ($allowedProducts as $product) {
        $productId = $product->getId();
        foreach ($allowAttributes as $attribute) {
            $productAttribute = $attribute->getProductAttribute();
            $productAttributeId = $productAttribute->getId();
            $attributeValue = $product->getData($productAttribute->getAttributeCode());
           // if ($product->isSalable()) {
                $options[$productAttributeId][$attributeValue][] = $productId;
           // }
            $options['index'][$productId][$productAttributeId] = $attributeValue;
        }
    }
    return $options;
}

I have to comment the if condition in that function like above.

I am trying it using the around plugin.

Created my plugin in following way.

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\ConfigurableProduct\Helper\Data">
    <plugin name="Vendor_Module_Plugin_Magento_ConfigurableProduct_Helper_Data" type="Vendor\Module\Plugin\Helper\Data" sortOrder="10" disabled="false"/>
</type>
</config>

Vendor/Module/Helper/Data.php

<?php

 namespace Vendor\Module\Plugin\Helper;

 class Data
 {

   public function aroundGetOptions(
    \Magento\ConfigurableProduct\Helper\Data $subject,
    \Closure $proceed
   ) {
    //Your plugin code
    $result = $proceed();
    return $result;
   }
}

Can anyone help me with the plugin file content for the above changes.Thank you..

Best Answer

Your plugin class should look like this:

<?php

namespace Vendor\Module\Plugin\Helper;

use Magento\Catalog\Model\Product;

class Data
{
    /**
     * @param \Magento\ConfigurableProduct\Helper\Data $subject
     * @param callable $proceed
     * @param Product $currentProduct
     * @param array $allowedProducts
     * @return array
     */
    public function aroundGetOptions(
        \Magento\ConfigurableProduct\Helper\Data $subject,
        callable $proceed,
        $currentProduct,
        $allowedProducts
    ) {
        $options = [];
        $allowAttributes = $subject->getAllowAttributes($currentProduct);

        foreach ($allowedProducts as $product) {
            $productId = $product->getId();
            foreach ($allowAttributes as $attribute) {
                $productAttribute = $attribute->getProductAttribute();
                $productAttributeId = $productAttribute->getId();
                $attributeValue = $product->getData($productAttribute->getAttributeCode());
                $options[$productAttributeId][$attributeValue][] = $productId;

                $options['index'][$productId][$productAttributeId] = $attributeValue;
            }
        }
        return $options;
    }
}