Jump to content

Product Count Displays Wrong Number


xxmayankxx

Recommended Posts

One more thing, I did had to change the code to make it display product counter on the category list.

 

Here's how my blockcategories.php looks like - added an array to display products 'products' => $ProductsCount);

 

if (!defined('_PS_VERSION_'))
exit;
class BlockCategories extends Module
{
public function __construct()
{
 $this->name = 'blockcategories';
 $this->tab = 'front_office_features';
 $this->version = '2.0';
 $this->author = 'PrestaShop';
 parent::__construct();
 $this->displayName = $this->l('Categories block');
 $this->description = $this->l('Adds a block featuring product categories.');
}
public function install()
{
 if (!parent::install() ||
  !$this->registerHook('leftColumn') ||
  !$this->registerHook('footer') ||
  !$this->registerHook('header') ||
  // Temporary hooks. Do NOT hook any module on it. Some CRUD hook will replace them as soon as possible.
  !$this->registerHook('categoryAddition') ||
  !$this->registerHook('categoryUpdate') ||
  !$this->registerHook('categoryDeletion') ||
  !$this->registerHook('actionAdminMetaControllerUpdate_optionsBefore') ||
  !$this->registerHook('actionAdminLanguagesControllerStatusBefore') ||
  !Configuration::updateValue('BLOCK_CATEG_MAX_DEPTH', 4) ||
  !Configuration::updateValue('BLOCK_CATEG_DHTML', 1))
  return false;
 return true;
}
public function uninstall()
{
 if (!parent::uninstall() ||
  !Configuration::deleteByName('BLOCK_CATEG_MAX_DEPTH') ||
  !Configuration::deleteByName('BLOCK_CATEG_DHTML'))
  return false;
 return true;
}
public function getContent()
{
 $output = '<h2>'.$this->displayName.'</h2>';
 if (Tools::isSubmit('submitBlockCategories'))
 {
  $maxDepth = (int)(Tools::getValue('maxDepth'));
  $dhtml = Tools::getValue('dhtml');
  $nbrColumns = Tools::getValue('nbrColumns', 4);
  if ($maxDepth < 0)
   $output .= '<div class="alert error">'.$this->l('Maximum depth: Invalid number.').'</div>';
  elseif ($dhtml != 0 && $dhtml != 1)
   $output .= '<div class="alert error">'.$this->l('Dynamic HTML: Invalid choice.').'</div>';
  else
  {
   Configuration::updateValue('BLOCK_CATEG_MAX_DEPTH', (int)($maxDepth));
   Configuration::updateValue('BLOCK_CATEG_DHTML', (int)($dhtml));
   Configuration::updateValue('BLOCK_CATEG_NBR_COLUMN_FOOTER', $nbrColumns);
   Configuration::updateValue('BLOCK_CATEG_SORT_WAY', Tools::getValue('BLOCK_CATEG_SORT_WAY'));
   Configuration::updateValue('BLOCK_CATEG_SORT', Tools::getValue('BLOCK_CATEG_SORT'));
   $this->_clearBlockcategoriesCache();
   $output .= '<div class="conf confirm">'.$this->l('Settings updated').'</div>';
  }
 }
 return $output.$this->displayForm();
}
public function displayForm()
{
 return '
 <form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post">
  <fieldset>
   <legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Settings').'</legend>
   <label>'.$this->l('Maximum depth').'</label>
   <div class="margin-form">
 <input type="text" name="maxDepth" value="'.(int)Configuration::get('BLOCK_CATEG_MAX_DEPTH').'" />
 <p class="clear">'.$this->l('Set the maximum depth of sublevels displayed in this block (0 = infinite)').'</p>
   </div>
   <label>'.$this->l('Dynamic').'</label>
   <div class="margin-form">
 <input type="radio" name="dhtml" id="dhtml_on" value="1" '.(Tools::getValue('dhtml', Configuration::get('BLOCK_CATEG_DHTML')) ? 'checked="checked" ' : '').'/>
 <label class="t" for="dhtml_on"> <img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'" title="'.$this->l('Enabled').'" /></label>
 <input type="radio" name="dhtml" id="dhtml_off" value="0" '.(!Tools::getValue('dhtml', Configuration::get('BLOCK_CATEG_DHTML')) ? 'checked="checked" ' : '').'/>
 <label class="t" for="dhtml_off"> <img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'" title="'.$this->l('Disabled').'" /></label>
 <p class="clear">'.$this->l('Activate dynamic (animated) mode for sublevels.').'</p>
   </div>
   <label>'.$this->l('Sort').'</label>
   <div class="margin-form">
 <input type="radio" name="BLOCK_CATEG_SORT" id="sort_on" value="0" '.(!Tools::getValue('BLOCK_CATEG_SORT', Configuration::get('BLOCK_CATEG_SORT')) ? 'checked="checked" ' : '').'/>
 <label class="t" for="sort_on"> <img src="../modules/'.$this->name.'/sort_number.png" alt="'.$this->l('Enabled').'" title="'.$this->l('By position').'" />'.$this->l('By position').'</label>
 <input type="radio" name="BLOCK_CATEG_SORT" id="sort_off" value="1" '.(Tools::getValue('BLOCK_CATEG_SORT', Configuration::get('BLOCK_CATEG_SORT')) ? 'checked="checked" ' : '').'/>
 <label class="t" for="sort_off"> <img src="../modules/'.$this->name.'/sort_alphabet.png" alt="'.$this->l('Disabled').'" title="'.$this->l('By name').'" />'.$this->l('By name').'</label> -
 <select name="BLOCK_CATEG_SORT_WAY">
  <option value="0" '.(!Tools::getValue('BLOCK_CATEG_SORT_WAY', Configuration::get('BLOCK_CATEG_SORT_WAY')) ? 'selected="selected" ' : '').'>'.$this->l('Ascending').'</option>
  <option value="1" '.(Tools::getValue('BLOCK_CATEG_SORT_WAY', Configuration::get('BLOCK_CATEG_SORT_WAY')) ? 'selected="selected" ' : '').'>'.$this->l('Descending').'</option>
 </select>
   </div>
   <label>'.$this->l('How many footer columns would you like?').'</label>
   <div class="margin-form">
 <input type="text" name="nbrColumns" value="'.(int)Configuration::get('BLOCK_CATEG_NBR_COLUMN_FOOTER').'" />
 <p class="clear">'.$this->l('Define the number of footer columns.').'</p>
   </div>
   <center><input type="submit" name="submitBlockCategories" value="'.$this->l('Save').'" class="button" /></center>
  </fieldset>
 </form>';
}
public function getTree($resultParents, $resultIds, $maxDepth, $id_category = null, $currentDepth = 0)
{
 if (is_null($id_category))
  $id_category = $this->context->shop->getCategory();
 $children = array();
 if (isset($resultParents[$id_category]) && count($resultParents[$id_category]) && ($maxDepth == 0 || $currentDepth < $maxDepth))
  foreach ($resultParents[$id_category] as $subcat)
   $children[] = $this->getTree($resultParents, $resultIds, $maxDepth, $subcat['id_category'], $currentDepth + 1);
 if (!isset($resultIds[$id_category]))
  return false;

  // added the # of products per category, so we can display these in the category tree block.
// N.B. Only the really shown products in the front office are counted, no drafts)
$ProductsCount = 0;
$ProductsCount = (int)Db::getInstance()->getValue('SELECT COUNT(cp.id_category) FROM '._DB_PREFIX_.'category_product cp, '._DB_PREFIX_.'product pr WHERE cp.id_category = '.$id_category .' AND cp.id_product = pr.id_product AND pr.active = 1' );
 /*$return = array('id' => $id_category, 'link' => $this->context->link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']),
  'name' => $resultIds[$id_category]['name'], 'desc'=> $resultIds[$id_category]['description'],
  'children' => $children);*/
  $return = array('id' => $id_category, 'link' => $this->context->link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']),'name' => $resultIds[$id_category]['name'], 'desc'=> $resultIds[$id_category]['description'], 'children' => $children , 'products' => $ProductsCount);
// N.B. don't forget the comma before 'products'!
 return $return;
}
public function hookLeftColumn($params)
{
 if (!$this->isCached('blockcategories.tpl', $this->getCacheId()))
 {
  // Get all groups for this customer and concatenate them as a string: "1,2,3..."
  $groups = implode(', ', Customer::getGroupsStatic((int)$this->context->customer->id));
  $maxdepth = Configuration::get('BLOCK_CATEG_MAX_DEPTH');
  if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
   SELECT DISTINCT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite
   FROM `'._DB_PREFIX_.'category` c
   INNER JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = '.(int)$this->context->language->id.Shop::addSqlRestrictionOnLang('cl').')
   INNER JOIN `'._DB_PREFIX_.'category_shop` cs ON (cs.`id_category` = c.`id_category` AND cs.`id_shop` = '.(int)$this->context->shop->id.')
   WHERE (c.`active` = 1 OR c.`id_category` = '.(int)Configuration::get('PS_HOME_CATEGORY').')
   AND c.`id_category` != '.(int)Configuration::get('PS_ROOT_CATEGORY').'
   '.((int)$maxdepth != 0 ? ' AND `level_depth` <= '.(int)$maxdepth : '').'
   AND c.id_category IN (SELECT id_category FROM `'._DB_PREFIX_.'category_group` WHERE `id_group` IN ('.pSQL($groups).'))
   ORDER BY `level_depth` ASC, '.(Configuration::get('BLOCK_CATEG_SORT') ? 'cl.`name`' : 'cs.`position`').' '.(Configuration::get('BLOCK_CATEG_SORT_WAY') ? 'DESC' : 'ASC')))
   return;
  $resultParents = array();
  $resultIds = array();
  foreach ($result as &$row)
  {
   $resultParents[$row['id_parent']][] = &$row;
   $resultIds[$row['id_category']] = &$row;
  }
  $blockCategTree = $this->getTree($resultParents, $resultIds, Configuration::get('BLOCK_CATEG_MAX_DEPTH'));
  unset($resultParents, $resultIds);
  $id_category = (int)Tools::getValue('id_category');
  $id_product = (int)Tools::getValue('id_product');

  $isDhtml = (Configuration::get('BLOCK_CATEG_DHTML') == 1 ? true : false);
  if (Tools::isSubmit('id_category'))
  {
   $this->context->cookie->last_visited_category = $id_category;
   $this->smarty->assign('currentCategoryId', $this->context->cookie->last_visited_category);
  }
  if (Tools::isSubmit('id_product'))
  {
   if (!isset($this->context->cookie->last_visited_category)
 || !Product::idIsOnCategoryId($id_product, array('0' => array('id_category' => $this->context->cookie->last_visited_category)))
 || !Category::inShopStatic($this->context->cookie->last_visited_category, $this->context->shop))
   {
 $product = new Product($id_product);
 if (isset($product) && Validate::isLoadedObject($product))
  $this->context->cookie->last_visited_category = (int)$product->id_category_default;
   }
   $this->smarty->assign('currentCategoryId', (int)$this->context->cookie->last_visited_category);
  }
  $this->smarty->assign('blockCategTree', $blockCategTree);
  if (file_exists(_PS_THEME_DIR_.'modules/blockcategories/blockcategories.tpl'))
   $this->smarty->assign('branche_tpl_path', _PS_THEME_DIR_.'modules/blockcategories/category-tree-branch.tpl');
  else
   $this->smarty->assign('branche_tpl_path', _PS_MODULE_DIR_.'blockcategories/category-tree-branch.tpl');
  $this->smarty->assign('isDhtml', $isDhtml);
 }
 $display = $this->display(__FILE__, 'blockcategories.tpl', $this->getCacheId());
 return $display;
}
protected function getCacheId($name = null)
{
 parent::getCacheId($name);
 $groups = implode(', ', Customer::getGroupsStatic((int)$this->context->customer->id));
 $id_product = (int)Tools::getValue('id_product', 0);
 $id_category = (int)Tools::getValue('id_category', 0);
 $id_lang = (int)$this->context->language->id;
 return 'blockcategories|'.(int)Tools::usingSecureMode().'|'.$this->context->shop->id.'|'.$groups.'|'.$id_lang.'|'.$id_product.'|'.$id_category;
}
public function hookFooter($params)
{
 // Get all groups for this customer and concatenate them as a string: "1,2,3..."
 if (!$this->isCached('blockcategories_footer.tpl', $this->getCacheId()))
 {
  $maxdepth = Configuration::get('BLOCK_CATEG_MAX_DEPTH');
  $groups = implode(', ', Customer::getGroupsStatic((int)$this->context->customer->id));
  if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
   SELECT DISTINCT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite
   FROM `'._DB_PREFIX_.'category` c
   '.Shop::addSqlAssociation('category', 'c').'
   LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = '.(int)$this->context->language->id.Shop::addSqlRestrictionOnLang('cl').')
   LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cg.`id_category` = c.`id_category`)
   WHERE (c.`active` = 1 OR c.`id_category` = 1)
   '.((int)($maxdepth) != 0 ? ' AND `level_depth` <= '.(int)($maxdepth) : '').'
   AND cg.`id_group` IN ('.pSQL($groups).')
   ORDER BY `level_depth` ASC, '.(Configuration::get('BLOCK_CATEG_SORT') ? 'cl.`name`' : 'category_shop.`position`').' '.(Configuration::get('BLOCK_CATEG_SORT_WAY') ? 'DESC' : 'ASC')))
   return;
  $resultParents = array();
  $resultIds = array();
  foreach ($result as &$row)
  {
   $resultParents[$row['id_parent']][] = &$row;
   $resultIds[$row['id_category']] = &$row;
  }
  //$nbrColumns = Configuration::get('BLOCK_CATEG_NBR_COLUMNS_FOOTER');
  $nbrColumns = Configuration::get('BLOCK_CATEG_NBR_COLUMN_FOOTER');
  if (!$nbrColumns)
   $nbrColumns = 3;
  $numberColumn = abs(count($result) / $nbrColumns);
  $widthColumn = floor(100 / $nbrColumns);
  $this->smarty->assign('numberColumn', $numberColumn);
  $this->smarty->assign('widthColumn', $widthColumn);
  $blockCategTree = $this->getTree($resultParents, $resultIds, Configuration::get('BLOCK_CATEG_MAX_DEPTH'));
  unset($resultParents, $resultIds);
  $isDhtml = (Configuration::get('BLOCK_CATEG_DHTML') == 1 ? true : false);
  $id_category = (int)Tools::getValue('id_category');
  $id_product = (int)Tools::getValue('id_product');
  if (Tools::isSubmit('id_category'))
  {
   $this->context->cookie->last_visited_category = $id_category;
   $this->smarty->assign('currentCategoryId', $this->context->cookie->last_visited_category);
  }
  if (Tools::isSubmit('id_product'))
  {
   if (!isset($this->context->cookie->last_visited_category) || !Product::idIsOnCategoryId($id_product, array('0' => array('id_category' => $this->context->cookie->last_visited_category))))
   {
 $product = new Product($id_product);
 if (isset($product) && Validate::isLoadedObject($product))
  $this->context->cookie->last_visited_category = (int)($product->id_category_default);
   }
   $this->smarty->assign('currentCategoryId', (int)($this->context->cookie->last_visited_category));
  }
  $this->smarty->assign('blockCategTree', $blockCategTree);
  if (file_exists(_PS_THEME_DIR_.'modules/blockcategories/blockcategories_footer.tpl'))
   $this->smarty->assign('branche_tpl_path', _PS_THEME_DIR_.'modules/blockcategories/category-tree-branch.tpl');
  else
   $this->smarty->assign('branche_tpl_path', _PS_MODULE_DIR_.'blockcategories/category-tree-branch.tpl');
  $this->smarty->assign('isDhtml', $isDhtml);
 }
 $display = $this->display(__FILE__, 'blockcategories_footer.tpl', $this->getCacheId());
 return $display;
}
public function hookRightColumn($params)
{
 return $this->hookLeftColumn($params);
}
public function hookHeader()
{
 $this->context->controller->addJS(_THEME_JS_DIR_.'tools/treeManagement.js');
 $this->context->controller->addCSS(($this->_path).'blockcategories.css', 'all');
}
private function _clearBlockcategoriesCache()
{
 $this->_clearCache('blockcategories.tpl');
 $this->_clearCache('blockcategories_footer.tpl');
}
public function hookCategoryAddition($params)
{
 $this->_clearBlockcategoriesCache();
}
public function hookCategoryUpdate($params)
{
 $this->_clearBlockcategoriesCache();
}
public function hookCategoryDeletion($params)
{
 $this->_clearBlockcategoriesCache();
}
public function hookActionAdminMetaControllerUpdate_optionsBefore($params)
{
 $this->_clearBlockcategoriesCache();
}
}

 

Added the category-tree-branch.tpl

<li {if isset($last) && $last == 'true'}class="last"{/if}>
<a href="{$node.link|escape:'htmlall':'UTF-8'}" {if isset($currentCategoryId) && $node.id == $currentCategoryId}class="selected"{/if} title="{$node.desc|escape:'htmlall':'UTF-8'}">{$node.name|escape:'htmlall':'UTF-8'}({$node.products})</a>
{if $node.children|@count > 0}
 <ul>
 {foreach from=$node.children item=child name=categoryTreeBranch}
  {if $smarty.foreach.categoryTreeBranch.last}
   {include file="$branche_tpl_path" node=$child last='true'}
  {else}
   {include file="$branche_tpl_path" node=$child last='false'}
  {/if}
 {/foreach}
 </ul>
{/if}
</li>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...