Opencart: sort products by their subcategory on the category page

Opencart: sort products by their subcategory on the category page



I am building a shop with opencart 1.5.5.1 (I'm new to it) and I ran into a little problem:



Any of my main category pages looks like this:
Refine search:
    -Subcategory1

    -Subcategory2

    -Subcategory3


  Product1  Product2  Product3  ...



Subcategory 1,2 and 3 being links to the subcategory pages.



What i want my main category page to be like is:



Subcategory1

Product1  Product2  Product3  .......

Subcategory2
Product1  Product2  Product3  .......

Subcategory3

Product1  Product2  Product3  .......



Where Subcategory 1,2 and 3 are no longer links to the subcategory pages but simple text.



I have found this extension: http://www.opencart.com/index.php?route=extension/extension/info&token=b4381909f29ca2d2511830b09f09fa84&extension_id=11190 but maybe someone can help me without me having to pay 15$



Can anyone help me please?





Have you installed a custom/new theme or are you using the default opencart theme?
– BIT CHEETAH
May 29 '13 at 7:25





@BIT CHEETAH - I use the default theme.
– Radu Cristian
May 29 '13 at 10:31






@burhan - I have not tried that much. The are a few posts on opencart forum asking the same thing but there aren't any concrete answers. I tried modifying some php and tpl files from the default theme but without luck.
– Radu Cristian
May 29 '13 at 14:07





2 Answers
2



→ Ok so I'm going to go through this step by step, ask questions between steps or vote this step up if you got it:



STEP #1:



Open the following file:


catelog/controller/product/category.php



SAVE A BACKUP OF THIS FILE FIRST THEN KEEP GOING!



Somewhere around line 271 you should see this:


$this->data['categories'] = array(

'name' => $result['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),

'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url)

);



Change the above code to:


$this->data['categories'] = array(

'category_id' => $result['category_id'], /*!!!*/

'name' => $result['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),

'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url)

);



STEP #2:



Open the same file you did in Step 1:


catelog/controller/product/category.php



SAVE A BACKUP OF THIS FILE FIRST THEN KEEP GOING!



Somewhere around line 271 you should see this:


$this->data['categories'] = array(

'category_id' => $result['category_id'], /*!!!*/

'name' => $result['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),

'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url)

);

}

/* New code will be pasted here! */

$this->data['products'] = array();



Obviously, this is the code we worked on in Step 1, but what your going to do now is copy and paste the following code after that last '}' but before '$this->data['products'] = array();'


$this->data['products_all'] = array();

for( $x = 0; $x < count( $this->data['categories'] ); $x++ )

$cat = $this->data['categories'][ $x ][ 'category_id' ];
$this->data['products_all'][ $cat ] = array();

$data = array(
'filter_category_id' => $cat,
'sort' => $sort,
'order' => $order,
'start' => ($page - 1) * $limit,
'limit' => $limit
);

$product_total = $this->model_catalog_product->getTotalProducts($data);
$results = $this->model_catalog_product->getProducts($data);

foreach ($results as $result)

if ($result['image'])
$image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));

else $image = false;

if (($this->config->get('config_customer_price') && $this->customer->isLogged())



STEP #3:



Open the following file:


catalog/view/theme/default/template/product/category.tpl



SAVE A BACKUP OF THIS FILE FIRST THEN KEEP GOING!



Somewhere around line 19 you should see this:


<?php if ($categories) { ?>
<h2><?php echo $text_refine; ?></h2>
<div class="category-list">
<?php if (count($categories) <= 5) ?>
<ul>
<?php foreach ($categories as $category) ?>
<li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a></li>
<?php ?>
</ul>
<?php else { ?>



Change the above code to:


<?php if ($categories) { ?>

<div class="category-list">

<?php

if ( count( $categories ) <= 5 )

foreach ( $categories as $k=>$category )

if( $k > 0 ) echo '<br /><br />';

echo '<div class="product-filter" style="background:#222;padding:5px;padding-bottom:2px">';
echo '<a href="' . $category['href'] . '" style="font-size:21px;text-decoration:none;"><h2 style="color:#eee;font-style:italic"><span style="color:#777;position:relative;bottom:2px">&rarr;&nbsp;</span><span style="border-bottom:solid 1px #777;">' . $category['name'] . '</span>:</h2></a>';
echo '</div>';
echo '<hr style="border:none;border-top:solid 1px #eee"/>';
echo '<div class="product-list">';

foreach ( $products_all[ $category['category_id'] ] as $product )

echo '<div>';
if ( $product['thumb'] )
echo '<div class="image"><a href="' . $product['href'] . '"><img src="' . $product['thumb'] . '" title="' . $product['name'] . '" alt="' . $product['name'] . '" /></a></div>';

echo '<div class="name"><a href="' . $product['href'] . '">' . $product['name'] . '</a></div>';
echo '<div class="description">' . $product['description'] . '</div>';
if ( $product['price'] )
echo '<div class="price">';
if ( !$product['special'] ) echo $product['price'];
else echo '<span class="price-old">' . $product['price'] . '</span> <span class="price-new">' . $product['special'] . '</span>';
if ( $product['tax'] ) echo '<br /><span class="price-tax">' . $text_tax . ' ' . $product['tax'] . '</span>';
echo '</div>';

if ( $product['rating'] )
echo '<div class="rating"><img src="catalog/view/theme/default/image/stars-' . $product['rating'] . '.png" alt="' . $product['reviews'] . '" /></div>';

echo '<div class="cart">';
echo '<input type="button" value="' . $button_cart . '" onclick="addToCart('' . $product['product_id'] . '');" class="button" />';
echo '</div>';
echo '<div class="wishlist"><a onclick="addToWishList('' . $product['product_id'] . '');">' . $button_wishlist . '</a></div>';
echo '<div class="compare"><a onclick="addToCompare('' . $product['product_id'] . '');">' . $button_compare . '</a></div>';
echo '</div>';

echo '</div>';



?>

<?php else { ?>





Ok I changed the code in catalog/controller/product/category.php (step 1 complete). Step2?
– Radu Cristian
May 30 '13 at 9:59






Don't You think asnwering for three times and requesting the OP to upvote Your each answer if he gets it is kinda harsh? You should answer just once and make edits to Your answer. I am flagging all three of Your answers.
– shadyyx
May 30 '13 at 15:03





@shadyyx, No I do not, just because I answered this question did not mean that the answer was sitting on my computer. I knew how to do it but I had to write the code as I did it. If I had not split it up, this question would have been left unanswered as I would not risk doing the entire piece without knowing if the questioner could follow along and complete the work. The upvote request is a measly incentive I had to help Radu which also saved him $15: I highly doubt he would consider my method "kinda harsh;" these are the sort of things that moderators with nothing real to moderate grip about.
– BIT CHEETAH
Jun 1 '13 at 7:45



What about opencart 2? If i do like you, I get



Notice: Undefined offset: 60 in ..category.tpl on line 59
Warning: Invalid argument supplied for foreach() in ..category.tpl on line 59






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)