0

I'm trying to add a plugin for Magento\Catalog\Model\Product\Type\AbstractType.

I've got a 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\Catalog\Model\Product\Type\AbstractType">
        <plugin name="plugin_abstracttype" type="MyModules\Module1\Plugins\AbstractType"/>
    </type>
</config>

and corresponding MyModules\Module1\Plugins\AbstractType class:

namespace MyModules\Module1\Plugins;

use Magento\Quote\Model\Quote\Item;

class AbstractType
{
    protected $logger;

    public function __construct(\Psr\Log\LoggerInterface $logger)
    {
        die('constructor');
        $this->logger = $logger;
        $this->logger->warning("constructor called");
    }

    public function beforePrepareForCart(\Magento\Catalog\Model\Product\Type\AbstractType $abstractType, \Magento\Framework\DataObject $buyRequest, $product)
    {
        die('prepareForCart');
        $this->logger->warning("called prepareForCart");
        return [$buyRequest, $product];
    }

    public function beforeCheckProductConfiguration(\Magento\Catalog\Model\Product\Type\AbstractType $abstractType, $product, $buyRequest)
    {
        $this->logger->warning("called checkProductConfig");
        return [$product, $buyRequest];
    }
}

For some darn reason, none of the methods (not even the constructor) ever get called.

I have already:

  • bin/magento cache:flush
  • rm -rf generated/*
  • bin/magento setup:upgrade
  • bin/magento setup:di:compile

Unfortunately, none of these helped. Other plugins in this module work just fine.

1
  • Where is your di.xml file located? – Black Nov 10 '20 at 13:34
0

Try blow, it's working for me on Magento 2.3.4

/**   /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\Catalog\Model\Product\Type\AbstractType">
        <plugin name="rw_skuupdater" type="Rootways\AdminTheme\Plugin\Catalog\Model\Product\Type\Update" sortOrder="20" disabled="false" />
    </type>
</config>

Blow is around plugin file.

<?php
namespace Rootways\AdminTheme\Plugin\Catalog\Model\Product\Type;

class Update
{
    /**
     * Item options prefix
     */
    const OPTION_PREFIX = 'option_';
    
    public function aroundGetOptionSku(\Magento\Catalog\Model\Product\Type\AbstractType $subject, callable $proceed, $product, $sku = '')
    {
        $result = $proceed($product, $sku);
        
        $skuDelimiter = '-';
        if (empty($sku)) {
            $sku = $product->getData('sku');
        }
        $optionIds = $product->getCustomOption('option_ids');
        if ($optionIds) {
            foreach (explode(',', $optionIds->getValue()) as $optionId) {
                $option = $product->getOptionById($optionId);
                if ($option) {
                    $confItemOption = $product->getCustomOption(self::OPTION_PREFIX . $optionId);

                    $group = $option->groupFactory($option->getType())
                        ->setOption($option)
                        ->setListener(new \Magento\Framework\DataObject());

                    $optionSku = $group->getOptionSku($confItemOption->getValue(), $skuDelimiter);
                    if ($optionSku) {
                        //$sku .= $skuDelimiter . $optionSku; // Updated by Rootways Inc.
                        $sku = $sku; // Updated by Vish at Rootways Inc.
                    }

                    if ($group->getListener()->getHasError()) {
                        $product->setHasError(true)->setMessage($group->getListener()->getMessage());
                    }
                }
            }
        }
        return $sku;
    }
}

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.