在Magento结帐页面上发送错误消息(Shipping Method阶段)

时间:2012-01-19 08:52:12

标签: magento module shipping

我开发了一个Magento运输方法模块,它工作正常。 我可以从我的远程服务中检索可用的送货方法列表,但是如果与srvice等的连接出现问题,我希望收到错误消息。 collectRates运营商级别的代码如下所示。

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;
}

如果模块无法检索送货方式列表,则此代码必须返回错误消息:

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

但错误未显示。 我检查了现有的(核心)Magento模块,它们也使用了错误处理程序(Ups,Usps模块),这些模块的错误处理程序也不起作用,错误信息没有显示。

请问一下这个问题的解决方案。

Tnank you!

1 个答案:

答案 0 :(得分:2)

你的代码很好。您只需要为您的运营商进行一些配置,以便始终显示其方法。

已在Mage_Shipping_Model_Shipping-> collectCarrierRates()(第176行)中显示,如果结果出错,Magento会在您的运营商配置中检查“showmethod”(如果评估结果为0,则丢弃它):

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

所以你只需要在config.xml中添加它:

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