toutpratique/modules/productaccessories/productaccessories.php

101 lines
3.0 KiB
PHP
Raw Normal View History

2016-07-06 11:21:41 +02:00
<?php
if (!defined('_PS_VERSION_'))
exit;
class ProductAccessories extends Module
{
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);
}
private function prepareAccessoriesTemplateVars()
{
$cart = $this->context->cart;
$currency = $this->context->currency;
$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($this->context->lang->id, $productId['id_product'])) {
$accessories = array_merge($accessories, $accessoriesProduct);
}
}
$accessories = array_unique($accessories, SORT_REGULAR);
$randAccessories = array_rand($accessories, 3);
$showAccessories = [];
foreach ($randAccessories as $key) {
$product = new Product($accessories[$key]['id_product'], $this->context->lang->id, $this->context->shop);
$product->id_product_attribute = 0;
$row['product'] = $product;
$row['id_product'] = $product->id;
$row['id_product_attribute'] = $product->id_product_attribute;
$row['image_url'] =$this->getImage($product);
$accessory = Product::getProductProperties($this->context->lang->id, $row, $this->context);
$showAccessories[] = $accessory;
}
if(isset($showAccessories)) {
$this->context->smarty->assign(array('enable' => 1, 'label' => $label, 'showAccessories' => $showAccessories));
} else {
$this->context->smarty->assign(array('enable' => 0));
}
}
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;
}
}