How to Get Products from a Particular Category in Magento Ecommerce

Display magento products by category ID

get Product from specific category

$categoryIds = array(2,4);//category id

$collection = Mage::getModel('catalog/product')
->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect('*')
->addAttributeToFilter('category_id', array('in' => $categoryIds))

get Product for specific product id

$productids = array(52,62);//product ids
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addFieldToFilter('entity_id',array( 'in' => $productids));

then write this in phtml

<?php $_collectionSize = $collection->count() ?>
<?php //$_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($collection as $product): ?>
<?php if ($i++%4==0): ?>
<ul class="products-grid">
<?php endif ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a href="<?php echo $product->getProductUrl()?>" title="<?php echo $product->getName()?>">
<img src="<?php echo Mage::helper('catalog/image')->init($product, 'small_image')->resize(197, 167); ?>" alt="<?php echo $product->getName()?>" border="0" />
</a>
<h2 class="product-name"><a href="<?php echo $product->getProductUrl()?>" title="<?php echo $product->getName()?>"><?php echo $product->getName() ?></a></h2>
<div class="price-box">
<?php echo Mage::helper('core')->currency($product->getPrice(),true,false);?>
</div>
<div class="actions">
<?php if($product->isSaleable()): ?>
<button class="button" onclick="setLocation('<?php echo Mage::getUrl('checkout/cart/add/')?>product/<?php echo $product->getId() ?>/')" title="<?php echo $this->__('Köp');?>" type="submit"><span><span><?php echo $this->__('Köp');?></span></span></button>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>

</div>
</li>
<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
</ul>
<?php endif ?>
<?php endforeach ?>

hope this help you

Magento - list of products in a specific category

Your code has a couple of mistakes in it, let me highlight them:

  1. $category=$category->getProductCollection(); - this is a very bad practice. You're assigning the Collection of product to a variable that was previously a category. It's easy to get lost in such code. Please remember to use meaningful naming conventions for your variable. This like should be $productCollection=$category->getProductCollection();

  2. Instead of looping the collection that you already have, you're getting the array of ids, and looping through them, loading objects during each loop. This will lead to memory leaks and very large amount of time your code will perform. The foreach statement should be like this: foreach ($productCollection as $product).

  3. You haven't actually asked a question here. You've said that you're going to make grid, and posted your code, but where's the problem?

  4. If you want to create Product Grid, please review how it's done in Magento core Catalog module : block Mage_Catalog_Block_Product_List and template catalog/product/list.phtml

How to get all product from a category including its subcategories in Magento?

I've solved this problem by implementing addCategoriesFilter in product collection model, here is the patch. Modified code to be copied to the local pool to allow updates to a newer version.

@@ -103,6 +103,7 @@
* Allowed filters
* store_id int;
* category_id int;
+ * category_ids array;
* category_is_anchor int;
* visibility array|int;
* website_ids array|int;
@@ -567,6 +568,21 @@
}

/**
+ * Specify categories filter for product collection
+ *
+ * @param array $categories
+ * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
+ */
+ public function addCategoriesFilter(array $categories)
+ {
+ $this->_productLimitationFilters['category_ids'] = $categories;
+
+ ($this->getStoreId() == 0)? $this->_applyZeroStoreProductLimitations() : $this->_applyProductLimitations();
+
+ return $this;
+ }
+
+ /**
* Join minimal price attribute to result
*
* @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
@@ -1592,7 +1608,7 @@
$this->_productLimitationJoinPrice();
$filters = $this->_productLimitationFilters;

- if (!isset($filters['category_id']) && !isset($filters['visibility'])) {
+ if (!isset($filters['category_id']) && !isset($filters['category_ids']) && !isset($filters['visibility'])) {
return $this;
}

@@ -1604,11 +1620,16 @@
$conditions[] = $this->getConnection()
->quoteInto('cat_index.visibility IN(?)', $filters['visibility']);
}
- $conditions[] = $this->getConnection()
- ->quoteInto('cat_index.category_id=?', $filters['category_id']);
- if (isset($filters['category_is_anchor'])) {
+
+ if (!isset($filters['category_ids'])) {
$conditions[] = $this->getConnection()
- ->quoteInto('cat_index.is_parent=?', $filters['category_is_anchor']);
+ ->quoteInto('cat_index.category_id=?', $filters['category_id']);
+ if (isset($filters['category_is_anchor'])) {
+ $conditions[] = $this->getConnection()
+ ->quoteInto('cat_index.is_parent=?', $filters['category_is_anchor']);
+ }
+ } else {
+ $conditions[] = $this->getConnection()->quoteInto('cat_index.category_id IN(' . implode(',', $filters['category_ids']) . ')', "");
}

$joinCond = join(' AND ', $conditions);

Usage:

$category = $layer->getCurrentCategory();
$categories = $category->getAllChildren(true);

$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoriesFilter($categories);

How can I show NEW products per row from specific categories in Magento?

Th following collection query should get you what you want

$todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);            
$products = $category->
getProductCollection()->
addCategoryFilter($category)->
addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate))->
addAttributeToFilter('news_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')->
addAttributeToSelect('*');

A products "newness" is determined by two attributes, news_from_date and news_to_date, so you want to add two additional attributes to the filter. The specific method calls from above that do this are

addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate))->
addAttributeToFilter('news_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')->

They're taken directly from the New Product block at

/app/code/core/Mage/Catalog/Block/Product/New.php

How to get a list of product categories and their ID's from Magento?

There's probably a easy way to do this using PHP / Magento code, but if you want to look directly at the database:

Try something like this:

SELECT entity_id AS categoryID, value AS categoryName
FROM catalog_category_entity_varchar
WHERE attribute_id=111

But attribute_id might be something other than 111 for you. Take a look at the table to see.

Get product collection in Magento, filtered by Category & Custom Attribute Value

<?php
// load category object by category ID
$category = Mage::getModel('catalog/category')->load(1);

// get product collection, filter it by category,
// add the color attribute to select to be able to filter using it later
$productCollection = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($category)
->addAttributeToSelect('color')
->addFieldToFilter(array(
array('attribute'=>'color','eq'=>'white'),
));

More info check

How to get products from a particular category in magento ecommerce

Magento - Retrieve products with a specific attribute value

https://magento.stackexchange.com/questions/5838/get-product-collection-from-a-category-id



Related Topics



Leave a reply



Submit