2

I created three custom fields in salesrule Database Table.

  1. apply_on_weekday
  2. applicable_time_from
  3. applicable_time_to

Now I wants to validate that custom fields before the applying the cart price rule to cart. but i haven't any idea about it.

for example :- first check date. if date is valid then need to check weekday, & then if weekday also valid then check time from & to values.

if all custom field validate is true then after, apply sales rule as normal flow as magento core.

here apply_on_weekday field has value as Fri,Sat,Sun. convert string to array then validate day is valid or not using current day of datetime of store.

1 Answer 1

1

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.

14
  • yeah, Thank you brother. Nov 10, 2022 at 9:39
  • Glad to help :).
    – Tu Van
    Nov 10, 2022 at 9:43
  • can you guide me in another dout. how can I create Buy X, get next Y with M% off Rule. means Buy X qty of one product, get next Y qty of another product with specific percentage of amount discount. I was research a lot to achive this but not success. Nov 10, 2022 at 12:55
  • 1
    Brother done I agree. & I will share here link of new question after some hours. Then please provide solution of related to apply discount on another product y based on X product qty step Dec 26, 2022 at 7:50
  • 1
    Hey @HarshPatel, sure, I'll take a look soon, but it will take time.
    – Tu Van
    Dec 27, 2022 at 8:06

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.