0

This question is about javascript carrier validation in the checkout page. Depending on the validation result, some carriers may disappear from the displayed list if validation failed for them.

I found documentation about how to Add custom shipping carrier validations. This doc explains how to create from scratch a brand new validator and its own validation rules and how to register them and how to add them in the checkout layout etc. etc...
The point is : I don't need to create a new validator from scratch. The carrier already exists, its validator too. I just want to add an extra validation rule to the existing ones (and fyi, I want this carrier validation to fail if shipping address telephone is not a mobile phone).

Is there a way to inject my own rule in the existing ones (with a mixin maybe ?) or do I have to go through the full creation as described in the official doc, to end up pushing an additional validator for this carrier ?

3 Answers 3

0

In Your Shipping Model File(eg. Career.php) add below like condition

public function collectRates(RateRequest $request)
{
        /**
         * here add custom condition and return false if not matching in your condition
         */
        if ($telephone = $quote->getShippingAddress()->getTelephone()) {
            if($telephone != 'your_variable'){
                return false;   // it will not execute furthure code
            }           
        }

        // here your shipping main code
}
4
  • Is collectRates() called somehow by javascript code in the checkout page ? Because I'm asking about JS validation, with dynamic refresh of carriers list in this checkout. Also, I don't own this carrier, so I guess I would have to make a plugin instead. Oct 25, 2021 at 6:27
  • better way is I suggested already..for js look below link. you must have to go through it there are no any shortcut. devdocs.magento.com/guides/v2.4/howdoi/checkout/… Oct 25, 2021 at 7:43
  • This is for JS validation. I finally followed this doc, without shortcut. Thank you. Nov 12, 2021 at 10:38
  • great !!....adding answer for helping someone Nov 14, 2021 at 7:48
0

Answering to myself : NO. It appears there are no shortcuts to this method.

0

Please Follow Below Document for JS Validation.

https://devdocs.magento.com/guides/v2.4/howdoi/checkout/checkout_carrier.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.