Jump to content

How to return error on shipping step using getOrderShippingCost()


Amazzing

Recommended Posts

Hello,

 

I am working on a carrier module. Delivery price is dynamically fetched from remote server.

After that price is included in order details using getOrderShippingCost() method in modulename.php. 
So, when I have all required fields filled correctly, everything works fine, price is calculated properly.
But if there is an error in any field, for example zipcode is not found, price is just set to 0. 

 

Instead of having 0 I would like to insert a note about what field is wrong and block carrier, but not hide it.

 

I can identify what field is wrong from response of remote server, but how do I display this information on shipping step of order process?

At the moment just I use return false if there is an error. So carrier is just not shown. 

PS 1.5.4.1

 

upd

Here is the basic code scheme I am using right now:

public function getOrderShippingCost($params)
{
  ...
  if (no errors found)
    return $ret['total'];
  else
    return false;
    // instead of return false I would need something like
    // Tools::displayError('Error text'), or trigger a JS event
}
Edited by Amazzing (see edit history)
Link to comment
Share on other sites

You can use return redirect, but make sure this redirect only run on appropriate page so there won't be a redirect loop problem

 

Example

 

On your shiping module file :

public function getOrderShippingCost($params, $shipping_cost)
{
     // check for error according to your conditions
     $error = false;
     if(!$condition_one)
        $error = 1;
     elseif(!$condition_two)
        $error = 2;

     // Define page
     $page = Context::getContext()->controller->php_self;

     // Define order step post variable is there
     $step = Tools::getValue('step');

     // load class Link
     $link = new Link();

     // define error URL for redirection, back to order-address page
     // with params step=1&error_msg=1
     $error_url = $link->getPageLink('order', null, null, array('step' => 1, 'error_msg' => $error));

     if($page == 'order' AND $step == '2' AND $error)
        Tools::redirect($error_url);
}

And then you should override OrderController.php to display error message based on errors conditions

<?php

class OrderController extends OrderControllerCore
{
    public function initContent()
    {
        parent::initContent();
        
        // if a get variable 'error_msg' was defined, display error message
        if(Tools::getValue('error_msg') == 1)
            $this->errors[] = Tools::displayError('Condition one error!');
        elseif(Tools::getValue('error_msg') == 2)
            $this->errors[] = Tools::displayError('Condition two error!');

    }
}
  • Like 1
Link to comment
Share on other sites

You can use return redirect, but make sure this redirect only run on appropriate page so there won't be a redirect loop problem

...

Thanks for your reply. I am sure this is a useful educative information.

But I used another approach, without overriding controller:

public function getOrderShippingCost($params)
{
  ...
  //error is defined as public variable
  $this->carrierError = $ret['errormsg'];
  if (no errors found)
    return $ret['total'];
  else
    return false;
  // when false is returned, carrier is not displayed, but warning message is displayed using hookDisplayHeader
}

public function hookDisplayHeader($params)
{
  if (isset($this->carrierError) &&
  Tools::getValue('controller') == 'order' &&
  Tools::getValue('step') == '2')
  {
     //running JS do display a warning message that carrier may be available if you fix the wrong field
     ...
  }
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...