0

How do I get the object that my plugin is attached to from within an after plugin? I thought it would be the $subject parameter, but that doesn't seem to be it.

My plugin is defined like this

di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\ConfigurableProduct\Block\Product\View\Type\Configurable">
        <plugin name="MyCompany_Configurable" type="MyCompany\ConfigurableProduct\Plugin\Configurable" sortOrder="10" disabled="false"/>
    </type>
</config>

Configurable.php

public function afterGetJsonConfig($subject, $result)
{
    // ...
}

I assumed that $subject should be an instance of Magento\ConfigurableProduct\Block\Product\View\Type\Configurable, but when the plugin runs, $subject is actually Magento\Swatches\Block\Product\Renderer\Configurable\Interceptor.

The reason that this matters is because I want to call getAttributesForCollection on the object that the plugin is attached to. However, if I call $subject.getAttributesForCollection(...) it returns null. When getAttributesForCollection(...) is called in the base class (Magento\ConfigurableProduct\Block\Product\View\Type\Configurable), it returns an array.

1 Answer 1

1

The method getAttributeForCollection is only available in the class Magento\ConfigurableProduct\Model\Product\Type\Configurable

You can check the original getJsonConfig Method on how the attribute data in the JSON is implemented.

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.