Skip to content

Issue with around plugins on both parent and child classes in Magento 2 #40199

@MagePsycho

Description

@MagePsycho

I am trying to use plugins on the following Magento 2 core methods:

  • \Magento\Bundle\Model\Product\Type::checkProductBuyState()
  • \Magento\Bundle\Model\Product\Type::getOptionsIds()
  • \Magento\Catalog\Model\Product\Type\AbstractType::checkProductBuyState()

Core classes (simplified)

Magento\Bundle\Model\Product\Type extends Magento\Catalog\Model\Product\Type\AbstractType:

class \Magento\Bundle\Model\Product\Type extends \Magento\Catalog\Model\Product\Type\AbstractType
{
    public function checkProductBuyState($product)
    {
        parent::checkProductBuyState($product);
        $productOptionIds = $this->getOptionsIds($product);
        // ... more logic
        return $this;
    }

    public function getOptionsIds($product)
    {
        return $this->getOptionsCollection($product)->getAllIds();
    }
}

AbstractType itself has its own checkProductBuyState:

abstract class \Magento\Catalog\Model\Product\Type\AbstractType
{
    public function checkProductBuyState($product)
    {
        if (!$product->getSkipCheckRequiredOption() && $product->getHasOptions()) {
            // ... validate required options
            throw new \Magento\Framework\Exception\LocalizedException(
                __('The product has required options. Enter the options and try again.')
            );
        }
        return $this;
    }
}

My plugins

<type name="Magento\Catalog\Model\Product\Type\AbstractType">
    <plugin name="MagePsycho_Catalog_AbstractType::aroundCheckProductBuyState"
            type="MagePsycho\Catalog\Plugin\Model\Product\Type\AbstractTypePlugin"
            sortOrder="10"/>
</type>

<type name="Magento\Bundle\Model\Product\Type">
    <plugin name="MagePsycho_Catalog_Bundle_Type::aroundCheckProductBuyState"
            type="MagePsycho\Catalog\Plugin\Bundle\Model\Product\Type\AroundCheckProductBuyStatePlugin"
            sortOrder="10"/>
    <plugin name="MagePsycho_Catalog_Bundle_Type::aroundGetOptionsIds"
            type="MagePsycho\Catalog\Plugin\Bundle\Model\Product\Type\AroundGetOptionsIdsPlugin"
            sortOrder="10"/>
</type>

Problem

If I add a plugin around Magento\Catalog\Model\Product\Type\AbstractType::checkProductBuyState(), then my plugins for the bundle product type (getOptionsIds, checkProductBuyState) stop working.

If I remove the plugin on AbstractType::checkProductBuyState(), the bundle plugins work fine.

Question

Why is my plugin on Magento\Bundle\Model\Product\Type not being executed when I also have a plugin on Magento\Catalog\Model\Product\Type\AbstractType?

  • Is it because Bundle\Type extends AbstractType and the interceptor chain is broken?
  • Do I need to handle $proceed differently in the AbstractType plugin?
  • Or should I move my logic directly to the bundle type instead of the abstract type?

💡 What am I missing here? How can I make both plugins (AbstractType::checkProductBuyState and Bundle\Type::getOptionsIds) work together?

Metadata

Metadata

Assignees

Labels

Issue: needs updateAdditional information is require, waiting for responseTriage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

Type

No type

Projects

Status

Needs Update

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions