Magento Get Product SELECTED ATTRIBUTE only? - magento

Hi i'm working on an external application thats shows all the ordered items on a magento store.
The query that display the ordered product attribute is this :
select group_concat(distinct(b.value) separator '<br/>') from catalog_product_entity_varchar a , eav_attribute_option_value b , eav_attribute c
where a.value = b.option_id
and c.attribute_id = a.attribute_id
and c.is_user_defined = 1
and a.entity_id = PRODUCT_ID
This is for the attributes with type VARCHAR , i use the same query to get attributes of int and text. I just change the table name from catalog_product_entity_varchar to catalog_product_entity_int and catalog_product_entity_text.
The problem that i have is that i GET all the product attributes , manufacturer , supplier... and due to the fact that we got many stores i dont want to retrieve all the additional attributes with an additional sql where clause!
Any solution to get only selected attributes?

The below will grab the collection in PHP. You just need to change the Attribute that you select on. By adding the ->addOrderedQty, it allows the ability to get the ordered qty. Now I understand that you are using an external application. The reason that I post this is that you can load the Mage.php into your application and use this directly or you can just run this once and watch for the query in your database and then use that query to accomplish what you are looking to.
$visibility = array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
);
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addOrderedQty()
->addAttributeToFilter('visibility', $visibility)
->setOrder('ordered_qty', 'desc');

Related

I cannot find certain fields on the MAGENTO database

I was trying to find this fields because i want to obtain all the products and then, make a query but i was not able to do that(they dont appear in the database diagram for Magento 1.9):
sku, code_brand, code_business_unit, code_line, code_group, code_business, min_sale_qty
This is the diagram:
https://anna.voelkl.at/uploads/magento/ce1922.png
I only found the fields "SKU" and "min_sale_qty". What happened to the others?
Do these fields : code_brand, code_business_unit, code_line, code_group, code_business even exist or they were created by someone?
Anyway, i am using MYSQL connected to the MAGENTO database.
I suppose that code_brand, code_business_unit, code_line, code_group, code_business are custom product attributed that you added right?
When you add a custom attribute, Magento add a row in the table eav_attribute.
There we can find attribute_id and backend_type.
For example if code_brand is a integer the backend_type should be int. So in catalog_product_entity_int we can find rows with the same attribute_id set in the eav_attribute table.
Here you are a sumple query to search that:
SELECT
*
FROM
catalog_product_entity_int
WHERE
attribute_id IN (
SELECT
attribute_id
FROM
eav_attribute
WHERE
attribute_code = 'code_brand'
);

Set Include in Navigation Menu to Yes in database

I want to set Include in Navigation Menu for all categories to Yes.
Can anybody tell me in which table this value is?
Magento use an eav model to save values in the database.
You have to search an attribute called "include_in_menu" in the "eav_attribute" table.
This attribute have an "attribute_id" which will be retrieve in the other tables.
On my installation, this attribute has attribute_id = 67 which is stored as an INTEGER (int)
On magento all the attributes have a type which you can find on the eav_attribute table.
You want to update your categories, we have to update the table where the integer attribute "include_in_menu" of the categories is saved.
As you can see in your database, you have a lot of tables for the categories :
catalog_category_entity, catalog_category_entity_datetime, catalog_category_entity_decimal, catalog_category_entity_int...
You have to select all your categories from the first table : select entity_id from catalog_category_entity
After you have to select the attribute_id in the table related to the good attribute type. Here is "catalog_category_entity_int" for all the INTEGER attribute...
select value from catalog_category_entity_int where attribute_id = 67 and store_id = ...
Be careful if you have multiple stores...All the stores are on the same table.
And now you just have to update value to "1" on the selected rows.
Sorry for my English, i m french...
Best regards
Cédric
This answer worked on Magento EE 2.2.2.
Based on Cedric's answer I used the following query to remove all of the categories from the menu via Sequel Pro.
You can remove all of your categories from your menu by running:
UPDATE catalog_category_entity_int SET value='0' WHERE attribute_id='67'
You can add all of your categories to your menu by running:
UPDATE catalog_category_entity_int SET value='1' WHERE attribute_id='67'
Then flush your cache (php bin/magento cache:flush) and you should see your changes on the frontend.
Again, be careful of what 'store_id' you're affecting.
You can use following query to update all categories:
update catalog_category_entity_int cci
inner join eav_attribute a on a.attribute_id = cci.attribute_id
set cci.value = 1
where a.attribute_code = 'include_in_menu';

Magento: Query Database for products with no assigned categories

I'm really new to Magento and I'm looking to find a way to get a list of all the products in the catalog which are not assigned to any categories. Can anyone offer any help as to how this can be achieved?
Many thanks.
Select entity_id from catalog_product_entity where entity_id not in (select distinct product_id from catalog_category_product);
This will give you all product entity id not belonging to any category.
You can get product collection by using following code :
$product = Mage::getModel('catalog/product');
$productCollection = $product->getCollection()
->addAttributeToSelect('*');
foreach ( $productCollection as $_product ) {
echo $_product->getName().'<br/>';
}
But for your requirement you can get idea from following links,may be its help you.
How do I get product category information using collections in Magento

Magento Load Collection Using Order By

My query is generating from magento collection is
$userModel = Mage::getSingleton('user/user')->getCollection()->addFieldToFilter('user_id', array('in' => array($User_Id)));
SELECT `main_table`.user_name FROM `user_table` AS `main_table` WHERE (user_id IN('1', '3', '2'));
I want to use this query:
SELECT `main_table`.user_name FROM `user_table` AS `main_table` WHERE (user_id IN('1', '3', '2')) ORDER BY FIELD(user_id , 1,3,2);
So that it will display the user name in the same order as ID's are input.
i.e. "1,3,2"
Which method is used in Magento to load collection using ORDER BY?
Thanks in advance!
You can get Zend_Db_Select from Collection and then add order to Zend_Db_Select like this question:
Zend DB Select : ORDER BY FIELD('id',some_array) - how?

Magento filter order collection by product sku

I'm using magento 1.7 and I need to get all orders from certain increment_id containing at least one item matching a sku.
Here's what I have:
$orderCollection = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('status',
array(
'nin' => array(
'new',
'pending',
'pending_payment',
'holded',
'canceled')
))
->addFieldToFilter('increment_id', array('gteq' => $last_order));
If I add a line with:
$orderCollection->addFieldToFilter('sku', $findSku);
I will get a PHP fatal error since 'sku' is not a field. I've tried addAttributeToFilter() and it won't work either.
I know I need to build a join with another table, but I don't know how joins are made in Magento and I don't know which table I should join to.
Any help will be greatly appreciated.
SKUs are placed in order item table not in orders.
Your final query must look like this one:
SELECT o.increment_id
FROM sales_flat_order_item oi
INNER JOIN sales_flat_order o ON o.entity_id = oi.order_id
WHERE product_id=XXX
ORDER BY o.increment_id DESC;
This query can be done using nearly such syntax:
$orderItem = Mage::getModel('sales/order_item')->getCollection();
$orderItem
->getSelect()
->joinInner(array('order' => Mage::getSingleton('core/resource')->getTableName('sales/order')), 'order.entity_id = main_table.order_id' )
->where('product_id=?', $productId)
->order('main_table.order_id DESC');

Resources