3
votes

I developed a Magento shipping method module and it works fine. I can retrieve the list of available shipping methods from my remote service but I want to have an error message if something will be wrong with connection to the srvice or etc. The code of collectRates carrier class is listed below.

public function collectRates(Mage_Shipping_Model_Rate_Request $request) {
if (!$this->getConfigFlag('active')) {
  return false;
}

require_once Mage::getBaseDir() . '/app/code/local/Citymail/CitymailDeliveryOption/lib/syvo_service_connect.php';

$citymail_service_url = $this->getConfigData('service_url') . '/syvo_service';

$connection = new syvo_service_connect($citymail_service_url);
if($connection->connect()) {

  $handling = Mage::getStoreConfig('carriers/'.$this->_code.'/handling');
  $rate_result = Mage::getModel('shipping/rate_result');

  $product_qty = 0;
  $products_data = array();

  $currency = Mage::app()->getStore()->getCurrentCurrencyCode();

  $quote = Mage::getSingleton('checkout/session')->getQuote();
  foreach($quote->getAllVisibleItems() as $cartItem) {
    $product_qty += $cartItem->getQty();
    $products_data[$cartItem->getId()] = array(
      'length_units' => 'cm',
      'length' => 0,
      'width' => 0,
      'height' => 0,
      'weight_units' => 'kg',
      'weight' => $cartItem->getProduct()->getWeight(),
      'shippable' => 'a',
      'sell_price' => $cartItem->getProduct()->getPrice(),
      'currency' => $currency,
    );
  }

  $shipping_address = Mage::getModel('checkout/cart')->getQuote()->getShippingAddress();
  $password = md5($this->getConfigData('service_password'));
  $params = array(
     'passw=' . $password,
     'login=' . $this->getConfigData('service_login'),
     'action=get_shipping_options',
     'product_qty=' . $product_qty,
     'products_data=' . json_encode($products_data),
     'country=' . $shipping_address->getCountryId(),
     'postal_code=' . $shipping_address->getPostcode(),
   );


  $response = $connection->send(implode('&', $params), 0, 30);
  $response_status_line = $connection->getResponseStatusLine();
  if((int)$response_status_line[1] == 403) {
    //Access denied message
  }
  else {
    $response_array = json_decode($response);

    $citymail_service_points_data = array();
    if($response_array->syvo_service_responce->type == 'success') {
      $options = array();        
      foreach($response_array->syvo_service_responce->data->syvo_shipping_methods as $key_method => $shipping_method) {

        $method_key = 'opt--' . $shipping_method->type . '--' . $shipping_method->distributor;

        if($shipping_method->type == 'DHL_SERVICE_POINT') {
          $citymail_service_points_data['service_points'][$shipping_method->id] = array(
            'dhl_service_point_postcode' => $shipping_method->postcode,
            'dhl_service_point_address' => $shipping_method->address,
          );
          $method_key .= '--' . $shipping_method->id;
        }

        $method = Mage::getModel("shipping/rate_result_method");
        $method->setCarrier($this->_code);
        $method->setCarrierTitle(Mage::getStoreConfig('carriers/' . $this->_code . '/title'));
        $method->setMethod($method_key);
        $method->setMethodTitle($shipping_method->text);
        $method->setCost($shipping_method->rate);
        $method->setPrice($shipping_method->rate + $handling);
        $rate_result->append($method);
      }

      $street = $shipping_address->getStreet();
      $citymail_temporary_order_data = array(
        'login' => $this->getConfigData('service_login'),
        'password' => $password,
        'country_id' => $shipping_address->getCountryId(),
        'company_name' => $shipping_address->getCompany(),
        'name' => trim($shipping_address->getFirstname() . ' ' . $shipping_address->getLastname()),
        'postal_code' => $shipping_address->getPostcode(),
        'phone_number' => $shipping_address->getTelephone(),
        'email' => $shipping_address->getEmail(),
        'street' => $street[0],
        'city' => $shipping_address->getCity(),
        'matrix_id' => $response_array->syvo_service_responce->data->matrix_id,
        'service_url' => $citymail_service_url,
      );


      $citymail_temporary_order_data = $citymail_service_points_data + $citymail_temporary_order_data;
      Mage::getSingleton('core/session')->setCitymailTemporaryOrderData($citymail_temporary_order_data);


    }
    else {
      //Some error handler

      $error = Mage::getModel("shipping/rate_result_error");
      $error->setCarrier('citymaildeliveryoption');
      $error->setErrorMessage('sasasas');
      $rate_result->append($error);


    }
  }

}
else {

}


return $rate_result;
}

This code has to return an error message if the module couldn't retrieve the list of shipping methods:

$error = Mage::getModel("shipping/rate_result_error");
$error->setCarrier('citymaildeliveryoption');
$error->setErrorMessage('sasasas');
$rate_result->append($error);

But the error is not displaying. I checked an existing (core) Magento modules which use error handlers too (Ups, Usps modules) and error handlers of these modules also do not work, error message is not displaying.

Could you please advice some solution of this problem.

Tnank you!

1
How are you currently debugging? I suggest you use firephp.org so you can spit out text or environmental variables with 'Mage::helper('firephp')->debug($var);' - Jared Eitnier
Is <?php echo $this->getChildHtml('global_messages') ?> present in your main templates (1column.phtml, 2columns-left.phtml, etc.)? - Anthony

1 Answers

2
votes

Your code is good. You just need a little bit configuration for your carrier to always displays its methods.

Has shown in Mage_Shipping_Model_Shipping->collectCarrierRates() (line 176), Magento checks for 'showmethod' in your carrier config if a result is in error (and discard it, if it evaluates to 0) :

if ($carrier->getConfigData('showmethod') == 0 && $result->getError()) {
    return $this;
}

So you just have to add this in your config.xml :

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <default>
        <carriers>
            <citymaildeliveryoption>
                <showmethod>1</showmethod>
            </citymaildeliveryoption>
        </carriers>
    </default>
</config>