/** * * @param Varien_Event_Observer $observer * @return \RocketWeb_UpsAddressTypeValidator_Model_Observer */ public function addResidentialIndicator(Varien_Event_Observer $observer) { $helper = Mage::helper('ups_address_validator'); // if (!$helper->doValidateAddress()) return $this; $order = $observer->getEvent()->getOrder(); $config = Mage::getModel('ups_address_validator/config'); $shippingMethod = $order->getShippingMethod(); if (!$config->enableIndicator() && !$config->enableIndicatorForOthers()) { return $this; } $shippingDescription = $order->getShippingDescription(); $session = Mage::getSingleton("core/session", array("name" => "frontend")); $indicator = $session->getData('ResidentialIndicator'); if ($indicator && !empty($indicator) && $order->getShippingAddress() && $order->getShippingAddress()->getCountryId() == 'US') { $indicatorText = $indicator == RocketWeb_UpsAddressTypeValidator_Model_Usa_Shipping_Carrier_Ups::ADDRESS_TYPE_RESIDENTIAL ? $config->getResidentialIndicator() : $config->getCommercialIndicator(); if (strpos($shippingDescription, $indicatorText) === false) { $shippingDescription = trim($shippingDescription . $indicatorText); } } else { if ($config->enableIndicator() && $config->enableIndicatorForOthers() && $order->getShippingAddress()) { $upsindicator = RocketWeb_UpsAddressTypeValidator_Helper_Data::getAddressTypeFromAddress($order->getShippingAddress()); if (is_numeric($upsindicator)) { $indicatorText = $upsindicator == RocketWeb_UpsAddressTypeValidator_Model_Usa_Shipping_Carrier_Ups::ADDRESS_TYPE_RESIDENTIAL ? $config->getResidentialIndicator() : $config->getCommercialIndicator(); } else { $indicatorText = $upsindicator; } // 0 = unknown // 1 = commercial // 2 = residential if ($upsindicator == 1) { $order->getShippingAddress()->setResidentialIndicator(2); $order->getShippingAddress()->setIsUpsInvalid(0); } elseif ($upsindicator == 2) { $order->getShippingAddress()->setResidentialIndicator(1); $order->getShippingAddress()->setIsUpsInvalid(0); } else { $order->getShippingAddress()->setIsUpsInvalid(1); } if (strpos($shippingDescription, $indicatorText) === false) { $shippingDescription = trim($shippingDescription . " " . $indicatorText); } } } if ($config->enableIndicator() && strpos($shippingMethod, 'ups_') !== false) { $order->setShippingDescription($shippingDescription); } elseif ($config->enableIndicatorForOthers() && strpos($shippingMethod, 'ups_') === false) { $order->setShippingDescription($shippingDescription); } $session->unsetData('ResidentialIndicator'); if ($session->getData('invalid_address') && $order->getShippingAddress()) { $order->getShippingAddress()->setIsUpsInvalid(1); } $session->unsetData('invalid_address'); return $this; }
/** * Returns the shipping mthod options HTML in * checkout onepage, including a custom warning, * if applicable * @return string */ protected function _getShippingMethodsHtml() { // Load the extension's Data.php helper $helper = Mage::helper('ups_address_validator'); // Load current customer's session $session = Mage::getSingleton("core/session", array("name" => "frontend")); // Switch between choice in Admin: // 0 - No validation // 1 - Warn customer // 2 - Do not allow checkout if faddress is invalid switch (Mage::getStoreConfig('rocketweb_addressvalidator/general/validation_type')) { // For Warn customer (1) case RocketWeb_UpsAddressTypeValidator_Helper_Addressvalidation::WARN_CUSTOMER: // Get whatever the shipping methods HTML is $shippingMethodsHtml = parent::_getShippingMethodsHtml(); // If the controller is in the scope (frontend in this case) if ($helper->doValidateAddress()) { // Load customer's shipping address $shipping = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress(); // Get Address type $ups_address_type = RocketWeb_UpsAddressTypeValidator_Helper_Data::getAddressTypeFromAddress($shipping); // Check if the address is valid by calling UPS $is_valid_address = is_numeric($ups_address_type) ? true : false; // If UPS couldn't determine the address type and the country is US if ($shipping->getCountryId() == 'US' && ($session->getData('invalid_address') || !$is_valid_address)) { // Prepend the warning message to shipping methods HTML $shippingMethodsHtml = '<strong>' . Mage::getStoreConfig('rocketweb_addressvalidator/general/customer_warning_message') . '</strong>' . $shippingMethodsHtml; } } return $shippingMethodsHtml; break; // For Stopping the checkout (2) // For Stopping the checkout (2) case RocketWeb_UpsAddressTypeValidator_Helper_Addressvalidation::DISALLOW_CHECKOUT: // If the controller is in the scope (frontend in this case) if ($helper->doValidateAddress()) { // Load customer's shipping address $shipping = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress(); // Get Address type $ups_address_type = RocketWeb_UpsAddressTypeValidator_Helper_Data::getAddressTypeFromAddress($shipping); // Check if the address is valid by calling UPS $is_valid_address = is_numeric($ups_address_type) ? true : false; // Add type to session $session->setVisitorData('ups_address_type', $ups_address_type); // If the country is US and the address is invalid if ($shipping->getCountryId() == 'US' && ($session->getData('invalid_address') || !$is_valid_address)) { // Replace the actual shipping HTML opion with a message $shippingMethodsHtml = '<strong>' . Mage::getStoreConfig('rocketweb_addressvalidator/general/checkout_stop_message') . '</strong>'; return $shippingMethodsHtml; } else { return parent::_getShippingMethodsHtml(); } } else { return parent::_getShippingMethodsHtml(); } break; case RocketWeb_UpsAddressTypeValidator_Helper_Addressvalidation::NO_VALIDATION: return parent::_getShippingMethodsHtml(); default: return parent::_getShippingMethodsHtml(); } }