1

I have created a custom module.

di.xml

< type name="Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable"> < plugin name="ptgConfigurableProduct" type="ModuleName\Extendfiles\Plugin\Model\ResourceModel\Product\Type\Configurable" sortOrder="1" /> < /type>

Path: /ModuleName/Extendfiles/Model/ResourceModel/Product/Type/ Configurable.php

namespace ModuleName\Extendfiles\Plugin\Model\ResourceModel\Product\Type;

class Configurable extends \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable {

}

Function

public function afterGetAttributeOptions($superAttribute, $productId){

    $scope  = $this->getScopeResolver()->getScope();
    $select = $this->getConnection()->select()->from(
        ['super_attribute' => $this->getTable('catalog_product_super_attribute')],
        [
            'sku' => 'entity.sku',
            'product_id' => 'product_entity.entity_id',
            'attribute_code' => 'attribute.attribute_code',
            'value_index' => 'entity_value.value',
            'option_title' => $this->getConnection()->getIfNullSql(
                'option_value.value',
                'default_option_value.value'
            ),
            'default_title' => 'default_option_value.value',
        ]
    )->joinInner(
        ['product_entity' => $this->getTable('catalog_product_entity')],
        "product_entity.{$this->getProductEntityLinkField()} = super_attribute.product_id",
        []
    )->joinInner(
        ['product_link' => $this->getTable('catalog_product_super_link')],
        'product_link.parent_id = super_attribute.product_id',
        []
    )->joinInner(
        ['attribute' => $this->getTable('eav_attribute')],
        'attribute.attribute_id = super_attribute.attribute_id',
        []
    )->joinInner(
        ['entity' => $this->getTable('catalog_product_entity')],
        'entity.entity_id = product_link.product_id',
        []
    )->joinInner(
        ['entity_value' => 'catalog_product_entity_int'],
        implode(
            ' AND ',
            [
                'entity_value.attribute_id = super_attribute.attribute_id',
                'entity_value.store_id = 0',
                "entity_value.{$this->getProductEntityLinkField()} = "
                . "entity.entity_id",
            ]
        ),
        []
    )->joinLeft(
        ['option_value' => $this->getTable('eav_attribute_option_value')],
        implode(
            ' AND ',
            [
                'option_value.option_id = entity_value.value',
                'option_value.store_id = ' . $scope->getId(),
            ]
        ),
        []
    )->joinLeft(
        ['default_option_value' => $this->getTable('eav_attribute_option_value')],
        implode(
            ' AND ',
            [
                'default_option_value.option_id = entity_value.value',
                'default_option_value.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID,
            ]
        ),
        []
    )->where(
        'super_attribute.product_id = ?',
        $productId
    )->where(
        'option_value.value != "N/A"',
        $productId
    )->where(
        'attribute.attribute_id = ?',
        $superAttribute->getAttributeId()             $attributeId
    );

  return $this->getConnection()->fetchAll($select);
  }

Getting below error for this code

$superAttribute->getAttributeId()

, Fatal error: Call to undefined method Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Interceptor::getAttributeId()

7
  • Are you working on developer mode? Aug 8, 2018 at 13:27
  • yes, it's already in developer mode, and i have already try with commands Aug 8, 2018 at 13:29
  • 1
    Have you added error_reporting(E_ALL); ini_set('display_errors', 1); this code to index.php ? Aug 8, 2018 at 13:30
  • Still public function not working and also not showing any error Aug 8, 2018 at 13:37
  • Did you got solution? Aug 9, 2018 at 5:07

1 Answer 1

0

As per as your code, you have to try to use Plugin as you have using afterGetAttributeOptions which after plugin method on getAttributeOptions.

But, at di.xml you have tried override Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable

In magento2 Plugin & Preference two different things. See Magento2: what is the basic difference between plugin and preference?

At the single word, PLugin can change only the public functions behavior of a class. Where Preference Can change the behavior of any functions of class.

So, to your case, if want to change out the behavior of getAttributeOptions method then you can use after/around/before the plugin method.

Then that case you have to define your class at plugin by below code at your di.xml.

<config>
...
    <type name="Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable">
      <plugin name="{pluginName}" type="Module\Namespace\Model\ResourceModel\Product\Type\Configurable" sortOrder="1" />
    </type>
..
</config>

If you rewrite the class using Preference the class then That case, you cannot use afterGetAttributeOptions,beforeGetAttributeOptions,aroundGetAttributeOptions as, those method are plugin method.

Update

UNDERSTAND form chat

from the chat, you have to use plugin on Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilder and create create after plugin over getSelect

di.xml

 <type name="Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilder">
          <plugin name="{pluginName}" type="Module\Namespace\Plugin\ResourceModel\Attribute\OptionSelectBuilder" sortOrder="1" />
</type>

*Plugin Class

<?php
namespace Module\Namespace\Plugin\ResourceModel\Attribute;
class OptionSelectBuilder
{
    public function afterGetSelect(
    \Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilder $subject,
     $result
    ){
    {
        return $result->where( 
            'option_value.value != "N/A"', 
                $productId 
        );
    }
}
11
  • now getting this error: Fatal error: Call to undefined method Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Interceptor::getAttributeId() in /Modulename/Extendfiles/Plugin/Model/ResourceModel/Product/Type/Configurable.php on line Aug 9, 2018 at 8:39
  • There is some issue at here: $superAttribute->getAttributeId() Aug 9, 2018 at 8:40
  • what you want to use plugin or preference?
    – Amit Bera
    Aug 9, 2018 at 9:02
  • i have update di.xml file now i have use plugin Aug 9, 2018 at 9:12
  • i have add function which i want to override and getting error near $superAttribute->getAttributeId() this code at bottom Aug 9, 2018 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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