I need to show product count behalf of brand/manufacturer
foreach($children as $subCat)
{
$brand_name = $subCat->getName();
$products = Mage::getModel('catalog/product')->getCollection();
$products = $products->addAttributeToFilter('manufacturer',$brand_name);
echo $products->count();
}
If brand is dropdowon or Multi Select attribute then you cannot filter by BrandName(That means option name).
You can filter by Brand Option id:
$products = Mage::getModel('catalog/product')->getCollection();
$products = $products->addAttributeToFilter('manufacturer',$brand_id);
if you are using Catalog Product flat then you need to Enable manufacturer attribute to Product Listing from Manage attribute >select manufacturer attribute enable attibute to product listing
According to you comment if you want to Product Collection by two catgories then try below
$_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' => $catid),
array('finset' => $parent_Cat_id))
)
->addAttributeToSort('created_at', 'desc');
More details in Magento 1.7 Filter products by multiple categories
Or get all option list of Manufacture attribute then you can get it option id by below code
$attribute_code = "manufacture";
$attribute_details = Mage::getSingleton("eav/config")->getAttribute("catalog_product", $attribute_code);
$options = $attribute_details->getSource()->getAllOptions(false);
foreach($options as $option){
// print_r($option) and find all the elements e
cho $option["value"];
echo $option["label"];
}
See more at: http://www.techdilate.com/code/magento-get-attribute-options-of-the-dropdown-type-attribute/#sthash.81mUiz6O.dpuf
Then get product Collection filter by option ids
Related
I have the following Url’s "wool-thread.php?manufacturer=153"
I want to get the product count from the url
ie. Products count for the category having the manufacture attribute "153"
You need to filter product collection by the manufacturer attribute. Here is working code.
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('manufacturer');
$products->addFieldToFilter(array(
array('attribute'=>'manufacturer', 'eq'=> $option_id,
)));
$products->joinField(
'stock_status',
'cataloginventory/stock_status',
'stock_status',
'product_id=entity_id', array(
'stock_status' => Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK,
'website_id' => Mage::app()->getWebsite()->getWebsiteId(),
)
);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
$pro_count = $products->count();
Where Option id is the manufacturer id. In your example it is 153.
It will return filtered collection of enabled product, visible products, and in stock products. After that count the products.
You can use something like :
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('manufacturer');
$collection->addCategoryFilter(Mage::registry('current_category'));
$collection->addAttributeToFilter('manufacturer',array('eq'=>'153'));
echo $collection->count();
or
$products = Mage::getModel('catalog/category')->load(Mage::registry('current_category'))
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('manufacturer', array('eq' => 153));
then
$usedAttributeValues = array_unique($products->getColumnValues($attributeCode));
$attributeModel = Mage::getSingleton('eav/config')->getAttribute('catalog_product', $attributeCode);
$usedAttributeValues = $attributeModel->getSource()->getOptionText(
implode(',', $usedAttributeValues)
);
See https://magento.stackexchange.com/a/4039/9489 for the use of getSize or count on collection.
I would like to create a dropdown menu prefilled with related products on the product page.
I came across the code below which displays all products in a dropdown menu. I pasted it in the file view.phtml and proved to be working. How can I modify it to display related products instead?
<select>
<?php
$products = Mage::getResourceModel('catalog/product_collection')
->setStore(Mage::app()->getStore()->getId())
->addAttributeToFilter('status', array('eq' => '1'))
->addAttributeToFilter('type_id', array('eq' => 'simple'));
foreach ($products as $prod_model) {
$product = Mage::getModel('catalog/product')->load($prod_model->getId());
echo "<option value=\"".$product->getId()."\">".$product->getName()."</option>";
}
?>
I also found out that I could do it through SKU's and came across this code that retrieves the ID's of the matching products:
$match = substr($product->getSku(), 0, 4);
$resource = Mage::getModel('core/resource');
$read = $resource->getConnection('core_read');
$select = $read->select()
->from(array('e'=>$resource->getTableName('catalog/product')), 'entity_id')
->where("e.sku LIKE '" . $match . "%'");
$ids = $read->fetchAll($select);
I just want to know how to link both codes together, by either using the related products or SKU.
Can anybody help me on that? Many thanks
Use this :
$products = $_product->getRelatedProductCollection()->setStore(Mage::app()->getStore()->getId())
->addAttributeToFilter('status', array('eq' => '1'))
->addAttributeToFilter('type_id', array('eq' => 'simple'));
To have your collection of related products.
i select products with
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('entity_id', array('in' => $productIds));
how can i archieve that the collection is in the same order as the ids in $productIds?
thanks
$productIds = array(1,3,2);
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('entity_id', array('in' => $productIds));
$products->getSelect()->order("find_in_set(entity_id,'".implode(',',$productIds)."')");
foreach($products as $product)
{
echo $product->getEntityId();
echo $product->getSku();
}
See more #
Magento get a product collection in an arbitrary order
How to select mysql rows in the order of IN clause
i am trying to get all enabled and disabled products and i am using this code :
/*$categoryId = 3; // a category id that you can get from admin
$category = Mage::getModel('catalog/category')->load($categoryId);
$collection = Mage::getModel('catalog/product')
->getCollection()
->addCategoryFilter($category)
->addAttributeToSelect('*')
->addAttributeToFilter('status', array('gt' => 0))
->load();
$categoryId = 3; // a category id that you can get from admin
$category = Mage::getModel('catalog/category')->load($categoryId);*/
above code brings enabled products only.
By commenting status filter it still brings the same result i.e. only enabled products.
/*$collection = Mage::getModel('catalog/product')
->getCollection()
->addCategoryFilter($category)
->addAttributeToSelect('*')
//->addAttributeToFilter('status', array('gt' => 0))
->load();*/
It still brings only Enabled products. But when i comment category check than it brings n all products :( Can anyone help plz ?
Note:
For those who are not clear about this query, let me tell you that Status Enabled = 1 and Status Disabled = 2.
So status greater than zero should bring me both enabled and disabled products, but it is not doing so.
So any idea ???
I Edit the code and it
$collection = Mage::getModel('catalog/category')->load(3)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToSort('entity_id', 'ASC');
die((string) $collection->getSelect());
And this bring in this query :
SELECT `e`.*, `cat_index`.`position` AS `cat_index_position` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id=1 AND cat_index.category_id='3' ORDER BY `e`.`entity_id` ASC
I have found why is this happening.
This is because when you want to get products from category using $productCollection->addCategoryFilter() or using $category->getProductCollection() query use product to category connection INDEX table. When you run re-index that product category connection INDEX table is populated only with Enabled products, and you can not get disabled products on that way. Is this bug in Magento or not I do not know, you can use Raw SQL query to get your products from a specific category.
You use ->addAttributeToFilter('status', array('gt' => 0)) on your collection, that filters on enabled products, so you can remove this line to get all products
Using your example, with minor change to reflect my category ID.
In category 233 I have 44 products, all enabled.
$categoryId = 233; // a category id that you can get from admin
$category = Mage::getModel('catalog/category')->load($categoryId);
$collection = Mage::getModel('catalog/product')
->getCollection()
->addCategoryFilter($category)
->addAttributeToSelect('*')
// ->addAttributeToFilter('status', array('gt' => 0)) //filter commented, show all products
->load();
echo $collection->count(); // 44 products
$category = Mage::getModel('catalog/category')->load($categoryId);
$collection = Mage::getModel('catalog/product')
->getCollection()
->addCategoryFilter($category)
->addAttributeToSelect('*')
->addAttributeToFilter('status', array('eq' => 1)) //show only enabled
->load();
echo $collection->count(); // 44 products
$collection = Mage::getModel('catalog/product')
->getCollection()
->addCategoryFilter($category)
->addAttributeToSelect('*')
->addAttributeToFilter('status', array('eq' => 2)) //only disabled
->load();
echo $collection->count(); // 0 products
As I said in my comment, I don't understand why do you want to filter status if you want to get both possible statuses.
You can use the Magento's constant Mage_Catalog_Model_Product_Status::STATUS_DISABLED will only return the products that are disabled within a category
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(
'status',
array('eq' => Mage_Catalog_Model_Product_Status::STATUS_DISABLED)
);
In Magento,
I want to get the product with name attribute is Product 1 *OR* Product 2
$product = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('name','Product 1')
->setPageSize(1)
->load()
->getFirstItem();
//OR
if( !$product->getId() )
{
$product = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('name','Product 2')
->setPageSize(1)
->load()
->getFirstItem();
}
How can I just using one statement to do?
refer to this article http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections in magento knowledgeBase