toutpratique/modules/productaccessories/productaccessories.php
2016-07-20 11:14:56 +02:00

116 lines
3.8 KiB
PHP

<?php
if (!defined('_PS_VERSION_'))
exit;
class ProductAccessories extends Module
{
const FEATURE_ID = 8;
public function __construct()
{
$this->name = 'productaccessories';
$this->tab = 'front_office_features';
$this->author = 'Antadis';
$this->version = '1.0.0';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.5', 'max' => _PS_VERSION_);
parent::__construct();
$this->displayName = $this->l('Product accessories');
$this->description = $this->l('Show Product accessories on basket page');
}
public function install()
{
if (parent::install() == false)
return false;
return true;
}
public function uninstall()
{
if (!parent::uninstall())
return false;
return true;
}
public function hookDisplayShoppingCart(){
return $this->displayAccessories('views/templates/accessories.tpl');
}
public function displayAccessories($template)
{
$this->prepareAccessoriesTemplateVars();
return $this->display(__FILE__, $template);
}
public static function getFrontFeaturesStatic($idLang, $idProduct, $idFeature)
{
return Db::getInstance()->getRow('SELECT name, value, pf.id_feature
FROM '._DB_PREFIX_.'feature_product pf
LEFT JOIN '._DB_PREFIX_.'feature_lang fl ON (fl.id_feature = pf.id_feature AND fl.id_lang = '.(int)$idLang.')
LEFT JOIN '._DB_PREFIX_.'feature_value_lang fvl ON (fvl.id_feature_value = pf.id_feature_value AND fvl.id_lang = '.(int)$idLang.')
LEFT JOIN '._DB_PREFIX_.'feature f ON (f.id_feature = pf.id_feature AND fl.id_lang = '.(int)$idLang.')
'.Shop::addSqlAssociation('feature', 'f').'
WHERE pf.id_product = '.(int)$idProduct.'
AND pf.id_feature = '. (int)$idFeature . ';');
}
private function prepareAccessoriesTemplateVars()
{
$context = Context::getContext();
$cart = $context->cart;
$label = 'Vous aimerez peut être aussi';
$products = $cart->getProducts();
$productsId = array_column($products, 'id_product');
$accessories = array();
foreach ($productsId as $productId) {
if($accessoriesProduct = Product::getAccessoriesLight($context->language->id, $productId['id_product'])) {
$accessories = array_merge($accessories, $accessoriesProduct);
}
}
$accessories = array_unique($accessories, SORT_REGULAR);
shuffle($accessories);
$randAccessories = array_slice($accessories, 0, 3);
$showAccessories = [];
foreach ($randAccessories as $key => $randAccessory) {
$product = new Product($accessories[$key]['id_product'], $context->language->id, $context->shop);
$product->id_product_attribute = 0;
$row['product'] = $product;
$row['id_product'] = $product->id;
$row['out_of_stock'] = $product->out_of_stock;
$row['id_product_attribute'] = $product->id_product_attribute;
$row['image_url'] =$this->getImage($product);
$row['features'] = self::getFrontFeaturesStatic($context->language->id, $product->id, self::FEATURE_ID);
$accessory = Product::getProductProperties($context->language->id, $row, $context);
$showAccessories[] = $accessory;
}
if(isset($showAccessories)) {
$context->smarty->assign(array('label' => $label, 'showAccessories' => $showAccessories));
}
}
public function getImage(Product $product)
{
global $link;
$image = ImageCore::getCover($product->id);
$image_url = $link->getImageLink($product->link_rewrite, $image['id_image'], 'small_default');
return $image_url;
}
}