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():
PHP Code:
// 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?
Re: USPS shipping module...trying to restrict shipping options programmatically
See if this works better for you:
Code:
if ($this->usps_countries == 'US' && MODULE_SHIPPING_USPS_FIRST_CLASS_FILTER_US == 'True' && preg_match('#First\-Class#', $type) && $cnt_first > 1) continue;
// 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 {
$methods[] = array('id' => $type,
'title' => $title,
'cost' => $cost,
);
}
// eof: skip Priority MailRM if master_categories_id 69 has more than 1
}
}
if (sizeof($methods) == 0) return false;
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....
Re: USPS shipping module...trying to restrict shipping options programmatically
You are most welcome ...
Thanks for the update that this worked for you ... :smile:
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!
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
Re: USPS shipping module...trying to restrict shipping options programmatically
Quote:
Originally Posted by
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)?
Code:
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;
Re: USPS shipping module...trying to restrict shipping options programmatically
Quote:
Originally Posted by
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)?
Code:
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:
Quote:
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?
Re: USPS shipping module...trying to restrict shipping options programmatically
Quote:
Originally Posted by
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!
Re: USPS shipping module...trying to restrict shipping options programmatically
Quote:
Originally Posted by
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:
Code:
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;