Developer forum

Forum » Development » Extending ValidateOrder(settings As ModuleSettings, order As Order)

Extending ValidateOrder(settings As ModuleSettings, order As Order)

Anders Ebdrup
Anders Ebdrup
Reply

Hi Dynamicweb,

 

We need to make some custom validation on the orders. Will it be possible to hook into the validation rutine in: ValidateOrder(settings As ModuleSettings, order As Order), so we can add our custom validations and errors to the list?

 

Best regards,

Anders


Replies

 
Nicolai Pedersen
Reply

Hi Anders

No possibility currently to hook into the validateorder with own rules.

The closest is the Ecommerce.Notifications.Ecommerce.Cart.BeforeRenderingNewStep which fires just after validation has been done. In that one you can set RedirectToStepIndex to True and overwrite what step is the next (stay at the same step because validation failed) and somehow mingle in the validation errors.

I just added a new property to the arguments on the BeforeRenderingNewStepArgs with a collection of validation errors - then you can see what validation errors happened and add your own. Out with next 9.6 hotfix of ecommerce package. TFS#63586.

BR Nicolai

 
Nicolai Pedersen
Reply

oh - this might only work if there are other validation errors first...

 
Anders Ebdrup
Anders Ebdrup
Reply

Then what about making a new notification into ValidateOrder? :-)

 
Martin Grønbekk Moen
Martin Grønbekk Moen
Reply

I tried this, and I cant get it to work, even if there are other validation errors.
How do you add new errors to an IEnumerable by the way?

I tried this:

    [Subscribe(Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.BeforeRenderingNewStep)]
    public class BeforeRenderingNewStep : NotificationSubscriber
    {
        public override void OnNotify(string notification, NotificationArgs _args)
        {
            if (_args == null || !ExecutingContext.IsFrontEnd())
                return;

                BeforeRenderingNewStepArgs args = (BeforeRenderingNewStepArgs)_args;
                var validationErrors = new List<ValidationError>();

                var validationError = new ValidationError();
                validationError.ValidationField = ValidationField.GetFieldBySystemName("EcomOrderDeliveryCountry");
                validationError.ErrorMessage = "Error test";
                validationErrors.Add(validationError);

                args.ValidationErrors = args.ValidationErrors.Concat(validationErrors);

                args.StepIndex = 0;
                args.RedirectToStepIndex = true;
        }
    }
 
Anders Ebdrup
Anders Ebdrup
Reply

Hi Nicolai,

Have you considered to add an extra notifier to achive this? :-)

 

Best regards, Anders

 
Anders Ebdrup
Anders Ebdrup
Reply

Is this going to be a part of 9.7? :-)

 
Anders Ebdrup
Anders Ebdrup
Reply

Has this been prioritized into Release 9.7?

 
Nicolai Pedersen
Reply

Nope. Sorry!

 
Anders Ebdrup
Anders Ebdrup
Reply

Dear Nicolai,

 

Has this extension point been added yet?

 
Anders Ebdrup
Anders Ebdrup
Reply

Dear Nicolai,

 

Have you considered implementing this extension point yet?

 

Best regards, Anders

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Hi Anders,

I have registered this as Feature 83108: Add notifications for order validation.

Best regards,
Morten

 
Anders Ebdrup
Anders Ebdrup
Reply

Sounds great! Thank you :-)

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Would it be sufficient if we change the ValidationErrors property on BeforeRenderingNewStepArgs to an IList<ValidationError> instead of IEnumerable<ValidationError>?
You can try it out by casting the property (this is just a hack for testing, not a permanent solution):

[Subscribe(Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.BeforeRenderingNewStep)]
public class BeforeRenderingNewStep : NotificationSubscriber
{
    public override void OnNotify(string notification, NotificationArgs _args)
    {
        BeforeRenderingNewStepArgs args = (BeforeRenderingNewStepArgs)_args;
        var validationErrors = (IList<ValidationError>)args.ValidationErrors;

        var validationError = new ValidationError();
        validationError.ValidationField = ValidationField.GetFieldBySystemName("EcomOrderDeliveryCountry");
        validationError.ErrorMessage = "Error test";
        validationErrors.Add(validationError);

        args.StepIndex = 0;
        args.RedirectToStepIndex = true;
    }
}
 
Anders Ebdrup
Anders Ebdrup
Reply

Dear Morten,

 

Yes, that could do it. But I would prefer to have a separate notification subscriber for this for having a more clean code in my end :-)

 

Best regards, Anders

 
Kristian Kirkholt Dynamicweb Employee
Kristian Kirkholt
Reply
This post has been marked as an answer

Hi Anders

We have added notifications for order validation in Dynamicweb versions 9.8.10 and in 9.9.0

Kind Regards
Dynamicweb Support
Kristian Kirkholt

Votes for this answer: 1
 
Mario Santos Dynamicweb Employee
Mario Santos
Reply

Hi there,

I took a look at source code and I have a question/request smiley 

// List of errors
var errors = new List<ValidationError>();
var beforeValidationArgs = new Ecommerce.Notifications.Ecommerce.Cart.BeforeOrderValidationArgs() { Order = order, ValidationErrors = errors };
NotificationManager.Notify(Ecommerce.Notifications.Ecommerce.Cart.BeforeOrderValidation, beforeValidationArgs);
if (beforeValidationArgs.Cancel)
{
    return errors;
}

......

Would it be possible to return beforeValidationArgs.ValidationErrors  instead of errors ? This way we could easily populate that collection with custom errors and these would be used by the Shopping Cart app.
I checked the AfterOrderValidation and the errors from notification are not used as well.

BR, Mario

 

You must be logged in to post in the forum