immanueljl Posted July 31, 2017 Share Posted July 31, 2017 Hi, i am just a beginner and i try to create my own carrier module. So far its cool but i stack at this point. I know i can return any integer at "public function getOrderShippingCost($params, $shipping_cost)" as my shipping price. My problem is i have 3 carrier and i want a different cost each carrier. Here my full code: class belvg_freightdelivery extends CarrierModule { const PREFIX = 'belvg_fcd_'; public $id_carrier; protected $_hooks = array( 'header', 'actionCarrierUpdate', 'displayOrderConfirmation', 'displayAdminOrder', 'displayBeforeCarrier', ); protected $_carriers = array( //HERE I PUT 3 CARRIER 'Freight Company' => 'fcd', 'Freight Company2' => 'fcd2', 'Freight Company3' => 'fcd3', ); public function __construct() { $this->name = 'belvg_freightdelivery'; $this->tab = 'shipping_logistics'; $this->version = '1.6.2'; $this->author = 'BelVG'; $this->bootstrap = TRUE; $this->module_key = ''; parent::__construct(); $this->displayName = $this->l('Delivery by freight company'); $this->description = $this->l('If your customer choose “Delivery by freight company”, you get a text field for entering information to the freight company'); } public function getTemplate($area, $file) { return 'views/templates/' . $area . '/' . $file; } public function install() { if (parent::install()) { foreach ($this->_hooks as $hook) { if (!$this->registerHook($hook)) { return FALSE; } } if (!$this->installDB()) { return FALSE; } if (!$this->createCarriers()) { return FALSE; } return TRUE; } return FALSE; } protected function uninstallDB() { $sql = array(); $sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'belvg_freightdelivery`'; foreach ($sql as $_sql) { if (!Db::getInstance()->Execute($_sql)) { return FALSE; } } return TRUE; } protected function installDB() { $sql = array(); $sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'belvg_freightdelivery` ( `id_belvg_freightdelivery` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT, `id_order` INT( 11 ) UNSIGNED, `details` TEXT, `date_upd` DATETIME NOT NULL, PRIMARY KEY (`id_belvg_freightdelivery`) ) ENGINE = ' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8'; foreach ($sql as $_sql) { if (!Db::getInstance()->Execute($_sql)) { return FALSE; } } return TRUE; } protected function createCarriers() { foreach ($this->_carriers as $key => $value) { //Create own carrier $carrier = new Carrier(); $carrier->name = $key; $carrier->active = TRUE; $carrier->deleted = 0; $carrier->shipping_handling = FALSE; $carrier->range_behavior = 0; $carrier->delay[Configuration::get('PS_LANG_DEFAULT')] = 'Depends on the freight company [1-2 days]'; $carrier->shipping_external = TRUE; $carrier->is_module = 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' => '25'), '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' => '25'), '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; } public function uninstall() { if (parent::uninstall()) { foreach ($this->_hooks as $hook) { if (!$this->unregisterHook($hook)) { return FALSE; } } /*if (!$this->uninstallDB()) { return FALSE; }*/ if (!$this->deleteCarriers()) { return FALSE; } return TRUE; } return FALSE; } public function getOrderShippingCost($params, $shipping_cost) { //reason of using $shipping_cost => CarrierModuleCore::getOrderShippingCost() //$params - you can use this parameter for customizing delivery price calculation $carrierObj = new Carrier(Configuration::get(self::PREFIX . 'fcd')); $delivery_price = Carrier::getDeliveryPriceByRanges($carrierObj->getRangeTable(), Configuration::get(self::PREFIX . 'fcd')); $max_delivery_price = 0; foreach ($delivery_price as $d_price) { if ($d_price['price'] > $max_delivery_price) { $max_delivery_price = $d_price['price']; } } return $max_delivery_price; //THIS PART RETURN A COST FOR ALL CARRIER, I WANT TO RETURN A DIFFERENT SHIPPING COST PER CARRIER (Example: fcd1 => 100, fcd2 => 200, fcd3 => 300) } public function getOrderShippingCostExternal($params) { return $this->getOrderShippingCost($params, 0); } public function hookActionCarrierUpdate($params) { if ($params['carrier']->id_reference == Configuration::get(self::PREFIX . 'fcd_reference')) { Configuration::updateValue(self::PREFIX . 'fcd', $params['carrier']->id); } } It will be good if someone can help me. Thanks! NOTE: i use prestashop 1.6 Link to comment Share on other sites More sharing options...
rudilee Posted September 29, 2017 Share Posted September 29, 2017 (edited) Just in case someone need the answer in future, you can get the carrier id for each carrier that being called getOrderShippingCostExternal function for by declaring public member variable of id_carrier for your module like so: class CustomCarrier extends CarrierModule { public $id_carrier; } In the Cart.php if it detect your module have id_carrier member variable it will set it with current carrier id being called the getOrderShippingCostExternal function for, so you could get it in your getOrderShippingCostExternal function like so: public function getOrderShippingCostExternal($params) { return calculateShippingCostForCarrierId($this->id_carrier); } Edited September 29, 2017 by rudilee (see edit history) Link to comment Share on other sites More sharing options...
mathdex Posted March 14, 2019 Share Posted March 14, 2019 On 9/29/2017 at 1:05 PM, rudilee said: Just in case someone need the answer in future, you can get the carrier id for each carrier that being called getOrderShippingCostExternal function for by declaring public member variable of id_carrier for your module like so: class CustomCarrier extends CarrierModule { public $id_carrier; } In the Cart.php if it detect your module have id_carrier member variable it will set it with current carrier id being called the getOrderShippingCostExternal function for, so you could get it in your getOrderShippingCostExternal function like so: public function getOrderShippingCostExternal($params) { return calculateShippingCostForCarrierId($this->id_carrier); } Hi there, When I declare that variable, it doesn't seem to actually fill it with the value of the carrier, it just stays empty. When is the getOrderShippingCostExternal function actually called ? I need to hide one of the two carriers I created based on the postcode given by the user, so I use the getOrderShippingCost function to return false to not display the carrier, which works perfectly fine in 1.7,but in 1.6 it just hides both my carriers, which is not what I want. Any idea how I could fix that ? Thanks in advance for your response, MD. PS: This is my code inside getOrderShippingCost public function getOrderShippingCost($cart, $shipping_cost) { $carrier = new Carrier(ID_CARRIER_I_WANT_TO_HIDE); $address = new Address($cart->id_address_delivery); $jsonFileLocation = Tools::file_get_contents("http://somelocation/postcodes.json"); $allPostcodes = json_decode($jsonFileLocation, true); for($i = 0; $i < sizeof($allPostcodes["postcodes"]); $i++){ if($address->postcode == $allPostcodes["postcodes"][$i]){ return (float)$shipping_cost; } } return false; } Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now