Skip to content

Instantly share code, notes, and snippets.

@mbaric
Created June 20, 2017 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbaric/022db6bc969512c2f966c301b8d9c015 to your computer and use it in GitHub Desktop.
Save mbaric/022db6bc969512c2f966c301b8d9c015 to your computer and use it in GitHub Desktop.
Joomla 3.5 virutemart VirtueMart 3.2.2 - Count Products in Categories and SubCategories
<?php
/**
*
* Shows the products/categories of a category
*
* @package VirtueMart
* @subpackage
* @author Max Milbers
* @link ${PHING.VM.MAINTAINERURL}
* @copyright Copyright (c) 2004 - 2014 VirtueMart Team. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* @version $Id: default.php 6104 2012-06-13 14:15:29Z alatak $
*/
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');
$categories = $viewData['categories'];
if ($categories) {
$categories_per_row = !empty($viewData['categories_per_row'])? $viewData['categories_per_row']:VmConfig::get ( 'categories_per_row', 3 );
if(empty($categories_per_row)) $categories_per_row = 3;
// Category and Columns Counter
$iCol = 1;
$iCategory = 1;
// Calculating Categories Per Row
$category_cellwidth = ' width'.floor ( 100 / $categories_per_row );
// Separator
$verticalseparator = " vertical-separator";
?>
<div class="category-view">
<?php
// Start the Output
foreach ( $categories as $category ) {
// Show the horizontal seperator
if ($iCol == 1 && $iCategory > $categories_per_row) { ?>
<div class="horizontal-separator"></div>
<?php }
// this is an indicator wether a row needs to be opened or not
if ($iCol == 1) { ?>
<div class="row">
<?php }
// Show the vertical separator
if ($iCategory == $categories_per_row or $iCategory % $categories_per_row == 0) {
$show_vertical_separator = ' ';
} else {
$show_vertical_separator = $verticalseparator;
}
// Category Link
$caturl = JRoute::_ ( 'index.php?option=com_virtuemart&view=category&virtuemart_category_id=' . $category->virtuemart_category_id , FALSE);
// Show Category ?>
<div class="category floatleft<?php echo $category_cellwidth . $show_vertical_separator ?>">
<div class="spacer">
<h2>
<a href="<?php echo $caturl ?>" title="<?php echo vmText::_($category->category_name) ?>">
<?php echo vmText::_($category->category_name); ?>
<br />
<?php // if ($category->ids) {
echo $category->images[0]->displayMediaThumb("",false);
//} ?>
</a>
<br>
<p id="kategorija_broj">(<?php
//M.Baric 20.06.2017 Determine if we are in category or subcategory
$db = JFactory::getDbo();
$mainResults = getCategoryChildren($db, $category->virtuemart_category_id);
if (!empty($mainResults[0]->category_child_id)) {
$subCategories = processSubCategories($mainResults);
/*If we only have one subcategory count and print - Albanija*/
$subCategorisArray = explode(",", $subCategories);
if (count($subCategorisArray) == 1) {
$ct = getCountSubCategory($db, $subCategories);
echo $ct;
} else {
/*If we more subcategories process and print - Andora*/
$subResults2 = getCategoryChildren($db, $subCategories);
if (!empty($subResults2)) {
$subCategories = processSubCategories($subResults2);
}
$ct = getCountSubCategory($db, $subCategories);
echo $ct;
}
} else {
$ct = getCountSubCategory($db, $category->virtuemart_category_id);
echo $ct;
}
?>)</p>
</h2>
</div>
</div>
<?php
$iCategory ++;
// Do we need to close the current row now?
if ($iCol == $categories_per_row) { ?>
<div class="clear"></div>
</div>
<?php
$iCol = 1;
} else {
$iCol ++;
}
}
// Do we need a final closing row tag?
if ($iCol != 1) { ?>
<div class="clear"></div>
</div>
<?php
}
?></div><?php
}
function getCategoryChildren($db, $subCategory)
{
$query = $db->getQuery(true);
$query->select($db->quoteName(array('category_child_id')));
$query->from($db->quoteName('#__virtuemart_category_categories'));
$query->where($db->quoteName('category_parent_id') . ' IN ('. $subCategory .')');
$db->setQuery($query);
$mainResults = $db->loadObjectList();
return $mainResults;
}
function getCountSubCategory($db, $subCategories)
{
$query = $db->getQuery(true);
$query->select('count(0) ct');
$query->from($db->quoteName('#__virtuemart_product_categories'));
$query->where($db->quoteName('virtuemart_category_id') . ' IN (' . $subCategories .')');
$db->setQuery($query);
$subResults = $db->loadObjectList();
return $subResults[0]->ct;
}
function processSubCategories($mainResults)
{
$subCategories = '';
foreach ($mainResults as $item) {
$subCategories .= $item->category_child_id . ',';
}
$subCategories = rtrim($subCategories, ',');
return $subCategories;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment