You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过编程方式检查Magento 2产品是否关联计划更新?

How to Programmatically Check for Scheduled Updates on a Magento 2 Product

Hey there! If you need to programmatically check if a specific Magento 2 product has any associated Schedule Updates, I've got you covered. Here are a couple of reliable ways to implement this:

Method 1: Using the Schedule Collection Factory

This is the most "Magento-native" approach, leveraging the built-in collection to query scheduled updates cleanly.

First, inject the collection factory into your class via dependency injection:

use Magento\Catalog\Model\ResourceModel\Product\Action\Schedule\CollectionFactory;

class YourCustomClass
{
    protected $scheduleCollectionFactory;

    public function __construct(CollectionFactory $scheduleCollectionFactory)
    {
        $this->scheduleCollectionFactory = $scheduleCollectionFactory;
    }

    /**
     * Check if a product has any scheduled updates (optional: filter by status)
     *
     * @param int $productId
     * @param int|null $status Use Magento\Catalog\Model\Product\Action\Schedule::STATUS_PENDING or STATUS_EXECUTED
     * @return bool
     */
    public function hasScheduledUpdates(int $productId, ?int $status = null): bool
    {
        $collection = $this->scheduleCollectionFactory->create();
        $collection->addFieldToFilter('product_id', $productId);

        // Optional: Filter by update status (pending vs executed)
        if ($status !== null) {
            $collection->addFieldToFilter('status', $status);
        }

        // Return true if any matching records exist
        return $collection->getSize() > 0;
    }
}

How to use this method:

// Check for ANY scheduled updates (pending or executed)
$productId = 123;
if ($this->hasScheduledUpdates($productId)) {
    // Logic to run if updates exist
}

// Check ONLY for pending (unexecuted) updates
use Magento\Catalog\Model\Product\Action\Schedule;
$hasPendingUpdates = $this->hasScheduledUpdates($productId, Schedule::STATUS_PENDING);

Method 2: Direct Database Query (for lightweight use cases)

If you prefer a more streamlined approach, you can query the catalog_product_action_schedule table directly using Magento's resource connection:

use Magento\Framework\App\ResourceConnection;

class YourCustomClass
{
    protected $resource;

    public function __construct(ResourceConnection $resource)
    {
        $this->resource = $resource;
    }

    /**
     * Check if a product has any scheduled updates
     *
     * @param int $productId
     * @return bool
     */
    public function hasScheduledUpdates(int $productId): bool
    {
        $connection = $this->resource->getConnection();
        $tableName = $this->resource->getTableName('catalog_product_action_schedule');

        $count = $connection->fetchOne(
            $connection->select()
                ->from($tableName, ['COUNT(*)'])
                ->where('product_id = ?', $productId)
        );

        return (int)$count > 0;
    }
}

Quick Notes:

  • Always use dependency injection instead of manually instantiating classes—this keeps your code aligned with Magento best practices and avoids tight coupling.
  • The catalog_product_action_schedule table stores all scheduled product changes, including price, status, stock, and attribute updates.
  • If you need to filter for specific update types (e.g., only price changes), add an extra condition on the action column in your collection or query.

内容的提问来源于stack exchange,提问作者Cristhian Andrade

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:25:07