Jump to content

How to use the new class CarrierModule of v1.4


Gabriel Paladino

Recommended Posts

Anyone know me through an example using the new functions in Class CarrierModule?

abstract class CarrierModuleCore extends Module
{
   abstract function getOrderShippingCost($params,$shipping_cost);    
   abstract function getOrderShippingCostExternal($params);    
}



I saw the Cart class calls the functions when calculating the value of using the function getOrderShippingCost delivery, but do not know how to use it properly. Someone from the development team can explain it?

if ($carrier->shipping_external)
       {    
           $moduleName = $carrier->external_module_name;
           if(file_exists(_PS_MODULE_DIR_ .$moduleName.'/'.$moduleName.'.php'))
               include_once(_PS_MODULE_DIR_ .$moduleName.'/'.$moduleName.'.php');
           else
               die(Tools::displayError('Hack attempt: "carrier error"'));
           $module = new $moduleName();
           return $module->getOrderShippingCostExternal($this);
           if($carrier->need_range)
               return $module->getOrderShippingCost($this,$shipping_cost);
       }
       else
           return floatval(Tools::ps_round(floatval($shipping_cost), 2));



Regarding the above code, as getOrderShippingCost will be called if there is a return on getOrderShippingCostExternal before.

Thanks.

Link to comment
Share on other sites

Looking at the code, it seems you can create a module in the modules directory that extends CarrierModuleCore instead of Module and implement those two shipping functions. I can't figure out how to link a carrier to a module though. I think you might have to do something like create a class in the override/classes directory to override the Carrier class and set the external_module_name to the directory name of the module, but I'm not sure. I'd also really like documentation on the new controllers and what can be overridden.

Link to comment
Share on other sites

Thanks for your reply rocky

In the case as to external_module_name is defined via the carrier database. See the new class carrier.

    public function getFields()
   {
       parent::validateFields();
       $fields['id_tax'] = intval($this->id_tax);
       $fields['name'] = pSQL($this->name);
       $fields['url'] = pSQL($this->url);
       $fields['active'] = intval($this->active);
       $fields['deleted'] = intval($this->deleted);
       $fields['shipping_handling'] = intval($this->shipping_handling);
       $fields['range_behavior'] = intval($this->range_behavior);
       $fields['shipping_method'] = intval($this->shipping_method);
       $fields['is_module'] = intval($this->is_module);
       $fields['shipping_external'] = intval($this->shipping_external);
       $fields['external_module_name'] = $this->external_module_name;
       $fields['need_range'] = $this->need_range;

       return $fields;
   }



What I could not understand yet is about need_range and how it will be possible to use it if there is a return before the "if".

Another interesting thing is if the module ignores all checks and values accumulated by the function of the cart, why not run in the beginning of the function the external module instead of the end of the function?

How is it possible to suggest modifications to the original code? I think it would be interesting if instead of returning these two functions were added to the shipping_cost that exists in that function of the card. Until that if any tax for that carrier, it will be applied before the calculation made by the module and thus the value returned by the module will not have the tax.

In short, I'm still thinking a little lack of logic in the implementation of these functions, so my question about how to correctly calculate and return the value with the module.

Link to comment
Share on other sites

That's a good point. It seems the $module->getOrderShippingCost() function will never be called since the result of $module->getOrderShippingCostExternal() is returned first. You can post this issue on the bug tracker. Hopefully, the PrestaShop team will provide an explanation.

Link to comment
Share on other sites

Hi Gabriel Paladino,

What about the use of these functions? So need_range is just to set even if the module will use the current shipping_cost or not, right?


Yes if carrier need_range is set to true the module will use the range of prestashop, like this you can add overcost by using this :

return $module->getOrderShippingCost($this, $shipping_cost);


If no additional cost is required simply return the input value "shipping_cost";

If the module dont use prestashop range, set need_range to false and use this :

return $module->getOrderShippingCostExternal($this);


In this case it is the module that returns the entire cost of shipping

I'm making a simple module for example to illustrate the operation of external shipping cost

Vincent

Link to comment
Share on other sites

Thanks Vincent, I had developed a module like this, and added a hack on Cart.php version 1.3 for it to work.

This works well and will get even better in version 1.4.

One last thing, my question about the tax continues. If within the Cart.php the value calculated by the module was added to shipping_cost and only after that the tax calculated, it would be better?

Gabriel.

Link to comment
Share on other sites

  • 6 months later...

I have a Delivery Change by Zip Code running quite nicely derived from Vincent's externalShippingRange module. Wowee, Thank you, Vincent ! ! ! But it displays a big truck image on the shopping cart where Delivery Method selected. If I go to edit the Carrier, Logo shows: No file chosen.

How can I get rid of Vincent's truck pix and change it to "Delivery by Courier"?

Link to comment
Share on other sites

  • 9 months later...

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...