toutpratique/modules/productaccessories/productaccessories.php

116 lines
3.8 KiB
PHP
Raw Normal View History

2016-07-06 11:21:41 +02:00
<?php
if (!defined('_PS_VERSION_'))
exit;
class ProductAccessories extends Module
{
2016-07-06 17:36:24 +02:00
const FEATURE_ID = 8;
2016-07-06 17:23:42 +02:00
2016-07-06 11:21:41 +02:00
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);
}
2016-07-06 17:23:42 +02:00
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 . ';');
}
2016-07-06 11:21:41 +02:00
private function prepareAccessoriesTemplateVars()
{
2016-07-06 17:23:42 +02:00
$context = Context::getContext();
$cart = $context->cart;
2016-07-06 11:21:41 +02:00
$label = 'Vous aimerez peut être aussi';
$products = $cart->getProducts();
2016-07-20 11:14:56 +02:00
2016-07-06 11:21:41 +02:00
$productsId = array_column($products, 'id_product');
$accessories = array();
foreach ($productsId as $productId) {
2016-07-06 17:23:42 +02:00
if($accessoriesProduct = Product::getAccessoriesLight($context->language->id, $productId['id_product'])) {
2016-07-06 11:21:41 +02:00
$accessories = array_merge($accessories, $accessoriesProduct);
}
}
$accessories = array_unique($accessories, SORT_REGULAR);
2016-07-20 11:14:56 +02:00
shuffle($accessories);
$randAccessories = array_slice($accessories, 0, 3);
2016-07-06 11:21:41 +02:00
$showAccessories = [];
2016-07-20 11:14:56 +02:00
foreach ($randAccessories as $key => $randAccessory) {
2016-07-06 17:23:42 +02:00
$product = new Product($accessories[$key]['id_product'], $context->language->id, $context->shop);
2016-07-06 11:21:41 +02:00
$product->id_product_attribute = 0;
$row['product'] = $product;
$row['id_product'] = $product->id;
2016-07-06 17:36:24 +02:00
$row['out_of_stock'] = $product->out_of_stock;
2016-07-06 11:21:41 +02:00
$row['id_product_attribute'] = $product->id_product_attribute;
$row['image_url'] =$this->getImage($product);
2016-07-06 17:23:42 +02:00
$row['features'] = self::getFrontFeaturesStatic($context->language->id, $product->id, self::FEATURE_ID);
2016-07-06 11:21:41 +02:00
2016-07-06 17:23:42 +02:00
$accessory = Product::getProductProperties($context->language->id, $row, $context);
2016-07-06 11:21:41 +02:00
$showAccessories[] = $accessory;
}
if(isset($showAccessories)) {
2016-07-20 10:14:12 +02:00
$context->smarty->assign(array('label' => $label, 'showAccessories' => $showAccessories));
2016-07-06 11:21:41 +02:00
}
}
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;
}
}