How do I get two products that were added just before current product and two products that were added just after the current product in a same category as the related product?
Here is how I am fetching 4 related products from the same category in product view page. Right now it shows them in random order.
<?php
if ($_product) {
$categories = $_product->getCategoryCollection()
->setPage(1, 1)
->load();
if ($categories->count()) { ?>
<h2>Related Products:</h2>
<?php foreach ($categories as $_category) {
$cur_category = Mage::getModel('catalog/category')->load($_category->getId());
?>
<?php $products = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($_category)
->addAttributeToSelect('small_image');
$products->getSelect()->limit(4);
$products->getSelect()->order('rand()');
foreach ($products as $productModel) {
$_product = Mage::getModel('catalog/product')->load($productModel->getId());
$width = 200;
$height = 150;
$_imageUrl = $this->helper('catalog/image')->init($productModel, 'small_image')->resize($width, $height);
?>
<div class="related-item">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200, 100); ?>" width="200" height="150" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"/>
</a>
<h4><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h4>
</div>
<?php }
} ?>
<?php }
}?>
Thanks
added just before
andadded just after
? by date ? by id