Get all existing values of attribute in the eav table - magento

I have the attribute "country" which is varchar, it is set in every product that I have.
I need to have a list of all the values that are set for this attribute thought all the products. Basically what I want to do is the equivalent of this query:
SELECT `value` FROM `catalog_product_entity_varchar` WHERE attribute_id=147
How to do this the magento way?
The thing I am doing at the moment is to get the collection of all the products and loop through them, which is not an option.

Check below code:-
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'color');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}

What about this?
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('sku');
$collection->addAttributeToSelect('country');
foreach ($collection as $product) {
// ... Your stuff here
}

Related

How to get a list of skus in magento

How to get a list of skus whiout using the looping in magento.
Example: I am using below code with my conditons.
$productsCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('sku');
Now I want to result as array('A001','A002'....) etc.
I don't want to iterate (loop) the product collection.
Please suggest.
If you want to retrieve the collection in that way, you will have to loop through the collection and retrieve the sku.
$skus = array();
foreach ($productsCollection as $product) {
$skus[] = $product->getSku();
}
If you don't want that, you can just use a simple query because the SKU is kept in the catalog_product_entity table.
$conn = Mage::getSingleton('core/resource')->getConnection('core_write');
$table = 'catalog_product_entity';
$q = "SELECT sku FROM {$table}";
$list = $conn->fetchOneFieldAll($q, 'sku');
Retrieve the read connection you should use ->getConnection('core_read') not ->getConnection('core_write'). Whole codes is below which runs faster.
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$skus = $readConnection->fetchCol('SELECT sku FROM `catalog_product_entity`');
foreach ($skus as $sku) {
echo $sku;
}

get data for current product from custom table magento

I have created custom table and model, I can access all data by this model using this code
$model = Mage::getModel('groupprice/groupprice');
$data = $model->getCollection()->getData();
print_r($data);
I just want to filter data only for current product, foreign key in custom table for product is product_id .
How I can achieve this?
Try this:
$productId = 1;//change to your product id
$collection = Mage::getModel('groupprice/groupprice')->getCollection()
->addFieldToFilter('product_id', $productId);
foreach ($collection as $item) {
//do something with $item
//for example print_r($item) to see all it's data
}

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 - get a list of bundled product ids from a product id

Lets say I load my product object:
$product = Mage::getModel('catalog/product')->load($productId);
Is there a function or some way to extract the bundled ids related to this product?
e.g.
$product->getBundledProductIDs()
The following should work:
$product->getTypeInstance(true)->getChildrenIds($product->getId(), false)
The result is a multi-dimensional array with the top level being options and the children of options being products.
Also, you can change the false to a true and it will only return required options of the bundle.
Try this-
$collection = $product->getTypeInstance(true)
->getSelectionsCollection(
$product->getTypeInstance(true)
->getOptionsIds($product), $product);
foreach ($collection as $item) {
# $item->product_id has the product id.
}

Finding all products with the same attribute on Magento

I'm trying to do something that would seem simple but just doesn't work
I'm running Magento 1.5.1.
I would like to show on the product page all the items that have the same attribute set as the original item.
For example:
Item A has XYZ attributes, show me all the other items that have xyz attributes.
Nothing I've tried worked so far...
EDIT:
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('orig_price');
//filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B
$collection->addFieldToFilter(array(
array('name'=>'orig_price','eq'=>'Widget A'),
array('name'=>'orig_price','eq'=>'Widget B'),
));
foreach ($collection as $product) {
//var_dump($product);
var_dump($product->getData());
}
EDIT 2:
I tried :
$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('color');
//filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B
$collection->addAttributeToFilter(array( array('name'=>'color','eq'=>'red')
));
foreach ($collection as $product) { //var_dump($product);
var_dump($product->getData()); }
and nothing at all showed up
First you should check that the attribute value for 'Used in Product Listing' is set to yes and than try the code below. I feel it will help you.
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('color', 'red');
foreach ($collection as $product) { //var_dump($product);
var_dump($product->getData()); }
once you get the filtered data than add other select & filter attribute as per your requirement.

Resources