1

Sort order of related products seems to be same with sort order of category products.

I would like to use position for sorting. when I tried to set position fields of related products and reindex category products, nothing happen.

Does someone know how to edit specifically for related products?

Sorry Amit

I can't understand your solution. I just would like to use magento default function.

Usually, if I set positional order in related product tab in product editing page. It can use.

I searched the internet and somebody says related.php file has a bug in this thread. Do you think so?

core>Mage>Catalog>Block>Product>List>Related.php

Original

$this->_itemCollection = $product->getRelatedProductCollection()
        ->addAttributeToSelect('required_options')
        ->addAttributeToSort('position', Varien_Db_Select::SQL_ASC)
        ->addStoreFilter()

to

$this->_itemCollection = $product->getRelatedProductCollection()
        ->addAttributeToSelect('required_options')
        ->setOrder('position', Varien_Db_Select::SQL_ASC)
        ->addStoreFilter()

but this solution also not working for my site.

2
  • If I set the position value of related products under the respective product's tab in the backend, the related products are shown in the specified order. That does not work for you?
    – Simon
    Nov 14, 2014 at 7:35
  • yes I tried but the sort order seems to follow "created at" setting which i use in default config setting for category.
    – user16342
    Nov 15, 2014 at 7:44

2 Answers 2

1

You can not sort related product Collection by using position sort field of category

First you need to add catalog_category_product table in related product Collection.

Because

One product may be included on different different categories that product have different position on different category,therefore for this reason on you can only join a category.

$collection->joinField('category_id',
    'catalog/category_product',
    '*',
    'product_id=product_id',
    null,
    'left'
);
$collection->addAttributeToFilter('category_id',5);

Example:

Suppose a product is included in two category cat1 and cat2.From admin>Manage Category>

you have set different sort for Cat1-> 5, Cat2->8. That time you will face the issue.

1
  • Excellent.. +1 for that :-) Nov 14, 2014 at 7:51
0

I used a custom module to do this. The sorting function is for Magento 1.9.x though (see code block below). It might as well be implemented into your template file, but I did not try this.

public function custom_FilterSort_relatedProducts($_product, $viewType) {
    if( $viewType == 'related' ) :
        $_collection = $_product->getRelatedLinkCollection();
    elseif( $viewType == 'upsell' ) :
        $_collection = $_product;
    endif;
    foreach ($_collection as $link):
        if( $viewType == 'related' ) :
            $_entityId = $link->getLinkedProductId();
            $relatedProduct = Mage::getModel('catalog/product')->load($link->getLinkedProductId());
        elseif( $viewType == 'upsell' ) :
            $_entityId = $link->getEntityId();
            $relatedProduct = $link;
        endif;
        $relatedData[$_entityId]['position'] = $link->getPosition();
        $relatedData[$_entityId]['status'] = $relatedProduct->getStatus();
        $relatedData[$_entityId]['avalaibility'] = $relatedProduct->getStockItem()->getIsInStock();
        if( $relatedData[$_entityId]['status'] == 2 || $relatedData[$_entityId]['avalaibility'] == 0 ) {
            unset($relatedData[$_entityId]);
        }
    endforeach;
    uasort($relatedData, function($first, $last) {
        return $first['position'] - $last['position'];
    });
    return $relatedData;
}

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.