Zen Cart Logo
Forums / Addon Shipping Modules / USPS shipping module...trying to restrict shipping options programmatically

USPS shipping module...trying to restrict shipping options programmatically

Views: 2,991

Results 1 to 12 of 12
3 May 2013, 5:40 PM
#1
ecclescake avatar

ecclescake

New Zenner

Join Date:
Jul 2011
Location:
Sonoma County, CA, USA
Posts:
20
Plugin Contributions:
0

USPS shipping module...trying to restrict shipping options programmatically

My shop has one category whose products must be shipped via Express Mail. All other products may be shipped either by Priority or Express. If an order contains one or more products from this particular category I need to force Express Mail.

So, both shipping methods are allowed, and I'm modifying the quote() function per Linda's code in this thread to check for products in category 69 in the cart and disable Priority Mail if that's the case. This code worked great in a past version of the USPS module, but now I'm using the update from March 27 and I can't get it to work.

Here's the code, from within quote():

// Disable Priority Mail for cheese. ecasteel
        if (($this->types[$type] == 'Priority MailRM') && $_SESSION['cart']->in_cart_check('master_categories_id','69') > 0) { 
          $skip_this = true; 
        } else { 
          $skip_this = false; 
        }
        if (!$skip_this) {
        // eof ecasteel
          if ((($method == '' && in_array($type, $types)) || $method == $type) && $usps_shipping_weight <= $maxweight && $usps_shipping_weight > $minweight) {
            $methods[] = array('id' => $type,
                               'title' => $title,
                               'cost' => $cost,
                              );
          }
        // bof ecasteel
        }
        // eof ecasteel
      }

I think the problem is how I check for the shipping type. I'm using the string 'Priority MailRM' because that's what I see when I print out the types[] array. I've also tried 'Priority Mail', which is what worked before, and 'Priority Mail®', which is what shows up on the shipping options page during checkout. Do I need to check for a different string? I also tried checking whether the array index was 15, which is where 'Priority MailRM' shows up when I look at the types[] array. Can anyone point me in the right direction?

3 May 2013, 6:30 PM
#2
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: USPS shipping module...trying to restrict shipping options programmatically

See if this works better for you:

          if ($this->usps_countries == 'US' && MODULE_SHIPPING_USPS_FIRST_CLASS_FILTER_US == 'True' && preg_match('#First\-Class#', $type) && $cnt_first > 1) continue;
[B]// bof: skip Priority MailRM if master_categories_id 69 has more than 1
if (($type == 'Priority MailRM') && $_SESSION['cart']->in_cart_check('master_categories_id','69') > 0) {
  $skip_this = true;
//echo 'USPS skip: ' . $type . '<br>';
} else {
  $skip_this = false;
//echo 'USPS NO skip: ' . $type . '<br>';
}
if ($skip_this) {
  // skip it
} else {
[/B][B]          $methods[] = array('id' => $type,
                             'title' => $title,
                             'cost' => $cost,
                            );
[/B][B]}
// eof: skip Priority MailRM if master_categories_id 69 has more than 1
[/B]        }
      }

      if (sizeof($methods) == 0) return false;
3 May 2013, 8:37 PM
#3
ecclescake avatar

ecclescake

New Zenner

Join Date:
Jul 2011
Location:
Sonoma County, CA, USA
Posts:
20
Plugin Contributions:
0

Re: USPS shipping module...trying to restrict shipping options programmatically

Brilliant! Thanks so much Linda! I wish I'd thought of testing $type directly. Couldn't see the forest for the trees....

3 May 2013, 9:02 PM
#4
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: USPS shipping module...trying to restrict shipping options programmatically

You are most welcome ...

Thanks for the update that this worked for you ... :smile:

12 Dec 2024, 1:32 AM
#5
mcqueeneycoins avatar

mcqueeneycoins

Zen Follower

Join Date:
Jan 2008
Posts:
156
Plugin Contributions:
0

Re: USPS shipping module...trying to restrict shipping options programmatically

I know this is an old thread, but would anyone have an updated solution to this problem using the current USPS module? I'm wanting to disable Ground Advantage for all tax classes except one (tax_class_id = 4). I was trying to modify the solution above but looks like the coding has changed in the last 10 years (no surprise there!). Using 1.5.8a and have the most updated USPS module available. Thanks in advance for any guidance!

12 Dec 2024, 2:13 AM
#6
barco57 avatar

barco57

Totally Zenned

Join Date:
Apr 2006
Location:
West Salem, IL
Posts:
2,845
Plugin Contributions:
0

Re: USPS shipping module...trying to restrict shipping options programmatically

Refer to the in-module comments present in /extras/includes/classes/observers/auto.usps_overrides.php

12 Dec 2024, 2:27 AM
#7
mcqueeneycoins avatar

mcqueeneycoins

Zen Follower

Join Date:
Jan 2008
Posts:
156
Plugin Contributions:
0

Re: USPS shipping module...trying to restrict shipping options programmatically

barco57:

Refer to the in-module comments present in /extras/includes/classes/observers/auto.usps_overrides.php

Thank you for pointing me in the right direction...but could still use some help if possible. Do I add the extra code in the override file, or just use that as a template and add to the usps.php file?

Second, can you tell me if I'm on the right track with this coding (wherever it ultimately goes)?

if (stripos($p1, 'USPS Ground Advantage') !== false) {
                    $chk_media = 0;
                    $chk_media += $_SESSION['cart']->in_cart_check('products_tax_class_id', '4') > 0;
                    if ($chk_media == $_SESSION['cart']->count_contents()) {
                        $p2 = true;
                    }
                }
break;
12 Dec 2024, 12:52 PM
#8
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,099
Plugin Contributions:
56

Re: USPS shipping module...trying to restrict shipping options programmatically

mcqueeneycoins:

Thank you for pointing me in the right direction...but could still use some help if possible. Do I add the extra code in the override file, or just use that as a template and add to the usps.php file?

Second, can you tell me if I'm on the right track with this coding (wherever it ultimately goes)?

if (stripos($p1, 'USPS Ground Advantage') !== false) {
$chk_media = 0;
$chk_media += $_SESSION['cart']->in_cart_check('products_tax_class_id', '4') > 0;
if ($chk_media == $_SESSION['cart']->count_contents()) {
$p2 = true;
}
}
break;

@barco57 has put you on the right track, you want to make your changed to the auto.usps_overrides.php (which you'll place in the site's /includes/classes/observers sub-directory).  That way, once working, it'll keep on working through a base USPS module update.

I'm a bit confused from your description as to what you're trying to do:

> I'm wanting to disable Ground Advantage for all tax classes except one (tax_class_id = 4).
Does this mean that Ground Advantage should be disallowed if ***any ***product in the cart uses tax_class_id 4 or if **all **products use that tax-class?
12 Dec 2024, 2:16 PM
#9
mcqueeneycoins avatar

mcqueeneycoins

Zen Follower

Join Date:
Jan 2008
Posts:
156
Plugin Contributions:
0

Re: USPS shipping module...trying to restrict shipping options programmatically

lat9:

I'm a bit confused from your description as to what you're trying to do:

Does this mean that Ground Advantage should be disallowed if ***any ***product in the cart uses tax_class_id 4 or if **all **products use that tax-class?

Thanks, lat9--basically I have 3 or 4 different tax classes, one of which is for supplies. The way I have it set up now is the flat-rate option is available for all products except tax class id 4, which works just fine. It then displays only the USPS module with ground advantage and priority mail options when tax class id 4 is present in the cart.

But, if the cart has no supplies (no products with tax id 4), it displays flat rate, ground advantage and priority mail. I basically need ground advantage to be disabled in this case and only display flat rate and priority.

THanks!

12 Dec 2024, 3:40 PM
#10
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,099
Plugin Contributions:
56

Re: USPS shipping module...trying to restrict shipping options programmatically

mcqueeneycoins:

Thanks, lat9--basically I have 3 or 4 different tax classes, one of which is for supplies. The way I have it set up now is the flat-rate option is available for all products except tax class id 4, which works just fine. It then displays only the USPS module with ground advantage and priority mail options when tax class id 4 is present in the cart.

But, if the cart has no supplies (no products with tax id 4), it displays flat rate, ground advantage and priority mail. I basically need ground advantage to be disabled in this case and only display flat rate and priority.

THanks!
Thanks for the clarification! You can use the following code in that auto-loaded observer:

            case 'NOTIFY_USPS_UPDATE_OR_DISALLOW_TYPE':
                // -----
                // Disallow "Ground Advantage" if there are no products with a tax_class_id of 4
                // in the cart.
                //
                if (stripos($p1, 'USPS Ground Advantage') !== false) {
                    if ($_SESSION['cart']->in_cart_check('products_tax_class_id', '4') == 0) {
                        $p2 = false;
                    }
                }
                break;
12 Dec 2024, 4:28 PM
#11
mcqueeneycoins avatar

mcqueeneycoins

Zen Follower

Join Date:
Jan 2008
Posts:
156
Plugin Contributions:
0

Re: USPS shipping module...trying to restrict shipping options programmatically

lat9:

Thanks for the clarification! You can use the following code in that auto-loaded observer:

        case 'NOTIFY_USPS_UPDATE_OR_DISALLOW_TYPE':
            // -----
            // Disallow "Ground Advantage" if there are no products with a tax_class_id of 4
            // in the cart.
            //
            if (stripos($p1, 'USPS Ground Advantage') !== false) {
                if ($_SESSION['cart']->in_cart_check('products_tax_class_id', '4') == 0) {
                    $p2 = false;
                }
            }
            break;

Worked like a charm--thank you!
13 Dec 2024, 4:24 AM
#12
mcqueeneycoins avatar

mcqueeneycoins

Zen Follower

Join Date:
Jan 2008
Posts:
156
Plugin Contributions:
0

Re: USPS shipping module...trying to restrict shipping options programmatically

@lat9--sorry, quick follow up question. Is there an easy way to change the display name of the shipping service? Right now it shows up as USPS Ground Advantage. Because of how I have my shipping options set up/combined, I'd like for it to just say Ground Advantage. I believe the module is pulling this info direct from USPS and didn't see a language file unless I missed one? Thanks again!