Jump to content

Shipping costs returns to zero after order confirmation


Recommended Posts

I created a custom module that inherits from CarrierModule (I'm on vers. 1.7.6) following the "classic example code" that can be found on web (which I do not report for brevity).
This module adds custom carriers (with properties shipping_external = true and need_range = true).
When I go to checkout and choose the shipper, it calculates the order summary correctly by adding the shipping cost line and the cart total is correct! Everything seems to work perfectly ..
But when I choose the payment method and confirm the order, prestashop records everything and redirect me to the order confirmation page but, at this point, summarizes the order with FREE shipping cost!? even in the invoice the shipping cost is no longer there but it becomes free!

Could someone tell me what happens during the order registration? why is the shipping cost reset?
What can I check?

Thanks in advance

Link to comment
Share on other sites

In my module, on getOrderShippingCost($cart, $shipping_cost), when the current page is "order" (get by $this->context->controller->php_self)
I calculate carrier shipping cost and return it to the function: this works fine and I can see costs of carriers!

When I'm on page "order-confirmation" I return ship cost too, but in this case it seems have no effect!
Another thing: the second parameter of function (that I called $shipping_cost) value is always set to zero.

Edited by girtri (see edit history)
Link to comment
Share on other sites

this is an example  of module (txshipping.php) code that, in the end, reproduces the exact same problem: after choosing "my new carrier", the payment method and confirming the order, the shipping costs disappear:

<?php
if (!defined('_PS_VERSION_')) {
    exit;
}

class TxShipping extends CarrierModule
{
    const PREFIX = 'tx_';
    public $id_carrier;

    private $loopCount = 0;
    private $shipCost = 0;

    protected $_hooks = array(
        'actionCarrierUpdate',
        'displayOrderConfirmation',
    );

    protected $_carriers = array(
        //"Public carrier name" => "technical name",
    	'My new carrier' => 'txshipping',
    );

	public function __construct()
    {
        $this->name = 'txshipping';
        $this->tab = 'shipping_logistics';
        $this->version = '1.0.0';
        $this->author = 'Gerry';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = [
            'min' => '1.7.1.0',
            'max' => _PS_VERSION_
        ];
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('Tx Shipping');
        $this->description = $this->l('manage shipping costs');

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

        if (!Configuration::get('TXSHIPPING_NAME')) {
            $this->warning = $this->l('No name provided');
        }
    }

    public function getTemplate($area, $file)
    {
        return 'views/templates/' . $area . '/' . $file;
    }

	//-------------------------------------------------
	// Hooks
	//-------------------------------------------------
    public function hookActionCartSave($params)
    {
        global $smarty;
        $product_list = $params['cart']->getProducts();
        $total_weight = 0;
        $total_extra_cost = 0;

        foreach ( $product_list as $product ):
            $total_weight += $product['weight_attribute'];
        endforeach;

        $total_extra_cost = $total_weight * 33;
    }


    public function hookActionCarrierUpdate($params)
    {
        if ($params['carrier']->id_reference == Configuration::get(self::PREFIX . 'fcd_reference')) {
            Configuration::updateValue(self::PREFIX . 'fcd', $params['carrier']->id);
        }
    }

    public function getOrderShippingCost($params = null, $shipping_cost = 0) {
        $curPage = $this->context->controller->php_self;

        /* using test on wich page is running cause the following code is always executed (even if is loading home page!?)
           I don't understand why */
        if ($curPage == "order") {
            $this->loopCount++;

            if ($this->loopCount == 1) {
                $this->shipCost = 77;

                $address = new Address($params->id_address_delivery);
                $cap = $address->postcode;
                $curID = $this->id_carrier;
            }
            return floatval($this->shipCost);

        } elseif ($curPage == "order-confirmation") {
            $test = 77; // for simple test
            return floatval($test);

        } else {
            if ($curPage != "pagenotfound") {
                $this->loopCount = 0;
                $this->shipCost = 0;
            }
        }
    }

    public function getOrderShippingCostExternal($params){
        //return 999; costi spedizione
        return $this->getOrderShippingCost($params, 0);
    }

	//-------------------------------------------------
	// Setup
	//-------------------------------------------------
    public function install()
    {
        if (parent::install()) {
            /*
            foreach ($this->_hooks as $hook) {
                if (!$this->registerHook($hook)) {
                    return false;
                }
            }*/

            if (!$this->createCarriers()) {
                return false;
            }

            return true;
        }
        return false;
    }

    public function uninstall()
    {
        if (parent::uninstall()) {
            foreach ($this->_hooks as $hook) {
                if (!$this->unregisterHook($hook)) {
                    return false;
                }
            }

			if (!$this->deleteCarriers()) {
                return false;
            }

            return true;
        }
        return false;
    }

	//-------------------------------------------------
	// Funzioni private
	//-------------------------------------------------
	protected function createCarriers()
    {
        foreach ($this->_carriers as $key => $value) {
            //Create own carrier
            $carrier = new Carrier();
            $carrier->name = $key;
            $carrier->id_tax_rules_group = 0;
            $carrier->active = 1;
            $carrier->deleted = 0;
            foreach (Language::getLanguages(true) as $language)
                $carrier->delay[(int)$language['id_lang']] = 'Delay [1-2 days]';
            $carrier->shipping_handling = false;
            $carrier->range_behavior = 0;
            $carrier->is_module = true;
            $carrier->shipping_external = true;
            $carrier->external_module_name = $this->name;
            $carrier->need_range = true;

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

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

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

                $zones = Zone::getZones(true);
                foreach ($zones as $z) {
                    Db::getInstance()->autoExecute(_DB_PREFIX_ . 'carrier_zone',
                        array('id_carrier' => (int) $carrier->id, 'id_zone' => (int) $z['id_zone']), 'INSERT');
                    Db::getInstance()->autoExecuteWithNullValues(_DB_PREFIX_ . 'delivery',
                        array('id_carrier' => $carrier->id, 'id_range_price' => (int) $rangePrice->id, 'id_range_weight' => NULL, 'id_zone' => (int) $z['id_zone'], 'price' => '0'), 'INSERT');
                    Db::getInstance()->autoExecuteWithNullValues(_DB_PREFIX_ . 'delivery',
                        array('id_carrier' => $carrier->id, 'id_range_price' => NULL, 'id_range_weight' => (int) $rangeWeight->id, 'id_zone' => (int) $z['id_zone'], 'price' => '0'), 'INSERT');
                }

                copy(dirname(__FILE__) . '/views/img/carrier.jpg', _PS_SHIP_IMG_DIR_ . '/' . (int) $carrier->id . '.jpg');

                Configuration::updateValue(self::PREFIX . $value, $carrier->id);
                Configuration::updateValue(self::PREFIX . $value . '_reference', $carrier->id);
            }
        }

        return TRUE;
    }

	protected function deleteCarriers()
    {
        foreach ($this->_carriers as $value) {
            $tmp_carrier_id = Configuration::get(self::PREFIX . $value);
            $carrier = new Carrier($tmp_carrier_id);
            $carrier->delete();
        }

        return true;
    }
}

 

Edited by girtri (see edit history)
Link to comment
Share on other sites

found bug: problem is caused by my attempt to limit the number of execution of code in "getOrderShippingCost"..
 

$curPage = $this->context->controller->php_self;
if ($curPage == "order") {
...

from page "order" to "order-confirmation", prestashop invoke multiple times "getOrderShippingCost" in which $curPage is null and my code didn't return a value to the function:
I handled the case and now it seems to work.

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