Question

I am creating a custom category page for my store. Currently on the Page I have the default product list showing all the products in the category, like in the image below:

enter image description here

I was wondering if it were someway possible to, instead of displaying every product in the category if I could choose specific products to show up with PHP? Like a query or something where I can have the product Ids separated by a comma?

Kinda like <?php query by product ids (24,153,2,51,765) { ?>

I know the code above doesnt work (ofcourse), but just hope that shows what Im trying to accomplish. Thanks in advance!

Below is the Code that Displays the Products like in the image above:

  <?php if ($products) { ?>
  <div class="product-filter">

    <div class="limit"><b><?php echo $text_limit; ?></b>
      <select onchange="location = this.value;">
        <?php foreach ($limits as $limits) { ?>
        <?php if ($limits['value'] == $limit) { ?>
        <option value="<?php echo $limits['href']; ?>" selected="selected"><?php echo $limits['text']; ?></option>
        <?php } else { ?>
        <option value="<?php echo $limits['href']; ?>"><?php echo $limits['text']; ?></option>
        <?php } ?>
        <?php } ?>
      </select>
    </div>
    <div class="sort"><b><?php echo $text_sort; ?></b>
      <select onchange="location = this.value;">
        <?php foreach ($sorts as $sorts) { ?>
        <?php if ($sorts['value'] == $sort . '-' . $order) { ?>
        <option value="<?php echo $sorts['href']; ?>" selected="selected"><?php echo $sorts['text']; ?></option>
        <?php } else { ?>
        <option value="<?php echo $sorts['href']; ?>"><?php echo $sorts['text']; ?></option>
        <?php } ?>
        <?php } ?>
      </select>
    </div>
  </div>
  <div class="product-compare"><a href="<?php echo $compare; ?>" id="compare-total"><?php echo $text_compare; ?></a></div>
  <div class="product-list">
    <?php foreach ($products as $product) { ?>
    <div>
      <?php if ($product['thumb']) { ?>
      <div class="image"><a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" title="<?php echo $product['name']; ?>" alt="<?php echo $product['name']; ?>" /></a></div>
      <?php } ?>
      <div class="name"><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></div>
      <div class="description"><?php echo $product['description']; ?></div>
      <?php if ($product['price']) { ?>
      <div class="price">
        <?php if (!$product['special']) { ?>
        <?php echo $product['price']; ?>
        <?php } else { ?>
        <div class="price-old"><span>MSRP: <?php echo $product['price']; ?></span></div> <div class="price-new"><?php echo $product['special']; ?></div>
        <?php } ?>
        <?php if ($product['tax']) { ?>
        <br />
        <span class="price-tax"><?php echo $text_tax; ?> <?php echo $product['tax']; ?></span>
        <?php } ?>
      </div>
      <?php } ?>
      <?php if ($product['rating']) { ?>
      <div class="rating"><img src="catalog/view/theme/default/image/stars-<?php echo $product['rating']; ?>.png" alt="<?php echo $product['reviews']; ?>" /></div>
      <?php } ?>
      <div class="cart">
        <input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button" />
      </div>
      <div class="free-shipping"></div>
      <div class="wishlist"><a onclick="addToWishList('<?php echo $product['product_id']; ?>');"><?php echo $button_wishlist; ?></a></div>
      <div class="compare"><a onclick="addToCompare('<?php echo $product['product_id']; ?>');"><?php echo $button_compare; ?></a></div>
    </div>
    <?php } ?>
  </div>
  <div class="pagination"><?php echo $pagination; ?></div>
  <?php } ?>
  <?php if (!$categories && !$products) { ?>
  <div class="content"><?php echo $text_empty; ?></div>
  <div class="buttons">
    <div class="right"><a href="<?php echo $continue; ?>" class="button"><?php echo $button_continue; ?></a></div>
  </div>
  <?php } ?>
Was it helpful?

Solution

Hard-coding product filtering: :)

Find this line in category.tpl: ~ line 69

<div class="product-list"> 

Put this bellow: here foreach loop starts... You just have to put additional condition there.

<?php  $ids=array(43,44,45); // array of product id's   ?>
<?php foreach ($products as $product) { if(in_array($product['product_id'],$ids)) {  ?>

...... template code.......

And bellow, at the loop end should look like this:

 <?php } }?>  

add one more '}', (if condition must be closed properly). Should be line ~119.

P.S. Since you want filtering in just one category, i guess, one condition more should be made (if category id == selected category -> filter products...)

P.S.2 - Not sure about your intentions and desired goal, but rather than using of this dirty hack, use open cart built-in features (enable/disable product in admin panel), or try (if your problem is too specific) to make custom model/controller methods...

OTHER TIPS

To be honest I really do not understand hwy asking such question to be solved programaticaly, because when You have a custom category and want to display only some concrete products, then all that is needed is to unlink unwanted products from this category. That' all!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top