Narrowing Down USPS shipping methods

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
14 年 前
I have been working on this for days and still no love. So I am asking for help. First off I am not a C# programmer but know enough to get me in to trouble so if it anything other than easy stuff, I program in vb.
I am trying to narrow down the USPS shipping methods. I have read on here that I can do this by adding code into Solution\Shipping\Nop.Shipping.USPS\USPSComputationMethod.cs. I am using a switch method to do this and I will add my code below. I have gotten everything from 'System.Web.HttpUnhandledException' errors to it working only not showing all of the methods and only prices.
Another thing I tried was in Solution\Shipping\Nop.Shipping.USPS\USPSStrings.cs I commented All, and uncommented what I wanted...eg priority and parcel. But that errored with a box type error.
Please help

here is my code, if I take out my commented out area it wont work

private ShippingOptionCollection ParseResponse(string response, ref string error)
        {
            ShippingOptionCollection shippingOptions = new ShippingOptionCollection();

            using (StringReader sr = new StringReader(response))
            using (XmlTextReader tr = new XmlTextReader(sr))


                do
                // while (tr.Read())
                {
                    // Read the next XML record
                    tr.Read();

                    if ((tr.Name == "Error") && (tr.NodeType == XmlNodeType.Element))
                    {
                        string errorText = "";
                        while (tr.Read())
                        {
                            if ((tr.Name == "Description") && (tr.NodeType == XmlNodeType.Element))
                                errorText += "Error Desc: " + tr.ReadString();
                            if ((tr.Name == "HelpContext") && (tr.NodeType == XmlNodeType.Element))
                                errorText += "USPS Help Context: " + tr.ReadString() + ". ";
                        }
                        error = "USPS Error returned: " + errorText;
                    }

                    // Process the inner postage XML
                    if ((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.Element))
                    {
                        string serviceCode = "";
                        string postalRate = "";
                        string test1 = "";
                        bool i = false;
                        // while (tr.Read())
                        do
                        {
                            i = false;
                            tr.Read();

                            if ((tr.Name == "MailService") && (tr.NodeType == XmlNodeType.Element))
                            {
                                test1 = tr.ReadString();                                
                                //switch (test1) {
                                //    case "Express Mail Hold For Pickup":
                                //        i = true;
                                //        break;
                            //    case "Express Mail Flat Rate Envelope Hold For Pickup":
                            //        i = true;
                            //        break;
                            //    case "Express Mail":
                            //        i = true;
                            //        break;
                            //    case "Express Mail Flat Rate Envelope":
                            //        i = true;
                            //        break;
                            //    case "Priority Mail":
                            //        i = true;
                            //        break;
                            //    case "Priority Mail Flat Rate Envelope":
                            //     i = false;
                            //        break;
                            //    case "Priority Mail Small Flat Rate Box":
                            //     i = false;
                            //        break;
                            //    case "Priority Mail Medium Flat Rate Box":
                            //     i = false;
                            //        break;
                            //    case "Priority Mail Large Flat Rate Box":
                            //     i = false;
                            //        break;
                            //    case "Parcel Post":
                            //    i = false;
                            //        break;
                            //    case "Bound Printed Matter":
                            //        i = true;
                            //        break;
                            //    case "Media Mail":
                            //        i = true;
                            //        break;
                            //    case "Library Mail":
                            //        i = true;
                            //        break;
                            //    default :
                            //        i = false;
                            //            break;
                            //}
                                if (i == false)
                                {
                                    serviceCode = test1;
                                    tr.ReadEndElement();
                                    if ((tr.Name == "MailService") && (tr.NodeType == XmlNodeType.EndElement))
                                        break;
                                }
                            }
                            // if (((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.EndElement)) || ((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.Element)))
                            //   break;

                            if ((tr.Name == "Rate") && (tr.NodeType == XmlNodeType.Element))
                            {
                                if (i == false)
                                {
                                    postalRate = tr.ReadString();
                                    tr.ReadEndElement();
                                    if ((tr.Name == "Rate") && (tr.NodeType == XmlNodeType.EndElement))
                                        break;
                                }
                            }
                            
                        } while (!((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.EndElement)));

                        if (shippingOptions.Find((s) => s.Name == serviceCode) == null)
                        {
                            ShippingOption shippingOption = new ShippingOption();
                            //TODO check whether we need to multiply rate by package quantity
                            shippingOption.Rate = Convert.ToDecimal(postalRate, new CultureInfo("en-US"));
                            shippingOption.Name = serviceCode;
                            shippingOptions.Add(shippingOption);
                        }
                    }
                } while (!tr.EOF);
            return shippingOptions;
        }
14 年 前
Ok I think I have it....at least it is working for now. Can someone please look over this code and tell me if Im missing anything.
thanks again


private ShippingOptionCollection ParseResponse(string response, ref string error)
        {
            ShippingOptionCollection shippingOptions = new ShippingOptionCollection();

            using (StringReader sr = new StringReader(response))
            using (XmlTextReader tr = new XmlTextReader(sr))


                do
                // while (tr.Read())
                {
                    // Read the next XML record
                    tr.Read();

                    if ((tr.Name == "Error") && (tr.NodeType == XmlNodeType.Element))
                    {
                        string errorText = "";
                        while (tr.Read())
                        {
                            if ((tr.Name == "Description") && (tr.NodeType == XmlNodeType.Element))
                                errorText += "Error Desc: " + tr.ReadString();
                            if ((tr.Name == "HelpContext") && (tr.NodeType == XmlNodeType.Element))
                                errorText += "USPS Help Context: " + tr.ReadString() + ". ";
                        }
                        error = "USPS Error returned: " + errorText;
                    }

                    // Process the inner postage XML
                    if ((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.Element))
                    {
                        string serviceCode = "";
                        string postalRate = "";
                        string test1 = "";
                        bool i = true;
                        // while (tr.Read())
                        do
                        {
                            i = true;
                            tr.Read();

                            if ((tr.Name == "MailService") && (tr.NodeType == XmlNodeType.Element))
                            {
                                test1 = tr.ReadString();
                                switch (test1)
                                {
                                    case "Express Mail Hold For Pickup":
                                        i = true;
                                        break;
                                    case "Express Mail Flat Rate Envelope Hold For Pickup":
                                        i = true;
                                        break;
                                    case "Express Mail":
                                        i = true;
                                        break;
                                    case "Express Mail Flat Rate Envelope":
                                        i = true;
                                        break;
                                    case "Priority Mail":
                                        i = false;
                                        break;
                                    case "Priority Mail Flat Rate Envelope":
                                        i = false;
                                        break;
                                    case "Priority Mail Small Flat Rate Box":
                                        i = false;
                                        break;
                                    case "Priority Mail Medium Flat Rate Box":
                                        i = false;
                                        break;
                                    case "Priority Mail Large Flat Rate Box":
                                        i = false;
                                        break;
                                    case "Parcel Post":
                                        i = false;
                                        break;
                                    case "Bound Printed Matter":
                                        i = true;
                                        break;
                                    case "Media Mail":
                                        i = true;
                                        break;
                                    case "Library Mail":
                                        i = true;
                                        break;
                                    //default:
                                    //    i = true;
                                    //    break;
                                }
                                //if (i == false)
                                //{
                                    serviceCode = test1;
                                    tr.ReadEndElement();
                                    if ((tr.Name == "MailService") && (tr.NodeType == XmlNodeType.EndElement))
                                        break;
                                //}
                            }
                            // if (((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.EndElement)) || ((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.Element)))
                            //   break;

                            if ((tr.Name == "Rate") && (tr.NodeType == XmlNodeType.Element))
                            {
                                //if (i == false)
                                //{
                                    
                                    postalRate = tr.ReadString();
                                    tr.ReadEndElement();
                                    if ((tr.Name == "Rate") && (tr.NodeType == XmlNodeType.EndElement))
                                        break;
                                //}
                            }
                            
                        } while (!((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.EndElement)));

                        if (i == false)
                        {
                            if (shippingOptions.Find((s) => s.Name == serviceCode) == null)
                            {
                                ShippingOption shippingOption = new ShippingOption();
                                //TODO check whether we need to multiply rate by package quantity
                                shippingOption.Rate = Convert.ToDecimal(postalRate, new CultureInfo("en-US"));
                                shippingOption.Name = serviceCode;
                                shippingOptions.Add(shippingOption);
                            }
                        }
                    }
                } while (!tr.EOF);
            return shippingOptions;
        }
14 年 前
Ya, i would like to do this, and kinda understand your code(total novice here). I see you have a True/False string attached to the case rate method.  Were do i even start if i wanted to try your code?

Solution\Shipping\Nop.Shipping.USPS\USPSComputationMethod.cs

I see, but were do i even find this page to edit or add the code too? I am not looking hard enough under the CP, or general file directory? But i am really new to XML. PLease advise if anyone can help. Thanks
14 年 前
It's fairly easy to do this.   I got the job done in 10 minutes.   You must however, use the source version of the code to do this.  I'm using V1.4 of the code.

In Shipping\Nop.Shipping.USPS\USPSStrings.cs, just comment the items you do not want.

See lines 37-50 below.  Here I only want to show Media Mail.  (Don't forget to build your Nop.Shipping.USPS project.)

string[] _elements = {  // "Express", 
                                  // "Express SH",
                                  // "Express Commercial",
                                  // "Express SH Commercial",
                                  // "First Class",                       /* 13 oz limit */
                                  // "Priority",
                                  // "Priority Commercial",
                                  // "Parcel",
                                  // "Library",
                                  // "BPM",
                                  "Media",
                                  //"ALL",
                                  // "ONLINE"
                                  };
14 年 前
Hey that worked great thanks.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.