cancel
Showing results for 
Search instead for 
Did you mean: 

Validation on the cart page in Magento2

Validation on the cart page in Magento2

Hello,

 

I was working on my custom module in Magento EE 2.2.4  and wondering if you have any thoughts about this question?

 

The goal is to perform a certain validations against to all cart items and it should be done on basket (cart) view page only. Error info will be assigned to a particular invalid cart item and cart object itself if this validation fails, for example:

 

// Perform validation...
if (validation fail) {
    $quoteItem->addErrorInfo(
        'my_moduleName',
        error_code_number,    
        'My test error message'
    );

    $quoteItem->getQuote()->addErrorInfo(
        'custom_error_type',
        'my_moduleName',
        error_code_number,    
        'My test error message'
    );
}


I was able to able to find a few Magento 2 core events which fires during the basket page request and hook (observe) my custom validation there. But the issue was that these core events will be fired multiple times within one page request and as of result my custom validation will be also calculated multiple times accordingly. For better performance I would like to find the event observer which will be fired only once during the basket page request. Can you please advise me if you know any of these event observers? Or any public methods which fires only once during the basket page request and where I can accomplish the above logic, I can hook it into this methods through my custom plugin in this case.

 

Thank you in advance,

Rustam.