/**
  * Display ajax content (this function is called instead of classic display, in ajax mode)
  */
 public function displayAjax()
 {
     // Add or remove product with Ajax
     if (Tools::getValue('ajax') && Tools::getValue('id_product') && Tools::getValue('action')) {
         if (Tools::getValue('action') == 'add') {
             $id_compare = isset($this->context->cookie->id_compare) ? $this->context->cookie->id_compare : false;
             if (CompareProduct::getNumberProducts($id_compare) < Configuration::get('PS_COMPARATOR_MAX_ITEM')) {
                 CompareProduct::addCompareProduct($id_compare, (int) Tools::getValue('id_product'));
             } else {
                 die('0');
             }
         } else {
             if (Tools::getValue('action') == 'remove') {
                 if (isset(self::$cookie->id_compare)) {
                     CompareProduct::removeCompareProduct((int) $this->context->cookie->id_compare, (int) Tools::getValue('id_product'));
                 } else {
                     die('0');
                 }
             } else {
                 die('0');
             }
         }
         die('1');
     }
 }
 public function preProcess()
 {
     parent::preProcess();
     //Add or remove product with Ajax
     if (Tools::getValue('ajax') and Tools::getValue('id_product') and Tools::getValue('action')) {
         if (Tools::getValue('action') == 'add') {
             $id_compare = isset(self::$cookie->id_compare) ? self::$cookie->id_compare : false;
             if (CompareProduct::getNumberProducts($id_compare) < Configuration::get('PS_COMPARATOR_MAX_ITEM')) {
                 CompareProduct::addCompareProduct($id_compare, (int) Tools::getValue('id_product'));
             } else {
                 die('0');
             }
         } elseif (Tools::getValue('action') == 'remove') {
             if (isset(self::$cookie->id_compare)) {
                 CompareProduct::removeCompareProduct((int) self::$cookie->id_compare, (int) Tools::getValue('id_product'));
             } else {
                 die('0');
             }
         } else {
             die('0');
         }
         die('1');
     }
 }
 /**
  * Assign template vars related to page content
  * @see FrontController::initContent()
  */
 public function initContent()
 {
     parent::initContent();
     //Clean compare product table
     CompareProduct::cleanCompareProducts('week');
     $hasProduct = false;
     if (!Configuration::get('PS_COMPARATOR_MAX_ITEM')) {
         return Tools::redirect('index.php?controller=404');
     }
     if (($product_list = Tools::getValue('compare_product_list')) && ($postProducts = isset($product_list) ? rtrim($product_list, '|') : '')) {
         $ids = array_unique(explode('|', $postProducts));
     } else {
         if (isset($this->context->cookie->id_compare)) {
             $ids = CompareProduct::getCompareProducts($this->context->cookie->id_compare);
         } else {
             $ids = null;
         }
     }
     if ($ids) {
         if (count($ids) > 0) {
             if (count($ids) > Configuration::get('PS_COMPARATOR_MAX_ITEM')) {
                 $ids = array_slice($ids, 0, Configuration::get('PS_COMPARATOR_MAX_ITEM'));
             }
             $listProducts = array();
             $listFeatures = array();
             foreach ($ids as $k => &$id) {
                 $curProduct = new Product((int) $id, true, $this->context->language->id);
                 if (!Validate::isLoadedObject($curProduct) || !$curProduct->active || !$curProduct->isAssociatedToShop()) {
                     if (isset($this->context->cookie->id_compare)) {
                         CompareProduct::removeCompareProduct($this->context->cookie->id_compare, $id);
                     }
                     unset($ids[$k]);
                     continue;
                 }
                 foreach ($curProduct->getFrontFeatures($this->context->language->id) as $feature) {
                     $listFeatures[$curProduct->id][$feature['id_feature']] = $feature['value'];
                 }
                 $cover = Product::getCover((int) $id);
                 $curProduct->id_image = Tools::htmlentitiesUTF8(Product::defineProductImage(array('id_image' => $cover['id_image'], 'id_product' => $id), $this->context->language->id));
                 $curProduct->allow_oosp = Product::isAvailableWhenOutOfStock($curProduct->out_of_stock);
                 $listProducts[] = $curProduct;
             }
             if (count($listProducts) > 0) {
                 $width = 80 / count($listProducts);
                 $hasProduct = true;
                 $ordered_features = Feature::getFeaturesForComparison($ids, $this->context->language->id);
                 $this->context->smarty->assign(array('ordered_features' => $ordered_features, 'product_features' => $listFeatures, 'products' => $listProducts, 'width' => $width, 'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));
                 $this->context->smarty->assign('HOOK_EXTRA_PRODUCT_COMPARISON', Hook::exec('displayProductComparison', array('list_ids_product' => $ids)));
             } else {
                 if (isset($this->context->cookie->id_compare)) {
                     $object = new CompareProduct((int) $this->context->cookie->id_compare);
                     if (Validate::isLoadedObject($object)) {
                         $object->delete();
                     }
                 }
             }
         }
     }
     $this->context->smarty->assign('hasProduct', $hasProduct);
     $this->setTemplate(_PS_THEME_DIR_ . 'products-comparison.tpl');
 }