0

How i get current product name in sendEmail funtion

if (isset($result) && $result) {
                $this->emailHelper->sendEmail('Product', $productName);
            }

And this is my full code.

<?php
declare(strict_types=1);

namespace custommodule\ScheduleContentNotifier\Plugin;

use Magento\CatalogStaging\Model\ProductApplier;
use custommodule\ScheduleContentNotifier\Helper\Email;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Staging\Api\UpdateRepositoryInterface;

class PluginProductApplier
{
    protected $emailHelper;
    protected $product; 
    /**
     * @var UpdateRepositoryInterface
     */
    protected $updateRepository;

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

    /**
     * @param UpdateRepositoryInterface $updateRepository
     * @param Collection $productResource
     * @param ReindexPool $indexerPool
     * @param IndexerRegistry $indexerRegistry
     */

    public function __construct(
        Collection $productResource,
        UpdateRepositoryInterface $updateRepository,
        Email $emailHelper,
    ) {
        $this->updateRepository = $updateRepository;
        $this->productCollectionResource = $productResource;
        $this->emailHelper = $emailHelper;
    }

    /**
     * {@inheritdoc}
     */
    public function aroundExecute(
        ProductApplier $subject,
        callable $proceed,
        array $entityIds
    ) {
        if ($entityIds) {
            $currentVersionId = $this->updateRepository->getVersionMaxIdByTime(
                strtotime("now")
            );
           $select = $this->productCollectionResource->getSelect()->reset(\Zend_Db_Select::COLUMNS)->reset(\Magento\Framework\DB\Select::WHERE)->columns(['entity_id', ])
                ->joinLeft(['ss' => 'staging_update'], 'e.created_in = ss.id', [])
                ->where('e.entity_id IN (?)', $entityIds)
                ->where('e.created_in =?', $currentVersionId)
                ->where('ss.is_rollback IS NULL');
            $result = $this->productCollectionResource
                ->getConnection()
                ->fetchAll($select);
            if (isset($result) && $result) {
                $this->emailHelper->sendEmail();
            }
        }
    }
}

2 Answers 2

0

Try this

In your code, the $productName variable is not defined or assigned a value. To get the current product name.

 if (isset($result) && $result) {
    foreach ($result as $productData) {
        $productId = $productData['entity_id'];
        $product = $this->product->create()->load($productId);
        $productName = $product->getName();
        $this->emailHelper->sendEmail('Product', $productName);
    }
}

Here's the updated code snippet:

  <?php
declare(strict_types=1);

namespace custommodule\ScheduleContentNotifier\Plugin;

use Magento\CatalogStaging\Model\ProductApplier;
use custommodule\ScheduleContentNotifier\Helper\Email;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Staging\Api\UpdateRepositoryInterface;
use Magento\Catalog\Model\ProductFactory;

class PluginProductApplier
{
    protected $emailHelper;
    protected $product;
    /**
     * @var UpdateRepositoryInterface
     */
    protected $updateRepository;

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

    /**
     * @param UpdateRepositoryInterface $updateRepository
     * @param Collection $productResource
     * @param ReindexPool $indexerPool
     * @param IndexerRegistry $indexerRegistry
     */

    public function __construct(
        Collection $productResource,
        UpdateRepositoryInterface $updateRepository,
        Email $emailHelper,
        ProductFactory $product
    ) {
        $this->updateRepository = $updateRepository;
        $this->productCollectionResource = $productResource;
        $this->emailHelper = $emailHelper;
        $this->product = $product;
    }

    /**
     * {@inheritdoc}
     */
    public function aroundExecute(
        ProductApplier $subject,
        callable $proceed,
        array $entityIds
    ) {
        if ($entityIds) {
            $currentVersionId = $this->updateRepository->getVersionMaxIdByTime(
                strtotime("now")
            );
            $select = $this->productCollectionResource->getSelect()->reset(\Zend_Db_Select::COLUMNS)->reset(\Magento\Framework\DB\Select::WHERE)->columns(['entity_id', ])
                ->joinLeft(['ss' => 'staging_update'], 'e.created_in = ss.id', [])
                ->where('e.entity_id IN (?)', $entityIds)
                ->where('e.created_in =?', $currentVersionId)
                ->where('ss.is_rollback IS NULL');
            $result = $this->productCollectionResource
                ->getConnection()
                ->fetchAll($select);
            if (isset($result) && $result) {
                foreach ($result as $productData) {
                    $productId = $productData['entity_id'];
                    $product = $this->product->create()->load($productId);
                    $productName = $product->getName();
                    $this->emailHelper->sendEmail('Product', $productName);
                }
            }
        }
    }
}

This updated code will load each product using the product ID obtained from the result set and retrieve the product name using $product->getName(). Then it will pass the product name to the sendEmail method.

0

Get current product

Using Object Manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
$name =  $product->getName();
var_dump($name);

Using construct

protected $registry;

public function __construct(
    
    \Magento\Framework\Registry $registry,
    
)
{
    $this->registry = $registry;
   
}


public function getCurrentProduct()
{
    $product = $this->_registry->registry('current_product');
    $name =  $product->getName();
    var_dump($name);
}

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.