Tax not calculated when Zip code is 5+4

Posted: May 10, 2017 at 9:38 AM Quote #187593
Hi,

I have noticed that when a customer enters a Zip+4 in their address, during checkout, the system will not calculate tax.  This works perfectly if they only enter the first 5 digits of the Zip code (my customers are in USA).

This makes sense as my tax setup is using Tax by County & State & Zip and I have tax rates for each Zip code I collect for but the zip codes only have the 5 digits.

How can I force the Zip Code field to only allow 5 Digits?

I looked at Nop.Web.Validators.Customer.CustomerInfoValidator and added a rule for the Zip code as follows:

if (customerSettings.ZipPostalCodeRequired && customerSettings.ZipPostalCodeEnabled)
{
RuleFor(x => x.ZipPostalCode).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.ZipPostalCode.Required"));
RuleFor(x => x.ZipPostalCode).length(5).WithMessage(localizationService.GetResource("Account.Fields.ZipPostalCode.Length"));
}

However, the rule did not fire when I tested.

Any ideas on how to resolve my problem?
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: May 10, 2017 at 5:30 PM Quote #187613
chris.navarro7 wrote:
Hi,

I have noticed that when a customer enters a Zip+4 in their address, during checkout, the system will not calculate tax.  This works perfectly if they only enter the first 5 digits of the Zip code (my customers are in USA).

This makes sense as my tax setup is using Tax by County & State & Zip and I have tax rates for each Zip code I collect for but the zip codes only have the 5 digits.

How can I force the Zip Code field to only allow 5 Digits?

I looked at Nop.Web.Validators.Customer.CustomerInfoValidator and added a rule for the Zip code as follows:

if (customerSettings.ZipPostalCodeRequired && customerSettings.ZipPostalCodeEnabled)
{
RuleFor(x => x.ZipPostalCode).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.ZipPostalCode.Required"));
RuleFor(x => x.ZipPostalCode).length(5).WithMessage(localizationService.GetResource("Account.Fields.ZipPostalCode.Length"));
}

However, the rule did not fire when I tested.

Any ideas on how to resolve my problem?


Where exactly are you adding this new rule?
..in the solution (and then re-compiled it), or directly in a view?

Look for this section in the /Views/Shared/_CreateOrUpdateAddress.cshtml

    @if (Model.ZipPostalCodeEnabled)
    {
        <div class="inputs">
            @Html.LabelFor(model => model.ZipPostalCode, new { }, ":")
                @Html.EditorFor(model => model.ZipPostalCode)
            @if (Model.ZipPostalCodeRequired)
            {
                @Html.RequiredHint()
            }
            @Html.ValidationMessageFor(model => model.ZipPostalCode)
        </div>
    }


Try injecting your if/then condition there.
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Sorry...but I will no longer answer private messages from people that I do not already know...unless you need work done and are willing to pay. I believe in this community. For every question I ask, I try to answer at least five others.
Posted: May 11, 2017 at 7:28 AM Quote #187637
I added the rule inside the class at Nop.Web.Validators.Customer.CustomerInfoValidator

Then I recompiled and moved the nop.web.dll to my test server and tested it.  (It takes too long for my machine to run the website within Visual Studio so I find myself copying DLLs to my test server to test things and if it doesn't work, then I debug it).  

I guess I'll have to step through it in debug...

My question is though, is this the right place to add this; Nop.Web.Validators.Customer.CustomerInfoValidator?
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: May 11, 2017 at 4:20 PM Quote #187652
Looking closely, I found another validator that was related at Nop.Web.Validators.Common.AddressValidator, I added my rule and now the form will not submit unless the Zip code length is 5.

That will do for me!
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: May 19, 2017 at 6:27 PM Quote #188013
Just wanted to add this bit to limit the validation to a specific state.
For example, California:


RuleFor(x => x.ZipPostalCode).Length(5).When(x=>x.ZipPostalCode.StartsWith("9")).WithMessage("Please use a valid 5 Digit California Zip Code")
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Premium support services
  • Dedicated premium support services provided by core developers are intended for persons who run mission critical websites, work on projects with tight deadlines, or want to get dedicated support.
Professional services
  • Want to open a new store? Want to take your store to the next level? Need a custom extension? We can customize nopCommerce to fit your store perfectly. Request a quote to get started.
E-COMMERCE CONFERENCE 2017
Learn more