To add validation for custom salesrule, you should add an after plugin to Magento\SalesRule\Model\Utility::canProcessRule
method.
I'll tell you how to validate the apply_on_weekday
custom field, then you can extend the plugin class to add validation for applicable_time_from
and applicable_time_to
.
Assume you named the vendor name Vendor
, and the module name Module
(You can change the vendor name and module name). Take the following steps:
Step 1: Create the registration.php file:
File path: app/code/Vendor/Module/registration.php
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_Module', __DIR__);
Step 2: Create the module.xml file:
File path: app/code/Vendor/Module/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module">
<sequence>
<module name="Magento_SalesRule"/>
</sequence>
</module>
</config>
Step 3: Create di.xml file:
File path: app/code/Vendor/Module/etc/di.xml
<?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="Magento\SalesRule\Model\Utility">
<plugin name="ValidateWeekdayCondition" type="Vendor\Module\Plugin\ValidateWeekdayCondition"/>
</type>
</config>
Step 4: Create ValidateWeekdayCondition.php file.
File path: app/code/Vendor/Module/Plugin/ValidateWeekdayCondition.php
<?php
declare(strict_types=1);
namespace Vendor\Module\Plugin;
use Magento\SalesRule\Model\Utility;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
class ValidateWeekdayCondition
{
/**
* @var TimezoneInterface
*/
protected $timezoneInterface;
/**
* @param TimezoneInterface $timezoneInterface
*/
public function __construct(
TimezoneInterface $timezoneInterface
) {
$this->timezoneInterface = $timezoneInterface;
}
/**
* after the canProcessRule, Check if salesRule can be applied for the day of the week
*
* @param \Magento\SalesRule\Model\Utility $subject
* @param boolean $result
* @param \Magento\SalesRule\Model\Rule $rule
* @param \Magento\Quote\Model\Quote\Address $address
* @return boolean
*/
public function afterCanProcessRule(Utility $subject, bool $result, $rule, $address)
{
if ($result) {
if ($applyOnWeekday = $rule->getApplyOnWeekday()) {
// $currentWeekday = (new \DateTime())->format('D');
$currentWeekday = $this->timezoneInterface->date()->format('D'); // get current store Weekday
$applyOnWeekday = explode(',', $applyOnWeekday);
if (!in_array($currentWeekday, $applyOnWeekday)) {
$result = false;
}
// Add new code here to validate applicable_time_from and applicable_time_to
} else {
// No weekdays applied
$result = false;
}
}
return $result;
}
}
Step 5: Finally, run the setup:upgrade
command to make your new module active, and then compile code, deploy static content:
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento setup:static-content:deploy -f
Done.