0

Here I Create custom rule condition for custom module in magento 2 now i need to check that all rule at order place time I create event "sales_order_place_after"

in that the rule condition not validate properly

so how can I validate the custom rule condition

here is code for generate custom rule condition in cutom module

LINK:: Magento 2 Can't get the sales rules conditions in my ui form at edit time Magento 2?

<?php

namespace Vednor\Module\Observer;

class OrderPlace implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        $order->getId();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $model=$objectManager->create('Vednor\Module\Model\Rule');
        $quoteId = $order->getQuoteId();
        
        $quote = $objectManager->create('Magento\Quote\Model\Quote')->load($quoteId);

        $datacollection=$model->getCollection()->addFieldToFilter('entity',['eq' => 1])->addFieldToFilter('status',['eq' => 1]);
        $ruleData = $datacollection->getData();
        echo "<pre>";
        echo json_encode($order->getData()); 
        print_r($ruleData);
        foreach($ruleData as $data) {

            $unserialized_conditions = $data;
            var_dump($unserialized_conditions);
            if(isset($unserialized_conditions->conditions_serialized[0]->conditions[0]->value)){
            $explode_array = explode(",", $unserialized_conditions->conditions_serialized[0]->conditions[0]->value); 
            $code="";
            for($j=0;$j<count($explode_array);$j++)
                {

                    if(trim($explode_array[$j])==$productsku)
                    {

                        echo $code.= $rule->getCode();
                        echo $rulesApplied .= "<strong>".$code."</strong>"."</br>";
                    }

                }
            }
            // print_r($data['conditions_serialized']);
            // echo json_decode($data['conditions_serialized']);
            // var_dump($model->getConditions()->validate($quote));
             // var_dump($model->getConditions()->validate());

        }
        // echo json_encode($model->getConditions()->validate());
        // print_r(get_class($model));
        // $model->getConditions()->validate()
        // var_dump($model->getConditions()->validate($model->getConditions()));
        // echo json_encode($ruleData[0]['conditions_serialized']);
        // $datacollection->getConnection()->validate();
        exit;
        $statuscode = $observer->getEvent()->getOrder()->getStatus();
        $statuslabel = $observer->getEvent()->getOrder()->getStatusLabel();
    }
}

1 Answer 1

1

To validate the custom rule condition at order place time, you can use the Magento\SalesRule\Model\RuleFactory class to load the rule and validate it against the quote or order.

2
  • ok but here I store that cart price rule in custom module that I mention above in my question and need to validate order with that custom rule conditions
    – Jay Parmar
    Commented Mar 29, 2023 at 6:12
  • To add your custom rules, you can simply create a collection of your custom rules using the model and filter them based on your requirements. Then, you can loop through these rules and check if they are valid for the current quote using the validate method of the rule's condition object.
    – Harun
    Commented Mar 29, 2023 at 6:18

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.