Magento: Prevent Entry of PO Boxes for Shipping Address

Background

Pretty simple, really. We use UPS for shipping, and UPS doesn’t deliver to P.O. Boxes. Period.

Solutions for Magento, courtesy Fontis

I found this solution at fontis for preventing P.O. Boxes in shipping addresses. They offer a solution that works perfectly well. So review that first, then come back here. You’ll see that they change up Customer/Model/Address/Abstract.php and Checkout/Model/Type/Onepage.php. They’ve got it covered over there, no sense repeating here. However, I will offer one change to their solution.

One Small Change Suggested

I’m a stickler for re-using code, and a double-stickler for never copying code, which the fontis solution calls for. So I changed the validate_pobox() function they suggested:

 
public function validate_pobox()
{
    /*
      * First, call the original 'validate()' function. No need to repeat
      * all that code, just call it! If the return is boolean 'true', all is
      * well so far. Otherwise, I get an array of $errors.
      */
    if ( ($errors = $this->validate()) == true ) {
        /*
          * If validate was OK, I have to allocate an array for $errors;
          * if validate had failed, $errors would already be an array.
          */
        $errors = array();
    }
    $helper = Mage::helper('customer');
 
    /*
      * Here's the regular expression offered up by fontis. Thank
      * goodness for that regex's make my head hurt.
      */
    if( preg_match("/p\.* *o\.* *box/i", $this->getStreet(1))
        || preg_match("/p\.* *o\.* *box/i", $this->getStreet(2)) )
    {
        $errors[] = $helper->__('We cannot ship to PO boxes.');
    }
    if( empty($errors) ) {
        return true;
    }
    return $errors;
}

Facebook comments: