Magento: fetching manufacturer/brand from database - magento

I have this code from Mukesh Chapagain: link here
$_product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$manufacturerName = $_product->getAttributeText('manufacturer');
$manufacturerId = $_product->getManufacturer();
This seems not to pick up the manufacturers even though I have them as attributes. Is it due to the fact that the manufacturer field is a drop-down?
any help in getting manufacturer attribute will be appreciated

To retrieve all Manufactures
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'manufacturer');
foreach ( $attribute->getSource()->getAllOptions(true, true) as $option){
$attributeArray[$option['value']] = $option['label'];
}
foreach($attributeArray as $key=>$val){
echo $val;
}
Get products from Manufactures
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('manufacturer');
$collection->addFieldToFilter(array(
array('attribute' => 'manufacturer', 'eq' =>$designer_id),
));
Get selected manufacturer to products
$_productCollection=$this->getLoadedProductCollection();
foreach ($_productCollection as $_product):
echo $_product->getAttributeText('manufacturer');
endforeach;

Honestly, I cannot tell what is wrong with the code in question.
However I have recently been working on something related - if you're trying to use a collection of products. Rather than trying to fix something which probably isn't broken, think of this as an alternative suggestion.
To start with you would need to download my library of query patterns. It contains a class for drop-down attributes. The following adds a manufacturer_text column to the collection.
$products = Mage::getResourceModel('catalog/product_collection');
Knectar_Select_Product_Values::enhanceProducts($products, 'manufacturer');
foreach ($products as $product) {
echo $product->getManufacturerText(), '<br>';
}

This work for me:
SELECT
CPEI.entity_id,
CPE.sku,
CPEI.attribute_id,
EA.attribute_code,
CPEI.value AS value_id,
EAOV.value
FROM
catalog_product_entity_int AS CPEI
INNER JOIN eav_attribute_option_value AS EAOV ON (EAOV.option_id = CPEI.value AND EAOV.store_id = CPEI.store_id)
INNER JOIN catalog_product_entity AS CPE USING(entity_id)
INNER JOIN eav_attribute_option AS EAO USING(option_id)
INNER JOIN eav_attribute AS EA ON(CPEI.attribute_id = EA.attribute_id)
WHERE
(
CPEI.value IS NOT NULL AND
EA.attribute_code = 'manufacturer' AND
CPEI.store_id = 0
);

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

Magento: How to limit a collection (sales/order) by a certain category?

I'm trying to limit order for admin users by category, I can find the collection of product id by category and collect sales/order but how can I use this with an event observer?
$category_id = 44;
$category = Mage::getModel("catalog/category")->load($category_id);
$products = Mage::getModel("catalog/product")->getCollection()
->addCategoryFilter($category);
Next I collect just the product ids so I can use them:
$product_ids = array();
foreach ($products as $product)
$product_ids[] = $product->getId();
$items = Mage::getModel("sales/order_item")->getCollection()
->addFieldToFilter("product_id", array("in" => $product_ids));
You can use SetPageSize and SetCurPage for that
$col = Mage::getModel('YOUR MODEL')
->getCollection()
->setPageSize(17)
->setCurPage(1);
HTH

Magento: How to filter a collection (sales/order) by a certain category?

I spend a lot of hours to solve this problem, but I don't get it :(
I need a selection of all ordered items from a special category. How can I filter the Collection e.g. for categoryId '44' ?
Here my code:
<?php
require_once '/home/web/public_html/app/Mage.php';
Mage::app();
//$_category = Mage::getModel('catalog/category')->load($category_id);
$salesCollection = Mage::getModel("sales/order")->getCollection();
echo $salesCollection->getSelect();
foreach ($salesCollection as $order) {
$items = $order->getAllItems();
... ?>
Thanks everyone for helping me,
best, Rik
the sales_flat_order_item database table knows nothing about the categories.
So I guess you have to use: $collection->getSelect()->join(.....);
In sales_flat_order_item (Mage::getModel('sales/order')) you can find the product_id.
In catalog_category_product (Mage::getModel('catalog/category_product')) you can find category_id, product_id, position
Now you have to join them...
http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento
Here's one (perhaps) not so elegant approach to doing so...
First grab all products in the category you want
$category_id = 44;
$category = Mage::getModel("catalog/category")->load($category_id);
$products = Mage::getModel("catalog/product")->getCollection()
->addCategoryFilter($category);
Next I collect just the product ids so I can use them
$product_ids = array();
foreach ($products as $product)
$product_ids[] = $product->getId();
Grab all order items where the product id is one of the products from our category
$items = Mage::getModel("sales/order_item")->getCollection()
->addFieldToFilter("product_id", array("in" => $product_ids));
Now fetch all the unique orders referenced by the items
$orders = array();
foreach ($items as $item) {
$order_id = $item->getOrderId();
if (!isset($orders[$order_id]))
$orders[$order_id] = Mage::getModel("sales/order")->load($order_id);
}

magento product collection with specific id

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

Magento 1.6.2 Bestseller products not working

$storeId = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToSelect(array('name', 'price', 'small_image', 'short_description', 'description'))
->addStoreFilter($storeId)
->setPageSize(4)
->setCurPage(1)
->setOrder('ordered_qty', 'desc')->load();
Bestsellers code not working in Magento 1.6.2. The collection is not even filtered by store.
Zend_Debug::dump($storeId); gives me string '2' (length=1) but I can not execute Zend_Debug::dump($products->getSelect()); because it gives me an error like There has been and error processing your request and it give s me the following query:
SELECT SUM(order_items.qty_ordered) AS ordered_qty, order_items.name AS order_items_name, order_items.product_id AS entity_id, e.entity_type_id, e.attribute_set_id, e.type_id, e.sku, e.has_options, e.required_options, e.created_at, e.updated_at, e.name, e.price, e.small_image, e.short_description FROM tp_sales_flat_order_item AS order_items INNER JOIN tp_sales_flat_order AS order ON order.entity_id = order_items.order_id AND order.state <> 'cancelled' LEFT JOIN tp_catalog_product_entity AS e ON (e.type_id NOT IN ('grouped', 'configurable', 'bundle')) AND e.entity_id = order_items.product_id AND e.entity_type_id = 4 WHERE (parent_item_id IS NULL) GROUP BY order_items.product_id HAVING (SUM(order_items.qty_ordered) > 0) ORDER BY ordered_qty desc LIMIT 4
Please help me.
Try this:
<?php
$products = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addOrderedQty($from, $to, true)
->addAttributeToSelect(array('name', 'price', 'small_image'))
->addCategoryFilter($category)
->setStoreId($storeId)
->addStoreFilter($storeId)
->setOrder('ordered_qty', 'desc');
?>
Try this:
$storeId = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel('reports/product_collection')
->addOrdersCount()
->addAttributeToSelect(array('name', 'price', 'small_image', 'short_description', 'description'))
->addStoreFilter($storeId)
->setPageSize(4)
->setCurPage(1)
->setOrder('orders', 'desc')->load();
The changes are the addOrdersCount() method, and the ->setOrder() method.
i was also face this type of issues in magento 1.6.2.
i found that there is some problem in attribute filter while i am filtering collection.
i have replace addAttributeToFilter with addFieldToFilter and it's work.
Try this may be it will be helpful for your
Try to set the storeid for your collection. Insert this line after adding OrderedQty:
->setStoreId($storeId)

Resources