Prestashop How To Create A Module
Today, we'll show you how to create a module for the shipping service. The main peculiarity is that such module should be inherited from the abstract class CarrierModule and should implement the following 2 methods: «getOrderShippingCost», «getOrderShippingCostExternal»:
abstract class CarrierModuleCore extends Module { abstract public function getOrderShippingCost ( $params , $shipping_cost ) ; abstract public function getOrderShippingCostExternal ( $params ) ; } |
These methods are required to calculate shipping costs. We will create a shipping module to better understand all the nuances. The theoretical part is available on this page: http://doc.prestashop.com/display/PS16/Creating+a+carrier+module. At the end of the article you can download the ready-to-use module. So, let's begin:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public function install ( ) { if ( parent :: install ( ) ) { foreach ( $this -> _hooks as $hook ) { if ( ! $this -> registerHook ( $hook ) ) { return FALSE ; } } if ( ! $this -> createCarriers ( ) ) { //function for creating new currier return FALSE ; } return TRUE ; } return FALSE ; } |
Here we use the standard constructor, and we are going to use only one hook "асtiоnCаrrierUpdаte". This hook is required to ensure that the module will not lose connection with the carrier, created by the module. The thing is that a carrier is given a new ID each time it is edited through the admin panel. Users do not notice this, but for the program this is very important, because ID serves as a unique identifier, which we can use to find the necessary carrier at any time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class belvg_studyshipping extends CarrierModule { const PREFIX = 'belvg_studyshipping_' ; protected $_hooks = array ( 'actionCarrierUpdate' , //For control change of the carrier's ID (id_carrier), the module must use the updateCarrier hook. ) ; protected $_carriers = array ( //"Public carrier name" => "technical name", 'My new carrier' = > 'belvgstudy' , ) ; public function __construct ( ) { $this -> name = 'belvg_studyshipping' ; $this -> tab = 'shipping_logistics' ; $this -> version = '1.6.1' ; $this -> author = 'BelVG' ; $this -> bootstrap = TRUE ; parent :: __construct ( ) ; $this -> displayName = $this -> l ( 'Belvg Shipping' ) ; $this -> description = $this -> l ( 'How to create shipping module.' ) ; } |
When installing the module we also create a carrier and assign it to the module. This way the carrier will be receiving the shipping costs not from a static database, but dynamically, perhaps, by using a third-party API. It is important to keep the ID and reference of the carrier, because we are going to identify the carrier exactly by that field when the ID changes.
Partner With Us
Let's discuss how to grow your business. Get a Free Quote.
Talk to Igor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | protected function createCarriers ( ) { foreach ( $this -> _carriers as $key = > $value ) { //Create new 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' ) ] = $key ; $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' = > '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/' . $value . '.jpg' , _PS_SHIP_IMG_DIR_ . '/' . ( int ) $carrier -> id . '.jpg' ) ; //assign carrier logo Configuration:: updateValue ( self :: PREFIX . $value , $carrier -> id ) ; Configuration:: updateValue ( self :: PREFIX . $value . '_reference' , $carrier -> id ) ; } } return TRUE ; } |
When removing the module also delete the carrier.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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 -> deleteCarriers ( ) ) { return FALSE ; } return TRUE ; } return FALSE ; } |
Implementing the functions of the abstract class CarrierModule. We are not going to focus attention on any logic for dynamic calculation of the cost, but will just return the amount of 777.
public function getOrderShippingCost ( $params , $shipping_cost ) { return 777 ; } public function getOrderShippingCostExternal ( $params ) { return $this -> getOrderShippingCost ( $params , 0 ) ; } |
The hook «ActionCarrierUpdate» keeps the reference of the original carrier.
public function hookActionCarrierUpdate ( $params ) { if ( $params [ 'carrier' ] -> id_reference == Configuration:: get ( self :: PREFIX . 'swipbox_reference' ) ) { Configuration:: updateValue ( self :: PREFIX . 'swipbox' , $params [ 'carrier' ] -> id ) ; } } |
So, this is the entire module. When developing such a module the main part of the work takes to receive data via API and to develop the logic for calculating the cost of delivery.
Check out our Prestashop Pickup at Store module adds a new option to the list of shipping methods in your store.
Payment Restriction by Carrier
Take your online store to the next level with BelVG extension
Download here
Partner With Us
Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Igor
Prestashop How To Create A Module
Source: https://belvg.com/blog/how-to-create-shipping-module-for-prestashop.html
Posted by: bowlesarned1981.blogspot.com
0 Response to "Prestashop How To Create A Module"
Post a Comment