toutpratique/modules/antadis_cross_selling/antadis_cross_selling.php
2016-11-10 10:42:45 +01:00

103 lines
3.3 KiB
PHP

<?php
if (!defined('_CAN_LOAD_FILES_'))
exit;
class Antadis_Cross_Selling extends Module
{
public function __construct()
{
$this->name = 'antadis_cross_selling';
$this->tab = 'administration';
$this->version = '1.0';
$this->author = 'Antadis';
parent::__construct();
$this->displayName = $this->l('Antadis Cross Selling');
$this->description = $this->l('Ajouter un élément au panier.');
$this->secure_key = Tools::encrypt($this->name);
$this->confirmUninstall = $this->l('Are you sure ?');
}
public function install()
{
if (!parent::install())
return false;
$sql = 'SELECT `id_tab` FROM `'._DB_PREFIX_.'tab` WHERE `id_parent` = 0 AND `class_name` = \'AdminCmsPs\'';
$id_tab_parent = Db::getInstance()->getValue($sql);
if (!$id_tab_parent) {
return false;
}
$new_tab = new Tab();
$new_tab->class_name = 'AdminCrossSelling';
$new_tab->id_parent = (int)$id_tab_parent;
$new_tab->module = $this->name;
// set tab name in all languages
$languages = DB::getInstance()->executeS(
'SELECT id_lang, iso_code FROM `' . _DB_PREFIX_ . 'lang`'
);
foreach ($languages as $value) {
$new_tab->name[$value['id_lang']] = ($value['iso_code'] == 'fr') ? 'Panier : proposer un produit' : 'Suggest product in cart';
}
$result = $new_tab->add();
Configuration::updateValue('CROSS_SELLING_PRODUCT_ID', 34);
Configuration::updateValue('CROSS_SELLING_PRODUCT_NAME', Product::getProductName(34));
return $result && $this->registerHook('displayProductLineShoppingCart');
}
public function uninstall()
{
$idTab = Tab::getIdFromClassName('AdminCrossSelling');
if ($idTab) {
$tab = new Tab($idTab);
$tab->delete();
}
Configuration::deleteByName('CROSS_SELLING_PRODUCT_ID');
Configuration::deleteByName('CROSS_SELLING_PRODUCT_NAME');
return parent::uninstall();
}
public function hookdisplayProductLineShoppingCart($params)
{
if(!empty($id_product = Configuration::get('CROSS_SELLING_PRODUCT_ID')))
{
$cart = Context::getContext()->cart;
$product = new Product($id_product, true, Context::getContext()->language->id);
if (Validate::isLoadedObject($product)) {
$image = Image::getCover($product->id);
$product->id_image = $image['id_image'];
$product->price = $product->getPrice(true);
$product->price_without_reduction = Product::getPriceStatic(
(int)$product->id,
true,
(isset($product->id_product_attribute) ? $product->id_product_attribute : null),
6,
null,
false,
false
);
if (empty($cart->containsProduct($product->id))) {
$this->context->smarty->assign(
array(
'product' => $product
)
);
return $this->display(__FILE__, 'cartCrossSelling.tpl');
}
}
}
}
}