How to get products by manufacturer? - magento

I want to get list of products which belongs to the selected manufacturer on custom module's frontend. I successfully got the list of manufacturers but I can't retrieve the products related to particular manufacturer. I searched it a lot but I didn't got any solution.
Here is my code
block file:-
public function __construct()
{
parent::__construct();
$brand_id = $this->getRequest()->getParam('id');
$layer = $this->getLayer();
//$collection = Mage::getModel('catalog/product')->getCollection()
$collection = $layer->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('manufacturer', 20);
$this->setCollection($collection);
}
public function getProductCollection()
{
if (is_null($this->_productCollection))
{
$layer = $this->getLayer();
//$collection = Mage::getModel('catalog/product')->getCollection()
$collection = $layer->getProductCollection()
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addAttributeToFilter('manufacturer', 20)
->addStoreFilter(Mage::app()->getStore()->getId())
->addMinimalPrice()
->addUrlRewrite();
$this->_productCollection = $collection;
Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this->_productCollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_productCollection);
}
return $this->_productCollection;
}
public function getColumnCount()
{
return 3;
}
and this is my phtml code
<?php
$product = $this->getProductCollection();
foreach ($product as $prods)
{
echo '<pre>'; print_r($prods->getData()); die;
}
?>

As mentioned above you will need to follow a few steps:
1) Goto Attribute Sets, and make sure "manufacturer" is assigned to the attribute set you are using.
2) Make sure you have added some manufacturers into the attribute options.
3) Assign one of the options to your product.
Depending on your magento version this should work:
<?php echo $_product->getAttributeText('manufacturer') ?>
I can see the error you are getting:
gives error Call to a member function getManufacturer() on a non-object in
Are you sure you are putting this code after this line:
<?php $_product = $this->getProduct(); ?>
Hope this solutions have make you day ....!!!

Related

sorting and pager not working on custom collection Magento

Hi i have added a new product relation in magento named as accesories.
Like i have a main product A and i have attach B c and d with it as
accesories
Now on the frontend i need to show this collection as the same design
on list .phtml
so i have decide to render the custom collection on the list .phtml
for this i have render a custom collection in _getProductCollection()
by below code
if($_GET['product_id'])
{
$productid=$_GET['product_id'];
$product = Mage::getModel('catalog/product_link')
->getCollection()
->addFieldToFilter('link_type_id', 6)
->addFieldToFilter('product_id',$productid)
->load();
$LinkedProduct=$product->getData();
foreach($LinkedProduct as $LinkedProducts)
{
$LinkedProductId[]= $LinkedProducts['linked_product_id'];
}
$productIds = array_values($LinkedProductId);
$_productCollection=Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', array('in' => $productIds))
->load();
$this->_productCollection=$_productCollection;
}else{
if (is_null($this->_productCollection)) {
$layer = $this->getLayer();
/* #var $layer Mage_Catalog_Model_Layer */
if ($this->getShowRootCategory()) {
$this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
}
// if this is a product view page
if (Mage::registry('product')) {
// get collection of categories this product is associated with
$categories = Mage::registry('product')->getCategoryCollection()
->setPage(1, 1)
->load();
// if the product is associated with any category
if ($categories->count()) {
// show products from this category
$this->setCategoryId(current($categories->getIterator()));
}
}
$origCategory = null;
if ($this->getCategoryId()) {
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
$this->addModelTags($category);
}
}
$this->_productCollection = $layer->getProductCollection();
$this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
if ($origCategory) {
$layer->setCurrentCategory($origCategory);
}
}
}
return $this->_productCollection;
}
The collection is working fine
but the problem is the sorting and toolbar is not working
. Can you please suggest me how can i make a sorting and pager works with my custom colection
OK i have solve this by adding a sort function in collection like below code
$productid=$_GET['product_id'];
$product = Mage::getModel('catalog/product_link')
->getCollection()
->addFieldToFilter('link_type_id', 6)
->addFieldToFilter('product_id',$productid)
->load();
$LinkedProduct=$product->getData();
foreach($LinkedProduct as $LinkedProducts)
{
$LinkedProductId[]= $LinkedProducts['linked_product_id'];
}
$productIds = array_values($LinkedProductId);
$_productCollection=Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', array('in' => $productIds))
->addAttributeToSort($_GET['order'],$_GET['dir'] )
->setPageSize($_GET['limit'])
->load();
echo $_productCollection->getSelect() ;
$this->_productCollection=$_productCollection;
the sorting was not working because there wqas no sort function added in the colection
->addAttributeToSort($_GET['order'],$_GET['dir'] )
->setPageSize($_GET['limit'])
now it is working fine.
thanks

Magento - Accessing a customer's wishlist

I would like to be able to load a customer's wishlist and return the product id's of the products within the list
I am using:
$wishList = Mage::getSingleton('wishlist/wishlist')->loadByCustomer($customer);
$wishListItemCollection = $wishList->getItemCollection();
The problem is that the arrays in the Item Collection are protected and I can't find any methods to extract the data.
Is there another way to do this?
You're very close to your target.
$wishList = Mage::getSingleton('wishlist/wishlist')->loadByCustomer($customer);
$wishListItemCollection = $wishList->getItemCollection();
if (count($wishListItemCollection)) {
$arrProductIds = array();
foreach ($wishListItemCollection as $item) {
/* #var $product Mage_Catalog_Model_Product */
$product = $item->getProduct();
$arrProductIds[] = $product->getId();
}
}
The array variable $arrProductIds will now contain the list of all Product IDs that have been wishlisted by that specific Customer.
Your code is correct. There may be customer is not loaded. here is code.
$customer = Mage::getSingleton('customer/session')->getCustomer();
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
// do things
}
In any template, using magento 1.8, this works
Total: <?php echo $this->helper('wishlist')->getItemCount() ?>
// Items
$this->helper('wishlist')->getWishlist()->getItemCollection();
$wishList = Mage::getSingleton('wishlist/wishlist')->loadByCustomer($customer);
$wishListItemCollection = $wishList->getItemCollection();
foreach ($wishListItemCollection as $item)
{
//do your thing e.g. echo $item->getName();
}
Try this with product all details like name, images etc...
<?php
$customer = Mage::getSingleton('customer/session')->getCustomer();
if($customer->getId())
{
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
echo $item->getName()."</br>";
echo $item->getId()."</br>";
echo $item->getPrice()."</br>";
echo $item->getQty()."</br>";
$item = Mage::getModel('catalog/product')->setStoreId($item->getStoreId())->load($item->getProductId());
if ($item->getId()) :
?>
<img src="<?php echo Mage::helper('catalog/image')->init($item, 'small_image')->resize(113, 113); ?>" width="113" height="113" />
<?php endif; } } ?>

Magento - Product Collection with the current user's Wishlist

Within a Magento php Controller, how can I get a Product Collection containing the products listed in the logged in user's (ie current user's) Wishlist.
I am getting the Wishlist using:
$wishList = Mage::getModel('wishlist/wishlist')->loadByCustomer(Mage::getSingleton('customer/session')->getCustomer());
and this contains the correct number of items.
But I would like to get a Product Collection. I have tried:
$productCollection = $wishList->getProductCollection();
and
$productCollection = $wishList->getProductCollection()->addAttributeToSelect('id')->load();
but the Product Collection I get has a length of 0.
How do I get the Product Collection?
You can use the getWishlistItemCollection (see link for more details) off the wishlist helper to return a collection of items, you then need to get the product from the item.
I have been using the following code to create an associative array of the products, which I then use to determine if a product I am displaying in the list page is in the wishlist...hopefully this will help:
public function getWishList() {
$_itemCollection = Mage::helper('wishlist')->getWishlistItemCollection();
$_itemsInWishList = array();
foreach ($_itemCollection as $_item) {
$_product = $_item->getProduct();
$_itemsInWishList[$_product->getId()] = $_item;
}
return $_itemsInWishList;
}
Try this with product all details like name, images etc...
<?php
$customer = Mage::getSingleton('customer/session')->getCustomer();
if($customer->getId())
{
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
echo $item->getName()."</br>";
echo $item->getId()."</br>";
echo $item->getPrice()."</br>";
echo $item->getQty()."</br>";
$item = Mage::getModel('catalog/product')->setStoreId($item->getStoreId())->load($item->getProductId());
if ($item->getId()) :
?>
<img src="<?php echo Mage::helper('catalog/image')->init($item, 'small_image')->resize(113, 113); ?>" width="113" height="113" />
<?php endif; } } ?>
$customer = Mage::getSingleton('customer/session')->getCustomer();
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
// do things
}

Magento - get cart items for a given product id

I try to get the cart items for a given product;
I have tried this code :
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load('2784');
$quote = Mage::getSingleton('checkout/cart')->getQuote();
$cartItems = $quote->getItemByProduct($product);
foreach ($cartItems as $item) {
echo $item->getId()."<br/>";
}
but it don't gives anything.
How can I modify my code to use "getItemByProduct" in the right form ?
Thanks for help.
getItemByProduct() returns the first matching Mage_Sales_Model_Quote_Item so there is no need for an extra loop.
$item = $quote->getItemByProduct($product);
if ($item !== false) echo $item->getId();
I'd use
foreach ($quote->getItems() as $item) {
if ($item->getProductId() == $product->getId()) {
print_r($item->getData());
}
}
You can not use getItemByProduct() function on the checkout/cart or checkout/quote model as that method is of the sales/quote model.
You can find this method at the Mage_Sales_Model_Quote class. so it is used withe sales/quote. hope this is useful.

How to load products media gallery along with the collection?

Can anybody give me a hint about how to load product's media gallery along with the collection?
I'm getting the collection like this:
$collection = Mage::getModel('catalog/product')->getCollection()
->addStoreFilter($storeId)
->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
foreach ($collection as $product) {
var_dump($product->getMediaGalleryImages());
}
But getMediaGalleryImages() returns null. I know that I can load each product separately with $product = Mage::getModel('catalog/product')->load($product->getId()) but I want to avoid this, because it causing unnecessary workload.
In case anyone’s looking for another approach on this, I found this to work (in just one case so no guarantees!):
Be sure to do $collection->addAttributeToSelect(’image’); first, then when looping through the collection products, do:
$attributes = $product->getTypeInstance(true)->getSetAttributes($product);
$media_gallery = $attributes[’media_gallery’];
$backend = $media_gallery->getBackend();
$backend->afterLoad($product); //this loads the media gallery to the product object
Not sure if all of this is necessary but I’m in a hurry. In my particular case I was trying to get the image url using $product->getImageUrl(); and this approach worked for me.
Hope it helps someone else.
I had to do the same recently, fastest method:
class My_Module_Block_Name extends Mage_Catalog_Block_Product_View_Abstract
{
/** #var null|Mage_Catalog_Model_Resource_Eav_Attribute */
protected static $_mediaGalleryBackend = null;
public function getGalleryImages()
{
$product = $this->getProduct();
$this->_getBackend()->afterLoad($product);
$collection = $product->getMediaGalleryImages();
return $collection;
}
/**
* #return Mage_Catalog_Model_Resource_Eav_Attribute
*/
protected function _getBackend() {
if (self::$_mediaGalleryBackend === null) {
$mediaGallery = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'media_gallery');
self::$_mediaGalleryBackend = $mediaGallery->getBackend();
}
return self::$_mediaGalleryBackend;
}
}
try this
$collection = Mage::getModel('catalog/product')->getCollection()
->addStoreFilter($storeId)
->addAttributeToSelect(array('image', 'media_gallery'))
->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
foreach ($collection as $product) {
var_dump($product->getMediaGallery());
}
It can be used directly in loop:
foreach ($collection as $product) {
$product->getResource()->getAttribute('media_gallery')->getBackend()->afterLoad($product);
foreach ($product->getMediaGalleryImages() as $image) {
var_dump($image->debug());
}
}
You are going to have to use:
// Returns the Media Gallery Images
Mage::getModel(’catalog/product’)->load(productid)->getMediaGalleryImages();
Reference: http://www.magentocommerce.com/boards/viewthread/29639/
The secret sauce when working with custom collections involving products is the third parameter of init method... at least it was for me. This way I don't need to load the whole product and run expensive queries.
So, having my custom $product which represents an instance of Mage_Catalog_Model_Product but from my custom collection, I can do:
(string)Mage::helper('catalog/image')->init($product, 'small_image', $product->getImage())
I also needed to add the image attribute to my custom collection, and I did that by adding ->addAttributeToSelect(['image']) to it.
You can also resize your image accordingly:
(string)Mage::helper('catalog/image')->init($product, 'small_image', $product->getImage())->resize($width, $height=null)
Here is a function to add the media gallery to a collection:
// Source: http://www.magentocommerce.com/boards/viewthread/17414/#t141830
public function addMediaGalleryAttributeToCollection(Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $_productCollection) {
$_mediaGalleryAttributeId = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'media_gallery')->getAttributeId();
$_read = Mage::getSingleton('core/resource')->getConnection('catalog_read');
$_mediaGalleryData = $_read->fetchAll('
SELECT
main.entity_id, `main`.`value_id`, `main`.`value` AS `file`,
`value`.`label`, `value`.`position`, `value`.`disabled`, `default_value`.`label` AS `label_default`,
`default_value`.`position` AS `position_default`,
`default_value`.`disabled` AS `disabled_default`
FROM `catalog_product_entity_media_gallery` AS `main`
LEFT JOIN `catalog_product_entity_media_gallery_value` AS `value`
ON main.value_id=value.value_id AND value.store_id=' . Mage::app()->getStore()->getId() . '
LEFT JOIN `catalog_product_entity_media_gallery_value` AS `default_value`
ON main.value_id=default_value.value_id AND default_value.store_id=0
WHERE (
main.attribute_id = ' . $_read->quote($_mediaGalleryAttributeId) . ')
AND (main.entity_id IN (' . $_read->quote($_productCollection->getAllIds()) . '))
ORDER BY IF(value.position IS NULL, default_value.position, value.position) ASC
');
$_mediaGalleryByProductId = array();
foreach ($_mediaGalleryData as $_galleryImage) {
$k = $_galleryImage['entity_id'];
unset($_galleryImage['entity_id']);
if (!isset($_mediaGalleryByProductId[$k])) {
$_mediaGalleryByProductId[$k] = array();
}
$_mediaGalleryByProductId[$k][] = $_galleryImage;
}
unset($_mediaGalleryData);
foreach ($_productCollection as &$_product) {
$_productId = $_product->getData('entity_id');
if (isset($_mediaGalleryByProductId[$_productId])) {
$_product->setData('media_gallery', array('images' => $_mediaGalleryByProductId[$_productId]));
}
}
unset($_mediaGalleryByProductId);
return $_productCollection;
}
An example of it's usage below:
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
$this->addMediaGalleryToArray($products);

Resources