
Andrew R
Members-
Posts
12 -
Joined
-
Last visited
Profile Information
-
Activity
Developer
Recent Profile Visitors
1,600,478 profile views
Andrew R's Achievements
Newbie (1/14)
1
Reputation
-
[SOLVED] Show number of products next to categories
Andrew R replied to DylzEn's topic in Core developers
Have you made sure your cache directory is writeable? It's in tools/smarty_v2/cache. You can empty both cache and compile folders, make sure they are writeable using chmod 777 in a terminal. It will generate the files again once you visit pages and they should be new versions of the files. The cache isn't all that important really. Servers are fast enough these days to run through PHP code. The compilation step is the bigger slow down. -
[SOLVED] Show number of products next to categories
Andrew R replied to DylzEn's topic in Core developers
The cache option should always be enabled but after you've made a change to your code, it has to recompiled to the cache so you would turn on force recompile, visit the page you changed to update the cache and then disable force recompile. -
[SOLVED] Show number of products next to categories
Andrew R replied to DylzEn's topic in Core developers
I made a typo up above. The line in getTree that says: $this->recursive_categories = array((int)($this->id)); should have been: $this->recursive_categories = array((int)($id_category)); -
[SOLVED] Show number of products next to categories
Andrew R replied to DylzEn's topic in Core developers
Try changing the database query for $productsCount to: $ProductsCount = (int)Db::getInstance()->getValue(' SELECT COUNT(DISTINCT cp.`id_product`) AS total FROM `'._DB_PREFIX_.'product` p LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON p.`id_product` = cp.`id_product` WHERE cp.`id_category` IN ('.implode(',',$combined_categories).') AND p.`active` = 1'); -
[SOLVED] Show number of products next to categories
Andrew R replied to DylzEn's topic in Core developers
Try changing the following line in getTree: $this->recursive_categories[] = (int)($id_category); to this: $this->recursive_categories = array((int)($this->id)); -
It looks like it's working ok for descending but not ascending. What is the code you pasted in product_sort.tpl for the ascending option? It should be: <option value="{$link->addSortDetails($request, 'id_product', 'asc')|escape:'htmlall':'UTF-8'}" {if $orderby eq 'id_product' AND $orderway eq 'asc'}selected="selected"{/if}>{l s='Product ID: low to high'}</option> <option value="{$link->addSortDetails($request, 'id_product', 'desc')|escape:'htmlall':'UTF-8'}" {if $orderby eq 'id_product' AND $orderway eq 'desc'}selected="selected"{/if}>{l s='Product ID: high to low'}</option>
-
[SOLVED] Show number of products next to categories
Andrew R replied to DylzEn's topic in Core developers
In that case, you'd paste the following two functions above getTree in blockcategories.php: public function getSubCategoriesOfCategoryID($category_id, $id_lang=1, $active = true) { if (!Validate::isBool($active)) die(Tools::displayError()); $groups = FrontController::getCurrentCustomerGroups(); $sqlGroups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1'); $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT c.*, cl.id_lang, cl.name, cl.description, cl.link_rewrite, cl.meta_title, cl.meta_keywords, cl.meta_description FROM `'._DB_PREFIX_.'category` c LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND `id_lang` = '.(int)($id_lang).') LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cg.`id_category` = c.`id_category`) WHERE `id_parent` = '.(int)($category_id).' '.($active ? 'AND `active` = 1' : '').' AND cg.`id_group` '.$sqlGroups.' GROUP BY c.`id_category` ORDER BY `level_depth` ASC, c.`position` ASC'); foreach ($result AS &$row) { $row['id_image'] = (file_exists(_PS_CAT_IMG_DIR_.$row['id_category'].'.jpg')) ? (int)($row['id_category']) : Language::getIsoById($id_lang).'-default'; $row['legend'] = 'no picture'; } return $result; } public function getSubCategoriesRecursive($category_id){ $subcategory_result = $this->getSubCategoriesOfCategoryID((int)($category_id)); if(sizeof($subcategory_result)>0){ foreach($subcategory_result as $subcat){ $this->recursive_categories[] = $subcat['id_category']; $this->getSubCategoriesRecursive($subcat['id_category']); } } return false; } and in the same file, paste the following line just before __construct: public $recursive_categories = array(); Then inside getTree, comment out the following: $ProductsCount = (int)Db::getInstance()->getValue('SELECT COUNT(*) FROM '._DB_PREFIX_.'category_product WHERE id_category = '. $id_category); and paste the following after it: $this->recursive_categories[] = (int)($id_category); $this->getSubCategoriesRecursive((int)($id_category)); $combined_categories = $this->recursive_categories; $ProductsCount = (int)Db::getInstance()->getValue(' SELECT COUNT(DISTINCT cp.`id_product`) AS total FROM `'._DB_PREFIX_.'product` p LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON p.`id_product` = cp.`id_product` WHERE cp.`id_category` IN ('.implode(',',$combined_categories).')'.($active ? ' AND p.`active` = 1' : '')); -
[SOLVED] Show number of products next to categories
Andrew R replied to DylzEn's topic in Core developers
You'd be best putting some of the above code near the function that generates your tree, which might not require overriding Category.php but that is what I meant with the override - putting the class in the override folder. What code are you using to generate the following?: - Apple Products (0) // main category - iPod (0) // subcategory - iPod Nano (1) //sub-subcategory - iPhone (0) // subcategory - iPhone 4 (1) //sub-subcategory - iPhone 5 (0) //sub-subcategory Is that just generated from a standard Prestashop .tpl file? -
You need to add an extra item in Tools.php > getProductsOrder(). For some reason, they missed out id_product in the array. Just change: $list = array(0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity'); to the following: $list = array(0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity', 7=>'id_product'); Your extra option button would be something like: <option value="{$link->addSortDetails($request, 'id_product', 'asc')|escape:'htmlall':'UTF-8'}" {if $orderby eq 'id_product' AND $orderway eq 'asc'}selected="selected"{/if}>{l s='Product ID: low to high'}</option>
-
[SOLVED] Show number of products next to categories
Andrew R replied to DylzEn's topic in Core developers
Override the class Category.php and put some of the following code in the file: class Category extends CategoryCore{ public $recursive_categories = array(); public function getSubCategoriesOfCategoryID($category_id, $id_lang=1, $active = true) { if (!Validate::isBool($active)) die(Tools::displayError()); $groups = FrontController::getCurrentCustomerGroups(); $sqlGroups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1'); $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT c.*, cl.id_lang, cl.name, cl.description, cl.link_rewrite, cl.meta_title, cl.meta_keywords, cl.meta_description FROM `'._DB_PREFIX_.'category` c LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND `id_lang` = '.(int)($id_lang).') LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cg.`id_category` = c.`id_category`) WHERE `id_parent` = '.(int)($category_id).' '.($active ? 'AND `active` = 1' : '').' AND cg.`id_group` '.$sqlGroups.' GROUP BY c.`id_category` ORDER BY `level_depth` ASC, c.`position` ASC'); foreach ($result AS &$row) { $row['id_image'] = (file_exists(_PS_CAT_IMG_DIR_.$row['id_category'].'.jpg')) ? (int)($row['id_category']) : Language::getIsoById($id_lang).'-default'; $row['legend'] = 'no picture'; } return $result; } public function getSubCategoriesRecursive($category_id){ $subcategory_result = $this->getSubCategoriesOfCategoryID((int)($category_id)); if(sizeof($subcategory_result)>0){ foreach($subcategory_result as $subcat){ $this->recursive_categories[] = $subcat['id_category']; $this->getSubCategoriesRecursive($subcat['id_category']); } } return false; } public function getProducts($id_lang, $p, $n, $orderBy = NULL, $orderWay = NULL, $getTotal = false, $active = true, $random = false, $randomNumberProducts = 1, $checkAccess = true) { global $cookie; if (!$checkAccess OR !$this->checkAccess($cookie->id_customer)) return false; if ($p < 1) $p = 1; if (empty($orderBy)) $orderBy = 'position'; else /* Fix for all modules which are now using lowercase values for 'orderBy' parameter */ $orderBy = strtolower($orderBy); if (empty($orderWay)) $orderWay = 'ASC'; if ($orderBy == 'id_product' OR $orderBy == 'date_add') $orderByPrefix = 'p'; elseif ($orderBy == 'name') $orderByPrefix = 'pl'; elseif ($orderBy == 'manufacturer') { $orderByPrefix = 'm'; $orderBy = 'name'; } elseif ($orderBy == 'position') $orderByPrefix = 'cp'; if ($orderBy == 'price') $orderBy = 'orderprice'; if (!Validate::isBool($active) OR !Validate::isOrderBy($orderBy) OR !Validate::isOrderWay($orderWay)) die (Tools::displayError()); $id_supplier = (int)(Tools::getValue('id_supplier')); // GET ALL CHILD CATEGORY NODES OF THIS CATEGORY $subcategory_result = $this->getSubCategoriesOfCategoryID((int)($this->id)); $this->recursive_categories[] = (int)($this->id); $this->getSubCategoriesRecursive((int)($this->id)); $combined_categories = $this->recursive_categories; /* Return only the number of products */ if ($getTotal) { $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' SELECT COUNT(DISTINCT cp.`id_product`) AS total FROM `'._DB_PREFIX_.'product` p LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON p.`id_product` = cp.`id_product` WHERE cp.`id_category` IN ('.implode(',',$combined_categories).')'.($active ? ' AND p.`active` = 1' : '').' '.($id_supplier ? 'AND p.id_supplier = '.(int)($id_supplier) : '')); return isset($result) ? $result['total'] : 0; } $sql = ' SELECT DISTINCT p.*, pa.`id_product_attribute`, pl.`description`, pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, i.`id_image`, il.`legend`, m.`name` AS manufacturer_name, tl.`name` AS tax_name, t.`rate`, cl.`name` AS category_default, DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AS new, (p.`price` * IF(t.`rate`,((100 + (t.`rate`))/100),1)) AS orderprice FROM `'._DB_PREFIX_.'category_product` cp LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = cp.`id_product` LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (p.`id_product` = pa.`id_product` AND default_on = 1) LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.(int)($id_lang).') LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)($id_lang).') LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1) LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)($id_lang).') LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` AND tr.`id_country` = '.(int)Country::getDefaultCountryId().' AND tr.`id_state` = 0) LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`) LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)($id_lang).') LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer` WHERE cp.`id_category` IN ('.implode(',',$combined_categories).')'.($active ? ' AND p.`active` = 1' : '').' '.($id_supplier ? 'AND p.id_supplier = '.(int)$id_supplier : ''); if ($random === true) { $sql .= ' ORDER BY RAND()'; $sql .= ' LIMIT 0, '.(int)($randomNumberProducts); } else { $sql .= ' ORDER BY '.(isset($orderByPrefix) ? $orderByPrefix.'.' : '').'`'.pSQL($orderBy).'` '.pSQL($orderWay).' LIMIT '.(((int)($p) - 1) * (int)($n)).','.(int)($n); } $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql); if ($orderBy == 'orderprice') Tools::orderbyPrice($result, $orderWay); if (!$result) return false; /* Modify SQL result */ return Product::getProductsProperties($id_lang, $result); } } That code will actually make each category you visit display all the products in subcategories below it i.e if you visit Apple Products, it will list all iPods and iPhones etc together. If you need to just get a product count, use the database statement with the count in it and assign that to a variable you can read in the .tpl file and revert the rest of the code to the same as the code in the original Prestashop Category.php file. Using the above code, the number is stored in {$nb_products} when you visit a certain category. You will need a lookup to show the counts for each category. -
If you mean the direct parent category of the product, you'd use: {$category->name|escape:'htmlall':'UTF-8'} If you mean the root category, you can pick any out of the path. If you have a breadcrumb included (breadcrumb.tpl), you can use the $path variable or just assign it if not: {if isset($smarty.capture.path)}{assign var='path' value=$smarty.capture.path}{/if} That's the full breadcrumb with links but you can extract the names by doing: {assign var=pathstrip value=$path|strip_tags} {assign var=pathname value='>'|explode:$pathstrip} {strip}{$pathname[0]}{/strip}
-
Hi there, you can do this by modifying Classes > Product.php. In the function getProductProperties, before the line: self::$producPropertiesCache[$cacheKey] = $row; add two lines: $row['tags'] = Tag::getProductTags((int)$row['id_product']); $row['tags'] = $row['tags'][(int)$id_lang]; In product-list.tpl, you can read the tags as an array e.g: {foreach from=$product.tags item=ptag} {/foreach} To read from product.tpl, the lookup is: {foreach from=$product->tags[$lang_id] item=ptag} {/foreach} Ideally you shouldn't modify core classes and override them instead but I've found some class overrides break so for the odd line of code, I just add it to the main class and note what I've changed.