Manual (Fixed or By Country/State/Zip) 9 digit zip code issue

2 months ago
Can't seem to get this right, need to trim zip code to first 5 digits when the length of the zip code is 9 digits.  Using Manual (Fixed or By Country/State/Zip) for tax calculations based on zip, the 9 digits come back as no tax.

This  is on 4.50.4 and above, have done it on 3.9 quite a while back and have reviewed any all posts I could find, but none refer to 4.50 or above.

Any advice is greatly appreciated.

Vic Lepouce
2 months ago
vlepouce wrote:
Can't seem to get this right, need to trim zip code to first 5 digits when the length of the zip code is 9 digits.

Where do the 9 digit zip codes come from - are they enterd by the customer or ?
How have you or how do you plan to do this trim ?
Have you or are you going to edit the plugin ?
2 months ago
You will need to modify the code

\Plugins\Nop.Plugin.Tax.FixedOrByCountryStateZip\FixedOrByCountryStateZipTaxProvider.cs
public async Task<TaxRateResult> GetTaxRateAsync(TaxRateRequest taxRateRequest)
...
var zip = taxRateRequest.Address.ZipPostalCode?.Trim() ?? string.Empty;
//add this code
if (zip.Length > 5)
    zip = zip.Substring(0, 5);

2 months ago
New York wrote:
You will need to modify the code

\Plugins\Nop.Plugin.Tax.FixedOrByCountryStateZip\FixedOrByCountryStateZipTaxProvider.cs
public async Task<TaxRateResult> GetTaxRateAsync(TaxRateRequest taxRateRequest)
...
var zip = taxRateRequest.Address.ZipPostalCode?.Trim() ?? string.Empty;
//add this code
if (zip.Length > 5)
    zip = zip.Substring(0, 5);



Very similar to what I had, thought my logic was flawed.  Stepping through I see it sees the long zip code, strips it down to 5 digits, but no record is found and the reduced zip code exists in the tax rates and with the correct tax category.  If the customer ship to address only uses a 5 digit zip code, everything works as expected.
Took an fresh download, made the change in the plugin and recompiled.  After setting up the tax code table and tax settings, same thing, works with 5 digits and fails to find a tax code after trim to 5 digits.

Any help is greatly appreciated.

Vic Lepouce



2 months ago
Yidna wrote:
Can't seem to get this right, need to trim zip code to first 5 digits when the length of the zip code is 9 digits.
Where do the 9 digit zip codes come from - are they enterd by the customer or ?
How have you or how do you plan to do this trim ?
Have you or are you going to edit the plugin ?


I typed 9, but it is really 10 characters.  It comes from the shipping address for the customer.  Limiting zip codes to 5 digits for the customer will not work, some customers are in areas that use something besides 5 digit zip codes.  What I was working on was when the cart goes to calculate tax, take the shipping zip code (taxes are set for ship to address) and test for length.  If length is 10 characters, trim to first 5.  

Can't seem to find where I should be doing this, when I trim it in fixedOrByCountryStateZipTaxProvider.cs it does not work for the order.  I must not be doing my trim in the right area.

Thanks,
Vic Lepouce
2 months ago
RE: "...made the change in the plugin and recompiled.  "
And deployed (copy .dll into site's plugin folder), and restarted site (or Reload list of plugins)?
2 months ago
New York wrote:
RE: "...made the change in the plugin and recompiled.  "
And deployed (copy .dll into site's plugin folder), and restarted site (or Reload list of plugins)?


I haven't published it yet, trying it in Visual Studio before deploying to site.
2 months ago
Then set a breakpoint and debug step through code.
2 months ago
New York wrote:
Then set a breakpoint and debug step through code.


I was following the code with breakpoints and find that when I get down to foundRecord != null, foundRecord is indeed null.  The zip does get trimmed.  I have tried this with it before filter by zip and right after var zip = taxRateRequest.Address.ZipPostalCode?.Trim() ?? string.Empty; with the same results.

2 months ago
If foundRecord is null at the point, then you'll need to find out which of these prior queries is not finding any records:
            var existingRates = allTaxRates.Where(...

            //filter by store
            var matchedByStore = existingRates.Where(...

            //filter by state/province
            var matchedByStateProvince = matchedByStore.Where(...

            //filter by zip
            var matchedByZip = matchedByStateProvince.Where(t...

Hover/inspect the vars one by one (or use immediate window)