The following code gets me all the manufacturers in all my stores:
$attribute = Mage::getSingleton('eav/config')->addStoreFilter($storeId)->getAttribute('catalog_product', 'manufacturer');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}
However I have multiple stores and I only want the manufacturers for my chosen store id.
I've tried adding store filters to this but with no luck.
Any one have any ideas how to filter this by store?
$product = Mage::getModel('catalog/product');
$storeId = Mage::app()->getStore()->getId();
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
->setStoreId($storeId);
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter('attribute_code', 'manufacturer') // This can be changed to any attribute code
->load(false);
$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
/* #var $attribute Mage_Eav_Model_Entity_Attribute */
$manufacturers = $attribute->getSource()->getAllOptions(false);
return $manufacturers;
This should do it. You can lower the attribute to select on.
$product = Mage::getModel('catalog/product');
$products = $product->setStoreId($storeId)->getCollection()
->addAttributeToSelect(array('name', 'price', 'small_image','short_description','manufacturer'), 'inner');
$this->setProductCollection($products);
See this blog post: http://www.sharpdotinc.com/mdost/2009/04/06/magento-getting-product-attributes-values-and-labels/
Go to the section labled "How to get Only the Attribute Values that have been used on products"
I got the list of manufacturer from the current store using product count
public function getAllManu()
{
$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter('attribute_code', 'manufacturer'); //can be changed to any attribute
$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
$attrMenuItems = $attribute->getSource()->getAllOptions(false);
foreach ($attrMenuItems as $key => $value) {
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addFieldToFilter(array(array('attribute' => 'manufacturer', 'eq' => $value['value'])));
$numberOfProducts = count($collection);
$attrMenuItems[$key]['products_count'] = $numberOfProducts;
}
return $attrMenuItems;
}
Related
I have product:
<?php $_helper = $this->helper('catalog/output'); ?>
<?php $_product = $this->getProduct(); ?>
I have name (not ID) of attribute group.
I need to list all attributes names and values in this attribute group (does not matter on attribute set).
How can i get attributes and values from attribute group, if i know only product and a attribute group name?
$setId = $_product->getAttributeSetId(); // Attribute set Id
$groups = Mage::getModel('eav/entity_attribute_group')
->getResourceCollection()
->setAttributeSetFilter($setId)
->setSortOrder()
->load();
$attributeCodes = array();
foreach ($groups as $group) {
if($group->getAttributeGroupName() == 'Somename'){ // set name
//$groupName = $group->getAttributeGroupName();
//$groupId = $group->getAttributeGroupId();
$attributes = Mage::getResourceModel('catalog/product_attribute_collection')
->setAttributeGroupFilter($group->getId())
->addVisibleFilter()
->checkConfigurableProducts()
->load();
if ($attributes->getSize() > 0) {
foreach ($attributes->getItems() as $attribute) {
/* #var $child Mage_Eav_Model_Entity_Attribute */
$attributeCodes[] = $attribute->getAttributeCode();
}
}
}
}
print_r($attributeCodes);
I'm trying to build an array of orders => products that I can use in reporting/updating attributes. The format I'm going for is:
//$orders[<number of orders>] = <array of product ids with this many orders>
$orders = array(
1 => array(1, 2, 3),
2 => array(4, 5)
//etc
);
So far the best I can do is
$productCollection = Mage::getModel('catalog/product')
->addAttributeToSelect("sku")
->getCollection();
$orders = array();
foreach ($productCollection as $product) {
$ordered = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToFilter('sku', $product->getSku())
->setOrder('ordered_qty', 'desc')
->getFirstItem();
$qtyOrdered = $ordered->getOrderedQty();
$total = $this->_counter - (int)(!$ordered ? 0 : $qtyOrdered);
if (!is_array($orders[$total])) {
$orders[$total] = array();
}
$orders[$total][] = $product->getId();
}
But this is obviously using a lot of resources, loading the entire product collection.
I think I can get by just using
$ordered = Mage::getResourceModel('reports/product_collection')
->addOrderedQty();
But I'm having trouble returning/iterating through the results. How do I extract the information I'm looking for?
This should be better.
$orderItems = Mage::getResourceModel('sales/order_item_collection');
$result = array();
foreach ($orderItems as $item) {
$result[$item->getOrderId()][] = $item->getProductId();
}
print_r($result);
NOTE: take care about hidden order item (Ex: simple, configurable product)
$ordered = Mage::getResourceModel('reports/product_collection')
->addOrderedQty();
foreach($ordered as $order) {
$qtyOrdered = (int)$order->getOrderedQty();
if (!is_array($totals[$qtyOrdered])) {
$totals[$qtyOrdered] = array();
}
$totals[$qtyOrdered][] = $order->getEntityId();
}
Works, but it seems to be returning product Ids that don't exists anymore.
I try to get the products which have been bought.
This is my code:
/** #var $order Mage_Sales_Model_Order */
$order = $eventObserver->getOrder();
/** #var $items Mage_Sales_Model_Resource_Order_Collection */
$items = $order->getItemsCollection(array(), TRUE);
/** #var $item Mage_Sales_Model_Order_Item */
foreach($items as $item) {
$product = $item->getProduct();
var_dump($product->getData('language'));
}
Language is a custom attribute. In this case it is empty, and I have no idea why. All default attributes, like name, id or sku are working.
This language is used as configurable attribute.
What have I to do, to get the value?
Try this:
$items = $order->getAllVisibleItems()
Got it!
When I take the product ID and load the product again:
$product = $item->getProduct();
$product = Mage::getModel('catalog/product')->load( $product->getId() );
I get the language as number.
I've a list of product Ids and based on that product Id I want to get product Name but as the catalog_product_entity table doesn't contain the product name only sku, I'm trapped.
kindly check my code, I've tried left and right join both but not getting the name
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('name')
->getSelect()->joinRight(
array('table_alias'=>'my_table'),
'entity_id = table_alias.product_id',
array('table_alias.*')
);
$result = null;
foreach ($collection as $temp) {
$result[] = array(
'name' => $temp->getCustomer_name(),
);
}
If you want your collection to load the product name, then the first part of your code is right. Here's a simplified version:
$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('name');
$result = array();
foreach ($collection as $temp) {
$result[] = array('name' => $temp->getName());
}
this is how I solved it
$resource = Mage::getSingleton('core/resource');
$readResource = $resource->getConnection('core_read');
$tableName = $resource->getTableName('myTable');
$results = $readResource->select('table_alias.*')
->from($tableName)
->joinLeft(
array('table_alias'=>'catalog_product_flat_1'),
'product_id = table_alias.entity_id',
array('table_alias.*')
)->where('customer_id=?',$customerId);
$products=$readResource->fetchAll($results);
$dataList = array();
foreach($products as $row)
{
$dataList[] = array('productName'=>$row['name'],'price'=> $row['price'],'status'=>$row['status'],'myTable_id'=>$row['myTable_id']);
}
I am working in a new "Filter by Product" module in Magento, i have a situation where i should retrieve all attributes and their values. I Googled this and found the below code
$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->load();
// ->addFieldToFilter('attribute_code', 'color')
$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
/* #var $attribute Mage_Eav_Model_Entity_Attribute */
$attr = $attribute->getSource()->getAllOptions(true);
foreach ($attr as $att) {
echo " Label : ".$att['label']." Value : ".$att['value']."";
}
but this retrieves only the label and value of last attribute from list of all available attributes.
how to i get all the attributes? what am i doing wrong in this code?
Thanks,
Balan
Try this:
$attributes = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection();
// Localize attribute label (if you need it)
$attributes->addStoreLabel(Mage::app()->getStore()->getId());
// Loop over all attributes
foreach ($attributes as $attr) {
/* #var $attr Mage_Eav_Model_Entity_Attribute */
// get the store label value
$label = $attr->getStoreLabel() ? $attr->getStoreLabel() : $attr->getFrontendLabel();
echo "Attribute: {$label}\n";
// If it is an attribute with predefined values
if ($attr->usesSource()) {
// Get all option values ans labels
$options = $attr->getSource()->getAllOptions();
// Output all option labels and values
foreach ($options as $option)
{
echo " {$option['label']} (Value {$option['value']})\n";
}
}
else
{
// Just for clarification of the debug code
echo " No select or multiselect attribute\n";
}
}
This is the first solution:
$products = Mage::getModel('catalog/product')->getCollection();
foreach($this->products as $product) {
$prod = Mage::getModel('catalog/product')->load($product->getId());
}
If you are using using the data from outside magento, you can use a class I made:
http://blog.alexparadise.com/magento-dbeav-class/
It's beta but that should work in your case.
Or get the concept of the EAV table... and make your own SQL query.