* @copyright 2007-2014 PrestaShop SA * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_PS_VERSION_')) exit; class StatsCatalog extends Module { private $join = ''; private $where = ''; public function __construct() { $this->name = 'statscatalog'; $this->tab = 'analytics_stats'; $this->version = '1.2.3'; $this->author = 'PrestaShop'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Catalog statistics'); $this->description = $this->l('Adds a tab containing general statistics about your catalog to the Stats dashboard.'); } public function install() { return (parent::install() && $this->registerHook('AdminStatsModules')); } public function getQuery1() { $sql = 'SELECT COUNT(DISTINCT p.`id_product`) AS total, SUM(product_shop.`price`) / COUNT(product_shop.`price`) AS average_price, COUNT(DISTINCT i.`id_image`) AS images FROM `'._DB_PREFIX_.'product` p '.Shop::addSqlAssociation('product', 'p').' LEFT JOIN `'._DB_PREFIX_.'image` i ON i.`id_product` = p.`id_product` '.$this->join.' WHERE product_shop.`active` = 1 '.$this->where; return DB::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql); } public function getTotalPageViewed() { return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT SUM(pv.`counter`) FROM `'._DB_PREFIX_.'product` p '.Shop::addSqlAssociation('product', 'p').' LEFT JOIN `'._DB_PREFIX_.'page` pa ON p.`id_product` = pa.`id_object` LEFT JOIN `'._DB_PREFIX_.'page_type` pt ON (pt.`id_page_type` = pa.`id_page_type` AND pt.`name` IN ("product.php", "product")) LEFT JOIN `'._DB_PREFIX_.'page_viewed` pv ON pv.`id_page` = pa.`id_page` '.$this->join.' WHERE product_shop.`active` = 1 '.$this->where); } public function getTotalProductViewed() { return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT COUNT(DISTINCT pa.`id_object`) FROM `'._DB_PREFIX_.'page_viewed` pv LEFT JOIN `'._DB_PREFIX_.'page` pa ON pv.`id_page` = pa.`id_page` LEFT JOIN `'._DB_PREFIX_.'page_type` pt ON pt.`id_page_type` = pa.`id_page_type` LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = pa.`id_object` '.Shop::addSqlAssociation('product', 'p').' '.$this->join.' WHERE pt.`name` IN ("product.php", "product") AND product_shop.`active` = 1 '.$this->where); } public function getTotalBought() { return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT SUM(od.`product_quantity`) FROM `'._DB_PREFIX_.'orders` o LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON o.`id_order` = od.`id_order` LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = od.`product_id` '.$this->join.' WHERE o.valid = 1 '.$this->where); } public function getProductsNB($id_lang) { $sql = 'SELECT p.`id_product` FROM `'._DB_PREFIX_.'orders` o LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON o.`id_order` = od.`id_order` LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = od.`product_id` '.Shop::addSqlAssociation('product', 'p').' '.$this->join.' WHERE o.valid = 1 '.$this->where.' AND product_shop.`active` = 1 GROUP BY p.`id_product`'; $precalc = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); $precalc2 = array(); foreach ($precalc as $array) $precalc2[] = (int)$array['id_product']; $sql = 'SELECT p.id_product, pl.name, pl.link_rewrite FROM `'._DB_PREFIX_.'product` p '.Shop::addSqlAssociation('product', 'p').' LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = p.`id_product` AND pl.id_lang = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').') '.$this->join.' WHERE product_shop.`active` = 1 '.(count($precalc2) ? 'AND p.`id_product` NOT IN ('.implode(',', $precalc2).')' : '').' '.$this->where; $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); return array('total' => Db::getInstance(_PS_USE_SQL_SLAVE_)->NumRows(), 'result' => $result); } public function hookAdminStatsModules($params) { $categories = Category::getCategories($this->context->language->id, true, false); $product_token = Tools::getAdminToken('AdminProducts'.(int)Tab::getIdFromClassName('AdminProducts').(int)$this->context->employee->id); $irow = 0; if ($id_category = (int)Tools::getValue('id_category')) { $this->join = ' LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_product` = p.`id_product`)'; $this->where = ' AND cp.`id_category` = '.$id_category; } $result1 = $this->getQuery1(true); $total = $result1['total']; $average_price = $result1['average_price']; $total_pictures = $result1['images']; $average_pictures = $total ? $total_pictures / $total : 0; $never_bought = $this->getProductsNB($this->context->language->id); $total_nb = $never_bought['total']; $products_nb = $never_bought['result']; $total_bought = $this->getTotalBought(); $average_purchase = $total ? ($total_bought / $total) : 0; $total_page_viewed = $this->getTotalPageViewed(); $average_viewed = $total ? ($total_page_viewed / $total) : 0; $conversion = number_format((float)($total_page_viewed ? ($total_bought / $total_page_viewed) : 0), 2, '.', ''); if ($conversion_reverse = number_format((float)($total_bought ? ($total_page_viewed / $total_bought) : 0), 2, '.', '')) $conversion .= sprintf($this->l('(1 purchase / %d visits)'), $conversion_reverse); $total_nv = $total - $this->getTotalProductViewed(); $html = '
'.$this->l('Defines the average conversion rate for the product page. It is possible to purchase a product without viewing the product page, so this rate can be greater than 1.').'
'.$this->l('ID').' | '.$this->l('Name').' | '.$this->l('Edit / View').' |
---|---|---|
'.$product['id_product'].' | '.$product['name'].' |