Related products dropdown menu in product page - magento

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.

Related

Failing to fetch product collection for a given category

I've written a method that I'm calling from my list.phtml file.
The logic should simply output the number of products in the currently viewed category and the highest and lowest prices. Pretty straight forward really.
For reasons beyond my understanding it's always returning that the current category contains no products, and I can't figure out why but I presume it's because I'm not fetching the product collection correctly.
Here's the code for my method.
public function writeCatInfo(){
if(Mage::registry('current_category')){
$_productCollection=$this->getLoadedProductCollection;
$catname = Mage::registry('current_category')->getName();
$priceArray = array();
foreach ($_productCollection as $_product){
$priceArray[] = $_product->getSpecialPrice();
}
$numItems = count($_productCollection);
$highPrice = number_format(max($priceArray),2);
$lowPrice = number_format(min($priceArray),2);
$infostring = ucwords($catname)." contains ".$numItems." products with prices ranging from £".$lowPrice." to £".$highPrice;
echo $infostring;
}
}
this will help you to get current category product collection.
$category_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
$category = Mage::getModel('catalog/category')->load($category_id);
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addCategoryFilter($category)
->setOrder('price', 'ASC')
->load();
Turns out my helper was extending the wrong class - Mage_Core_Helper_Abtract instead of Mage_Catalog_Block_Product_List. Changing this fixed the problem.
Please see the below code to get product collection by Category id
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*') // add all attributes - optional
->addAttributeToFilter('status', 1) // enabled
->addAttributeToFilter('visibility', 4) //visibility in catalog,search
->setOrder('price', 'ASC'); //sets the order by price

How to count number of product in magento product listing?

I am new to magento, i have to show total number of product of every category !
It shows only product of single page, but have to show all product of single category ?
My code is below :)
<?php
$_productCollection = $this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
// Changed By Umesh //
echo $_productCollection->count();
// End of Change by Umesh
?>
It shows only 9, that is pagination per page product, but i have to show all number of product of certain category.
you can also use getSize()
$cat_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
$collection = Mage::getResourceModel('catalog/product_collection')
->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
->addAttributeToFilter('category_id', array('in' => $cat_id))
->addAttributeToSelect('*');
echo $collection->getSize();
To get the total no of products on product listing page (of that particular category), you can write below code
$currentCatId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
$_testproductCollection = Mage::getResourceModel('catalog/product_collection')
->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
->addAttributeToFilter('category_id', array('in' => $currentCatId))
->addAttributeToSelect('*');
echo count($_testproductCollection);

How to get the product count from the url's for the manufacture

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.

Need product count in magento

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

How to get all enable and disable products from a specific category?

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)
);

Resources