---
title: "Magento random display same category product"
author: "kirenenko04"
date: "2014-12-17T18:11:18Z"
tags: []
categories: ["编程语言"]
slug: "magento-random-display-same-category-product"
draft: false
lastmod: "2014-12-17T18:11:18Z"
id: 2167154
url: "https://kirenenko04.iteye.com/blog/2167154"
---

```
	public function get_random_same_cate_products() {
		$_product = $this->getProduct();
		$categoryIds = $_product->getCategoryIds();
		if(count($categoryIds) ){
			foreach ($categoryIds as $cid) {
				$_category = Mage::getModel('catalog/category')->load($cid);
				if ($_category->getLevel() == 3) {
					
					$_productCollection = Mage::getModel('catalog/product')
					->getCollection()
					->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
					->addAttributeToSelect('*')
					
					->addAttributeToFilter('category_id', array(
							array('finset' => $cid),
							)
					)
					->addAttributeToFilter('type_id','configurable')
					->addAttributeToFilter('sku',array('neq'=>$_product->getSku()));
					
					$_productCollection->getSelect()->order(new Zend_Db_Expr('RAND()'));
					$_productCollection->setPage(1, 4);
					
					return $_productCollection;
				}
			}
			
		}
	}
```