0

For some reason the sort order for related products is not working.

How can I sort the order asc to desc (low to high)?

Here is my current /catalog/product/list/related.phtml file:

<div class="block-content">
    <ul class="gerelateerde-producten-list" id="block-related">
    <?php $i = 0; foreach ($this->getItems() as $_item) : if($i < 2) {?>
        <li class="item">
            <div class="product">
                <div class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(150, 170) ?>" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" /></div>
                <div class="product-details">
                    <p class="product-name"><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $this->htmlEscape($_item->getName()) ?></a></p>
                    <?php echo $this->getPriceHtml($_item, true, '-related') ?>
                    <?php if(!$_item->isComposite() && $_item->isSaleable()): ?>
                    <?php if (!$_item->getRequiredOptions()): ?>
                    <div class="related-box-check">
                        <input type="checkbox" class="checkbox related-checkbox" id="related-checkbox<?php echo $_item->getId() ?>" name="related_products[]" value="<?php echo $_item->getId() ?>" />
                        <label class="label-related" for="related-checkbox<?php echo $_item->getId() ?>"><span class="icon-related-product"></span>order</label>
                    </div>
                    <?php endif; ?>
                    <?php endif; ?>
                </div>
            </div>
        </li>
    <?php } $i++; endforeach ?>
    </ul>
    <script type="text/javascript">decorateList('block-related', 'none-recursive')</script>
</div>
0
<div class="block-content">
<ul class="gerelateerde-producten-list" id="block-related">

<?php 
if (count($this->getItems())): 
$datarev = array();
foreach ($this->getItems() as $_item): 
  $datarev [] = $_item;  
endforeach; 

$datarev = array_reverse($datarev);
$i = 0;
foreach($datarev as $_item): ?>
    <li class="item">
        <div class="product">
            <div class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(150, 170) ?>" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" /></div>
            <div class="product-details">
                <p class="product-name"><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $this->htmlEscape($_item->getName()) ?></a></p>
                <?php echo $this->getPriceHtml($_item, true, '-related') ?>
                <?php if(!$_item->isComposite() && $_item->isSaleable()): ?>
                <?php if (!$_item->getRequiredOptions()): ?>
                <div class="related-box-check">
                    <input type="checkbox" class="checkbox related-checkbox" id="related-checkbox<?php echo $_item->getId() ?>" name="related_products[]" value="<?php echo $_item->getId() ?>" />
                    <label class="label-related" for="related-checkbox<?php echo $_item->getId() ?>"><span class="icon-related-product"></span>order</label>
                </div>
                <?php endif; ?>
                <?php endif; ?>
            </div>
        </div>
    </li>
<?php 
$i++;
if ($i == 2) break;
endforeach;?>
<?php endif; ?>
</ul>
<script type="text/javascript">decorateList('block-related', 'none-recursive')</script>

  • Thanks a lot! That works almost perfect, but I want to display only the first two items. So how can I change that? Therefore I used <?php $i = 0; foreach ($this->getItems() as $_item) : if($i < 2) {?>. – Henk Z Mar 14 '17 at 10:54
  • Are you check this code? – Mohammad Aktaruzzaman Mar 20 '17 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.