Skip to content

How i override or create plugin product applier?

I am trying to create plugin product applier but not succeed.
I am trying to send email when content goes live, so i schedule product and goes live then send a push email
when i send 10 product update then compare the timestamp and send the right product update email.

This is my ScheduleContentNotifieretcfrontenddi.xml file code

<?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="MagentoCatalogStagingModelProductApplier">
        <plugin name="around_productapplier" type="CustommoduleScheduleContentNotifierPluginPluginProductApplier" disabled="false" sortOrder="1" />
    </type>
</config>

This is my plugin file CustommoduleScheduleContentNotifierPluginPluginProductApplier

<?php
declare(strict_types=1);
 
namespace CustommoduleScheduleContentNotifierPlugin;

use MagentoCatalogStagingModelProductApplier;
use CustommoduleScheduleContentNotifierHelperEmail;
use MagentoCatalogModelIndexerProductCategory;
use MagentoCatalogModelResourceModelProduct;
use MagentoCatalogModelResourceModelProductCollection;
use MagentoCatalogStagingHelperReindexPool;
use MagentoFrameworkIndexerCacheContext;
use MagentoFrameworkIndexerIndexerRegistry;
use MagentoStagingModelStagingApplierInterface;
use MagentoStagingModelVersionManager;


class PluginProductApplier
{
    protected $emailHelper;

    /**
     * @var Collection
     */
    protected $productCollectionResource;

    /**
     * @var ReindexPool
     */
    protected $indexerPool;

    /**
     * @var IndexerRegistry
     */
    protected $indexerRegistry;

    /**
     * @var CacheContext
     */
    protected $cacheContext;

    /**
     * @param Collection $productResource
     * @param ReindexPool $indexerPool
     * @param IndexerRegistry $indexerRegistry
     * @param CacheContext $cacheContext
     */
    public function __construct(
        Collection $productResource,
        ReindexPool $indexerPool,
        IndexerRegistry $indexerRegistry,
        CacheContext $cacheContext,
        Email $emailHelper
    ) {
        $this->productCollectionResource = $productResource;
        $this->indexerPool = $indexerPool;
        $this->indexerRegistry = $indexerRegistry;
        $this->cacheContext = $cacheContext;
        $this->emailHelper = $emailHelper;
    }

    /**
     * {@inheritdoc}
     */

    public function aroundExecute(ProductApplier $subject)
    {
        $this->emailHelper->sendEmail();
    }
}

This is my helper class CustommoduleScheduleContentNotifierHelperEmail.php

<?php
namespace CustommoduleScheduleContentNotifierHelper;

use MagentoFrameworkAppHelperContext;
use MagentoFrameworkTranslateInlineStateInterface;
use MagentoFrameworkEscaper;
use MagentoFrameworkMailTemplateTransportBuilder;

class Email extends MagentoFrameworkAppHelperAbstractHelper
{
    protected $inlineTranslation;
    protected $escaper;
    protected $transportBuilder;
    protected $logger;

    public function __construct(
        Context $context,
        StateInterface $inlineTranslation,
        Escaper $escaper,
        TransportBuilder $transportBuilder
    ) {
        parent::__construct($context);
        $this->inlineTranslation = $inlineTranslation;
        $this->escaper = $escaper;
        $this->transportBuilder = $transportBuilder;
        $this->logger = $context->getLogger();
    }

    public function sendEmail()
    {
        try {
            $this->inlineTranslation->suspend();
            $sender = [
                'name' => $this->escaper->escapeHtml('example'),
                'email' => $this->escaper->escapeHtml('[email protected]'),
            ];
            $transport = $this->transportBuilder
                ->setTemplateIdentifier('content_update_template')
                ->setTemplateOptions(
                    [
                        'area' => MagentoFrameworkAppArea::AREA_FRONTEND,
                        'store' => MagentoStoreModelStore::DEFAULT_STORE_ID,
                    ]
                )->setTemplateVars([])
                ->setFrom($sender)
                ->addTo('[email protected]')
                ->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();
        } catch (Exception $e) {
            $this->logger->debug($e->getMessage());
        }
    }
}