Scheduling Product Availability in Magento: A Step-by-Step Guide
As an e-commerce business owner or manager, you may need to schedule the launch of new products or special promotions at specific dates and times. Magento provides built-in functionality to manage product visibility, but scheduling automatic changes requires a bit more development work. In this post, we’ll walk through creating a cron job in Magento 2 to automatically enable/disable products based on a specified date attribute.
Step 1: Create the Publish Date Attribute
First, you need to create an additional date attribute called “Publish Date” for your products. Go to Stores > Attributes > Product and click “Add New Attribute”. Configure it as follows:
- Attribute Code:
publish_date - Data Type:
Date - Input Type:
Date
After saving this new attribute, go to the product editing page and add a date value for each product you want to schedule.
Step 2: Create the Cron Job
Next, we’ll create a custom cron job that will check for products needing visibility changes based on their “Publish Date” attribute. Here’s how:
Create a new model In your custom module directory (app/code/Vendor/Module/etc/db_schema.xml), define an entity that maps to your product collection:
<entity type="Magento\Catalog\Model\Product" entity_model="Vendor\Module\Model\ResourceModel\Product">
<attribute name="publish_date" backend_type="datetime"/>
</entity>
Create the Cron Job
In app/code/Vendor/Module/etc/crontab.xml, add the following cron configuration:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<resource>
<connection name="default Setup Connection" usage=".setup"/>
<connection name="default Connection" usage="persistent"/>
</resource>
<schedule id="product_visibility_cron" start_time="02:00">
<job instance="Vendor\Module\Cron\ProductVisibility" method="execute"/>
</schedule>
</config>
Create the Cron Job Class
In app/code/Vendor/Module/Cron, create a file called ProductVisibility.php:
namespace Vendor\Module\Cron;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Psr\Log\LoggerInterface;
class ProductVisibility
{
protected $logger;
protected $collectionFactory;
public function __construct(
LoggerInterface $logger,
CollectionFactory $collectionFactory
) {
$this->logger = $logger;
$this->collectionFactory = $collectionFactory;
}
public function execute()
{
try {
$todayDate = date('Y-m-d H:i:s');
$products = $this->collectionFactory->create()
->addFieldToFilter('publish_date', ['gteq' => $todayDate])
->addFieldToSelect(['entity_id', 'status']);
foreach ($products as $product) {
if ((int)$product['status'] === Mage_Catalog_Model_Product::STATUS_DISABLED && strtotime($product['publish_date']) <= strtotime($todayDate)) {
$product->setStatus(Mage_Catalog_Model_Product::STATUS_ENABLED)->save();
$this->logger->info('Enabled product ID: ' . $product['entity_id']);
}
}
return true;
} catch (\Exception $e) {
$this->logger->error($e);
return false;
}
}
}
Step 3: Compile and Enable the Module
Compile your code using the following command in your terminal:
php bin/magento setup:di:compile
Enable your module by running:
php bin/magento module:enable Vendor_Module
php bin/magento cache:flush
php bin/magento indexer:reindex
That’s it! The cron job will run every day at 2 AM (or the start time you specified in crontab.xml), checking for products with a “Publish Date” equal to or greater than today’s date. It will enable any disabled products that match this criteria.
Remember, proper module development and deployment practices should be followed when applying these changes to your production environment. For more detailed guidance on Magento 2 development best practices, visit our official blog at https://laravelcompany.com/blog/magento-2-development-best-practices.
With this setup, you can now schedule product launches and promotions with ease in Magento 2. Happy selling!