0
<?php
declare(strict_types=1);

namespace custom\ScheduleContentNotifier\Plugin;

use Magento\CatalogStaging\Model\ProductApplier;
use custom\ScheduleContentNotifier\Helper\Email;
use Magento\Catalog\Model\Indexer\Product\Category;
use Magento\Catalog\Model\ResourceModel\Product;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\CatalogStaging\Helper\ReindexPool;
use Magento\Framework\Indexer\CacheContext;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Staging\Model\StagingApplierInterface;
use Magento\Staging\Model\VersionManager;
use Magento\Staging\Api\UpdateRepositoryInterface;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;



class PluginProductApplier
{
    protected $emailHelper;

    protected $_productCollectionFactory;

    /**
     * @var UpdateRepositoryInterface
     */
    protected $updateRepository;

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

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

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

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

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

    public function __construct(
        Collection $productResource,
        ReindexPool $indexerPool,
        IndexerRegistry $indexerRegistry,
        CacheContext $cacheContext,
        UpdateRepositoryInterface $updateRepository,
        Email $emailHelper,
        CollectionFactory $productCollectionFactory,
        array $data = []
    ) {
        $this->updateRepository = $updateRepository;
        $this->productCollectionResource = $productResource;
        $this->indexerPool = $indexerPool;
        $this->indexerRegistry = $indexerRegistry;
        $this->cacheContext = $cacheContext;
        $this->emailHelper = $emailHelper;
        $this->_productCollectionFactory = $productCollectionFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function aroundExecute(ProductApplier $subject,  callable $proceed, array $entityIds)
    {        
     if($entityIds) {
        $currentVersionId = $this->updateRepository->getVersionMaxIdByTime(strtotime('now'));
        $productcollection = $this->_productCollectionFactory->create();
        $productcollection->addAttributeToFilter('entity_id', ['in' => $entityIds]);
        $productcollection->addAttributeToFilter('created_in', $currentVersionId);
        echo $productcollection->getSelect();
        die();
        if ($currentVersionId === $createdAt) {
                $this->emailHelper->sendEmail();
            }

        }
    }
}

2 Answers 2

2

Ah...you might be on to something here. addAttributeToFilter works only with existing attributes (or the PK).
In this case I think this is a bug (or maybe it is intentional) that created_in is not added by magento to the list of attributes, but only as a column in the main product table catalog_product_entity.
There are other columns in there (like sku for example) for which the values are stored directly in the main table and not in an eav related table.
You can try to get support from Magento for this, or you can try a workaround

Instead of

$productcollection->addAttributeToFilter('created_in', $currentVersionId);

try this

$select = $productcollection->getSelect()
$select->where('created_in = ?', $currentVersionId);
1

Try this code it will be useful to you

Based on the provided code, it seems that you are attempting to create a plugin for the ProductApplier class in Magento. However, there is an issue with the attribute name created_in in the addAttributeToFilter method of the product collection. To resolve this issue, you need to replace the attribute name with a valid attribute.

  *Use this code* 


 <?php
declare(strict_types=1);

namespace custom\ScheduleContentNotifier\Plugin;

use Magento\CatalogStaging\Model\ProductApplier;
use custom\ScheduleContentNotifier\Helper\Email;
use Magento\Catalog\Model\Indexer\Product\Category;
use Magento\Catalog\Model\ResourceModel\Product;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\CatalogStaging\Helper\ReindexPool;
use Magento\Framework\Indexer\CacheContext;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Staging\Model\StagingApplierInterface;
use Magento\Staging\Model\VersionManager;
use Magento\Staging\Api\UpdateRepositoryInterface;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

class PluginProductApplier
{
    protected $emailHelper;
    protected $_productCollectionFactory;

    /**
     * @var UpdateRepositoryInterface
     */
    protected $updateRepository;

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

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

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

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

    /**
     * @param UpdateRepositoryInterface $updateRepository
     * @param Collection $productResource
     * @param ReindexPool $indexerPool
     * @param IndexerRegistry $indexerRegistry
     * @param CacheContext $cacheContext
     */
    public function __construct(
        Collection $productResource,
        ReindexPool $indexerPool,
        IndexerRegistry $indexerRegistry,
        CacheContext $cacheContext,
        UpdateRepositoryInterface $updateRepository,
        Email $emailHelper,
        CollectionFactory $productCollectionFactory,
        array $data = []
    ) {
        $this->updateRepository = $updateRepository;
        $this->productCollectionResource = $productResource;
        $this->indexerPool = $indexerPool;
        $this->indexerRegistry = $indexerRegistry;
        $this->cacheContext = $cacheContext;
        $this->emailHelper = $emailHelper;
        $this->_productCollectionFactory = $productCollectionFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function aroundExecute(ProductApplier $subject, callable $proceed, array $entityIds)
    {
        if ($entityIds) {
            $currentVersionId = $this->updateRepository->getVersionMaxIdByTime(strtotime('now'));
            $productCollection = $this->_productCollectionFactory->create();
            $productCollection->addAttributeToFilter('entity_id', ['in' => $entityIds]);
            // Replace 'created_in' with a valid attribute name for filtering
            $productCollection->addAttributeToFilter('your_attribute_name', $currentVersionId);
            echo $productCollection->getSelect();
            die();
            if ($currentVersionId === $createdAt) {
                $this->emailHelper->sendEmail();
            }
        }
    }
}


NOTE: please replace **'your_attribute_name**' with the correct attribute name you want to use for filtering the product collection.



     Hope you it can useful to you
1
  • I think replacing created_in is not a solution. that is the whole idea... to filter by created_in which is a proper field in the product table added by the content staging module.
    – Marius
    Commented May 30, 2023 at 15:19

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.