Reading list Switch to dark mode

    Working on shipping cost of externally made shipping carriers in Prestashop

    Updated 22 April 2016

    As we create Prestashop shipping methods in our modules when creating prestashop extensions. By setting values of some variable of Shipping method we can work on the shipping cost of the Shipping methods for many purposes. Some requirements for which this functionality can be used are as follows-

    •  If you want to add address wise impact price on externally made shipping carriers in your modules.
    • When creating shipping modules like USPS shipping, UPS shipping, DHL shipping etc and you have to calculate price of their carriers via their API.

    And for many more other requirements.

    Set these variable’s values when creating external carriers in your modules.

           $obj_shipping_method->shipping_external = true;

           $obj_shipping_method->external_module_name = module name;

    Searching for an experienced
    Prestashop Company ?
    Find out More

           $obj_shipping_method->is_module = 1;

           $obj_shipping_method->need_range = 1;

    Now lets have a look to the code written under “getPackageShippingCost” function in the file Cart.php,  which is used for the above described functionality.

     

            $shipping_cost = Tools::convertPrice($shipping_cost, Currency::getCurrencyInstance((int)$this->id_currency));
    
            //get external shipping cost from module
            if ($carrier->shipping_external) {
                $module_name = $carrier->external_module_name;
    
                /** @var CarrierModule $module */
                $module = Module::getInstanceByName($module_name);
    
                if (Validate::isLoadedObject($module)) {
                    if (array_key_exists('id_carrier', $module)) {
                        $module->id_carrier = $carrier->id;
                    }
                    if ($carrier->need_range) {
                        if (method_exists($module, 'getPackageShippingCost')) {
                            $shipping_cost = $module->getPackageShippingCost($this, $shipping_cost, $products);
                        } else {
                            $shipping_cost = $module->getOrderShippingCost($this, $shipping_cost);
                        }
                    } else {
                        $shipping_cost = $module->getOrderShippingCostExternal($this);
                    }
    
                    // Check if carrier is available
                    if ($shipping_cost === false) {
                        Cache::store($cache_id, false);
                        return false;
                    }
                } else {
                    Cache::store($cache_id, false);
                    return false;
                }
            }

    This above code executes everytime when calculating the cost of every package in the cart. So if you have defined functions in the above code in your module file. Then you can work on the shipping cost of the carrier which is applied to that package. So give shipping method’s variables values according to which function you want to use in your module because every function has different parameters.

    If in your shipping method’s need_range variable value is supplied as 0 then “getOrderShippingCostExternal($this)” function is called in which only one parameter is available which is current cart object. If need_range variable value is supplied as 1 then –

    • If in your module “getPackageShippingCost” function is defined then “getPackageShippingCost($this, $shipping_cost, $products)” function of your module’s file will be called. In this function three parameters are available first is current cart object , second is carrier’s shipping cost and third is array of product in the current package for which carrier’s cost is calculated.
    • If in your module “getPackageShippingCost” function is not defined Then “getOrderShippingCost($this, $shipping_cost)” function of your module’s file will be called. This function has two parameters. First is object of current cart and second is carrier’s shipping cost and third is array of product in the current package for which carrier’s cost is calculated.

    So if we have to change(increase or decrease) the shipping cost of carrier created by you in your module, No need to override files (like Cart.php ..) you can use this functionality. And because every function has different set of parameters you can set values of the variables of your external module’s shipping carriers and define functions in your module’s main file according to your need and use this functionality of prestashop for your desired requirement.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • Tamjad
    • amit kushwaha (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home