Jump to content

getOrderShippingCost is called 16 times


Recommended Posts

Here is my module code:

<?php

if(!defined('_PS_VERSION_')) {
    exit;
}

class MyModule extends CarrierModule
{
    public  $id_carrier;

    public function __construct()
    {
        $this->name                     = 'mymodule';
        $this->tab                      = 'shipping_logistics';
        $this->version                  = '1.0.0';
        $this->author                   = 'My Module';
        $this->need_instance            = 0;
        $this->ps_versions_compliancy   = array('min' => '1.5', 'max' => '1.7');
        $this->bootstrap                = true;

        parent::__construct();

        $this->displayName              = $this->l('My module');
        $this->description              = $this->l("Integration module with My module delivery system");

        $this->confirmUninstall         = $this->l('Are you sure you want to uninstall?');     
    }

    public function install()
    {
        $carrierConfig = array(
            
            0 => array(
                'name'                  => 'My module Standard',
                'id_tax_rules_group'    => 0, // We do not apply thecarriers tax
                'active'                => true,
                'deleted'               => 0,
                'shipping_handling'     => false,
                'range_behavior'        => 0,
                'delay'                 => array(
                    'fr' => 'Delivery in 4 to 10 days',
                    'en' => 'Delivery in 4 to 10 days',
                ),
                'id_zone'               => 1, // Area where the carrier operates
                'is_module'             => true, // We specify that it is a module
                'shipping_external'     => true,
                'external_module_name'  => $this->name, // We specify the name of the module
                'need_range'            => true,
            ),
            1 => array(
                'name'                  => 'My module Express',
                'id_tax_rules_group'    => 0, // We do not apply thecarriers tax
                'active'                => true,
                'deleted'               => 0,
                'shipping_handling'     => false,
                'range_behavior'        => 0,
                'delay'                 => array(
                    'fr' => 'Delivery with in a day',
                    'en' => 'Delivery with in a day',
                ),
                'id_zone'               => 1, // Area where the carrier operates
                'is_module'             => true, // We specify that it is a module
                'shipping_external'     => true,
                'external_module_name'  => $this->name, // We specify the name of the module
                'need_range'            => true,
            )
        );
        
        $id_carrier_standard = $this->installExternalCarrier($carrierConfig[0]);
        $id_carrier_express  = $this->installExternalCarrier($carrierConfig[1]);
        Configuration::updateValue('MYMODULE_IDCARRIER_STANDARD', $id_carrier_standard);
        Configuration::updateValue('MYMODULE_IDCARRIER_EXPRESS', $id_carrier_express);

        // PARENT MAIN TAB
        $parent_tab = new Tab();
               foreach(Language::getLanguages(true) as $lang){
            $parent_tab->name[(int) $lang['id_lang']] = $this->l('My module');
        }
        $parent_tab->class_name = 'AdminWaitingJobs';
        $parent_tab->id_parent  = 0; // Home tab
        $parent_tab->active     = 1;
        $parent_tab->module     = $this->name;
        $parent_tab->add();

        // SUB TAB 1
        $sub1 = new Tab();
        foreach(Language::getLanguages(true) as $lang){
            if($lang['iso_code'] && $lang['iso_code'] == 'fr') {
               $sub1->name[(int) $lang['id_lang']] = 'Missions en attente';                
            }
            else {
               $sub1->name[(int) $lang['id_lang']] = 'Waiting Jobs';
            }
        }
        $sub1->class_name = 'AdminWaitingJobs';
        $sub1->id_parent  = $parent_tab->id; // Home tab
        $sub1->active     = 1;
        $sub1->module     = $this->name;
        $sub1->add();

        // SUB TAB 2
        $sub2 = new Tab();
        foreach(Language::getLanguages(true) as $lang){

            if($lang['iso_code'] && $lang['iso_code'] == 'fr') {
               $sub2->name[(int) $lang['id_lang']] = 'Missions en cours';
            }
            else {
               $sub2->name[(int) $lang['id_lang']] = 'Current Jobs';
            }
        }
        $sub2->class_name = 'AdminCurrentJobs';
        $sub2->id_parent = $parent_tab->id; // Home tab
        $sub2->active = 1;
        $sub2->module = $this->name;
        $sub2->add();
        
        // SUB TAB 3
        $sub3 = new Tab();
        foreach(Language::getLanguages(true) as $lang){
            if($lang['iso_code'] && $lang['iso_code'] == 'fr') {
               $sub3->name[(int) $lang['id_lang']] = 'Missions complétées';
            }
            else {
               $sub3->name[(int) $lang['id_lang']] = 'Completed Jobs';
            }
        }
        $sub3->class_name = 'AdminCompletedJobs';
        $sub3->id_parent  = $parent_tab->id; // Home tab
        $sub3->active     = 1;
        $sub3->module     = $this->name;
        $sub3->add();

        // SUB TAB 4
        $sub4 = new Tab();
        foreach(Language::getLanguages(true) as $lang){
            if($lang['iso_code'] && $lang['iso_code'] == 'fr') {
               $sub4->name[(int) $lang['id_lang']] = 'Configuration';
            }
            else {
               $sub4->name[(int) $lang['id_lang']] = 'Configuration';
            }
        }
        $sub4->class_name = 'AdminConfig';
        $sub4->id_parent = $parent_tab->id; // Home tab
        $sub4->active = 1;
        $sub4->module = $this->name;
        $sub4->add();

        return parent::install() && $this->registerHook('updateCarrier');
    }

    public function uninstall()
    {
        // We first carry out a classic uninstall of a module
        // We delete the carriers we created earlier
        $id_carrier_standard = (int) Configuration::get('MYMODULE_IDCARRIER_STANDARD');
        Db::getInstance()->update('carrier', ['deleted'=>'1'], 'id_carrier='.$id_carrier_standard);

        $id_carrier_express = (int) Configuration::get('MYMODULE_IDCARRIER_EXPRESS');
        Db::getInstance()->update('carrier', ['deleted'=>'1'], 'id_carrier='.$id_carrier_express);

        // Uninstall Tabs
        $moduleTabs = Tab::getCollectionFromModule($this->name);
        if(!empty($moduleTabs)) {
            foreach ($moduleTabs as $moduleTab) {
                $moduleTab->delete();
            }
        }

        if(!parent::uninstall() ||           
            !$this->unregisterHook('updateCarrier') ||
            !$this->unregisterHook('actionPaymentConfirmation')) {
            return false;
        }

        return true;
    }

    public function hookUpdateCarrier($params)
    {
        if($params['id_carrier'] == Configuration::get('MYMODULE_IDCARRIER_STANDARD')) {
            Configuration::updateValue('MYMODULE_IDCARRIER_STANDARD', $params['carrier']->id);
        }
        if($params['id_carrier'] == Configuration::get('MYMODULE_IDCARRIER_EXPRESS')) {
            Configuration::updateValue('MYMODULE_IDCARRIER_EXPRESS', $params['carrier']->id);
        }
    }

 

    public function getOrderShippingCost($params, $cost)
    {

        error_log(json_encode('Inside  getOrderShippingCost') ."\n", 3, dirname(__FILE__).'/logging.log');
       
       return 20;
    }

    public function getContent()
    {
        // One config page to rule them all
        Tools::redirectAdmin('index.php?controller=AdminConfig&token='.Tools::getAdminTokenLite('AdminConfig'));
    }

    public static function installExternalCarrier($config)
    {
        $carrier                        = new Carrier();
        $carrier->name                  = $config['name'];
        $carrier->id_tax_rules_group    = $config['id_tax_rules_group'];
        $carrier->id_zone               = $config['id_zone'];
        $carrier->active                = $config['active'];
        $carrier->deleted               = $config['deleted'];
        $carrier->delay                 = $config['delay'];
        $carrier->shipping_handling     = $config['shipping_handling'];
        $carrier->range_behavior        = $config['range_behavior'];
        $carrier->is_module             = $config['is_module'];
        $carrier->shipping_external     = $config['shipping_external'];
        $carrier->external_module_name  = $config['external_module_name'];
        $carrier->need_range            = $config['need_range'];

        $languages = Language::getLanguages(true);
        foreach ($languages as $language) {
            if($language['iso_code'] == 'fr') {
                $carrier->delay[(int) $language['id_lang']] = $config['delay'][$language['iso_code']];
            }
            if($language['iso_code'] == 'en') {
                $carrier->delay[(int) $language['id_lang']] = $config['delay'][$language['iso_code']];
            }
            if($language['iso_code'] == Language::getIsoById(Configuration::get('PS_LANG_DEFAULT'))) {
                $carrier->delay[(int) $language['id_lang']] = $config['delay'][$language['iso_code']];
            }
        }

        if($carrier->add()) {
            
            $groups = Group::getGroups(true);
            foreach ($groups as $group) {
                Db::getInstance()->insert('carrier_group', array('id_carrier' => (int) ($carrier->id), 'id_group' => (int) ($group['id_group'])));
            }

            $rangePrice             = new RangePrice();
            $rangePrice->id_carrier = $carrier->id;
            $rangePrice->delimiter1 = '0';
            $rangePrice->delimiter2 = '10000';
            $rangePrice->add();

            $rangeWeight             = new RangeWeight();
            $rangeWeight->id_carrier = $carrier->id;
            $rangeWeight->delimiter1 = '0';
            $rangeWeight->delimiter2 = '10000';
            $rangeWeight->add();

            $zones = Zone::getZones(true);
            foreach ($zones as $zone) {
                Db::getInstance()->insert('carrier_zone', array('id_carrier' => (int) ($carrier->id), 'id_zone' => (int) ($zone['id_zone'])));
                Db::getInstance()->insert('delivery', array('id_carrier' => (int) ($carrier->id), 'id_range_price' => (int) ($rangePrice->id), 'id_range_weight' => null, 'id_zone' => (int) ($zone['id_zone']), 'price' => '0'), true);
                Db::getInstance()->insert('delivery', array('id_carrier' => (int) ($carrier->id), 'id_range_price' => null, 'id_range_weight' => (int) ($rangeWeight->id), 'id_zone' => (int) ($zone['id_zone']), 'price' => '0'), true);
            }

            // Copy Logo
            if(!copy(dirname(__FILE__).'/carrier.png', _PS_SHIP_IMG_DIR_.'/'.(int) $carrier->id.'.jpg')) {
                return false;
            }

            // Return ID Carrier
            return (int) ($carrier->id);
        }

        return false;
    }
}
Link to comment
Share on other sites

  • 1 year later...

Same problem wih Prestashop 1,7
Seems to be a bug. If you have eg 4 carriers with Id 1,2,3,4, your getOrderShippingCost() function is called one time with id 1 then again with d 1 then wih id 2, then wih 1,2,3 and so on.

So rather that calling 1-2-3-4 the loop call 1 then 1-2 then 1-2-3 then1-2-3-4...
I'll have a look at the code so check that.

 

  • Thanks 1
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...