Skip to content

Instantly share code, notes, and snippets.

@alissonbovenzo
Created July 22, 2017 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alissonbovenzo/648d4edc050506f257654beed53343de to your computer and use it in GitHub Desktop.
Save alissonbovenzo/648d4edc050506f257654beed53343de to your computer and use it in GitHub Desktop.
<?php
if (!defined('_PS_VERSION_'))
{
exit;
}
class MeuModulo extends CarrierModule {
const PREFIX = "pinkbit_correios";
protected $_hooks = array(
'actionCarrierUpdate',
// 'leftColumn',
// 'header'
);
protected $_carriers = array(
"Sedex" => "sedex",
"Sedex 10" =>"sedex10"
);
public function __construct(){
$this->name = 'PinkBIT Correios';
$this->tab = 'front_office_features';
$this->version = '0.1.0';
$this->author = 'PinkBIT';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min'=> '1.7.0', 'min' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Modulo de integração com os correios');
$this->description = $this->l('Este módulo integra perfeitamente sua plataforma com os correios');
$this->confirmUninstall = $this->l('Você tem certeza que deseja desinstalar este modulo ?');
if(! Configuration::get('MYMODULE_NAME'))
$this->warning = $this->l('No name provided');
}
public function install() {
//sei la
p(22);
if(Shop::isFeatureActive()){
Shop::setContext(Shop::CONTEXT_ALL);
}
//Caso não consiga instalar o modulo ou setar o nome, falha a instalação
if(!parent::install() || !Configuration::updateValue('MYMODULE_NAME', 'Correios')) {
return false;
}
//Registra todos os Hooks
foreach($this->_hooks AS $hook){
if(!$this->registerHook($hook)) return false;
}
p(1);
//Tenta criar os carriers
if(!$this->createCarriers()) return false;
return true;
}
public function uninstall(){
if(!parent::uninstall() ||
!Configuration::deleteByName('MYMODULE_NAME')
){
return false;
}
//removemos os hooks
foreach($this->_hooks AS $hook){
if(!$this->unregisterHook($hook)){
return false;
}
}
// //removemos os carrieers
// if(!$this->deleteCarriers()){
// return false;
// }
return true;
}
protected function createCarriers() {
foreach( $this->_carriers AS $key => $value) {
//Criando a transportadora
$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 = $this->name;
$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) $carrier['id_group']
),'INSERT');
}
//Range de valores
$rangePrice = new RangePrice();
$rangePrice->id_carrier = $carrier->id;
$rangePrice->delimiter1 = '0';
$rangePrice->delimiter2 = '100000';
$rangePrice->add();
//Range de Pesos
$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'=> (int) $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'=> (int) $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;
}
}
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 getOrderShippingCost($params, $shipping_cost){
return 777;
}
public function getOrderShippingCostExternal($params){
return $this->getOrderShippingCost($params, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment