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.

Harsh Patel
  • 219
  • 1
  • 10

1 Answers1

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-&gt;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-&gt;getApplyOnWeekday()) {
            // $currentWeekday = (new \DateTime())-&gt;format('D');
            $currentWeekday = $this-&gt;timezoneInterface-&gt;date()-&gt;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.

Harsh Patel
  • 219
  • 1
  • 10
Tu Van
  • 6,868
  • 2
  • 11
  • 22
  • yeah, Thank you brother. – Harsh Patel Nov 10 '22 at 09:39
  • Glad to help :). – Tu Van Nov 10 '22 at 09: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. – Harsh Patel Nov 10 '22 at 12:55
  • I mean, suppose we need create buy 3 Face wash, get 1 Bodylotion with 10% off SalesRule. it means if customer buy 3 face wash then they will get 10% discount on 1 bodylotion. if customer buy 5 face wash then they will get 10% discount on only 1 bodylotion. if customer buy 6 face wash then they will get 10% discount on 2 bodylotion... I don't think, we can achive this using only condition. – Harsh Patel Nov 11 '22 at 04:48
  • You can take a look at this post to find know how to add a new action to salesrule: https://magento.stackexchange.com/questions/284785/in-magento-2-how-to-add-custom-options-in-action-apply-field-in-cart-price-rule – Tu Van Nov 11 '22 at 04:58
  • Implement Sales rule action requires much time to investigate and develop, so I'm currently not available to help you with the complete guide. You should dig deep into Magento_SalesRule to know how it implements then apply it to your new Sale rule action. Looks like there is no online documentation for that, if you have done that, creating a new question and self-answer that question will help others who have the same issue. Good luck. – Tu Van Nov 11 '22 at 05:05
  • ya, I try it first, then I will tell you status of this. & I already added new action in dropdown for that. – Harsh Patel Nov 11 '22 at 05:09
  • hey brother can you help me in this issue? https://magento.stackexchange.com/q/362504/93504 – Harsh Patel Dec 06 '22 at 08:00
  • Hi @HarshPatel Sorry, I'm very busy with my work right now, so I can't help you at the moment, maybe I can take a look after 1-2 weeks... Hope you can resolve your issue soon. Whenever you resolve an issue by yourself you will earn more experience and knowledge. – Tu Van Dec 06 '22 at 10:45
  • hey brother, I doesn't solve my last issue about discount applies on another Y SKU product based on X product Step qty. can you check it please. – Harsh Patel Dec 26 '22 at 04:50
  • your link doesn't works. it shows 404 page not found – Harsh Patel Dec 26 '22 at 07:35
  • 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 – Harsh Patel Dec 26 '22 at 07:50
  • brother I edited my last question for discount apply on another product Y issue. please check it here https://magento.stackexchange.com/q/362504/93504 & please help me as soon as possible – Harsh Patel Dec 27 '22 at 05:23
  • 1
    Hey @HarshPatel, sure, I'll take a look soon, but it will take time. – Tu Van Dec 27 '22 at 08:06