Magento query how to get category, description and product url
-
11-12-2019 - |
Question
This query work ok:
SELECT
CONCAT(address.firstname,' ', address.lastname) AS Name,
address.email AS Email,
items.created_at AS Date,
items.name AS Description,
items.store_id AS Logon,
items.name AS Category,
items.store_id AS FeedbackDate,
items.sku AS ProductSearchcode,
items.order_id AS Orderref
FROM sales_flat_order AS orders
JOIN sales_flat_order_item AS items
ON items.order_id = orders.entity_id
LEFT JOIN sales_flat_order_address AS address
ON orders.entity_id = address.parent_id
WHERE
items .created_at BETWEEN '2014-04-15 00:00:00' AND '2014-07-30 00:00:00'
AND orders.status = 'complete'
But, I also need to get item category, description and url to product?
Solution
The following should give you the values for the attributes description and url key plus the categories for each product attached to an order.
SELECT
CONCAT(address.firstname,' ',
address.lastname) AS Name,
address.email AS Email,
items.created_at AS Date,
items.store_id AS Logon,
items.name AS Category,
items.store_id AS FeedbackDate,
items.sku AS ProductSearchcode,
items.order_id AS Orderref ,
IFNULL(store_prod_desc.value,main_prod_desc.value) as Description,
IFNULL(store_prod_url.value,main_prod_url.value) as Url,
category_ids.category_id
FROM sales_flat_order AS orders
JOIN sales_flat_order_item AS items
ON items.order_id = orders.entity_id
LEFT JOIN sales_flat_order_address AS address
ON orders.entity_id = address.parent_id
INNER JOIN catalog_product_entity_text as main_prod_desc
ON items.product_id = main_prod_desc.entity_id AND main_prod_desc.attribute_id = 72 AND main_prod_desc.store_id = 0
LEFT JOIN catalog_product_entity_text as store_prod_desc
ON items.product_id = store_prod_desc.entity_id AND store_prod_desc.attribute_id = 72 AND store_prod_desc.store_id = orders.store_id
INNER JOIN catalog_product_entity_varchar as main_prod_url
ON items.product_id = main_prod_url.entity_id AND main_prod_url.attribute_id = 98 AND main_prod_url.store_id = 0
LEFT JOIN catalog_product_entity_varchar as store_prod_url
ON items.product_id = store_prod_url.entity_id AND store_prod_url.attribute_id = 98 AND store_prod_url.store_id = orders.store_id
INNER JOIN catalog_category_product as category_ids
ON items.product_id = category_ids.product_id
WHERE
items .created_at BETWEEN '2014-04-15 00:00:00' AND '2014-07-30 00:00:00'
AND orders.status = 'complete'
Note it does assume that attribute_id 72 is the description and attribute_id 98 is the url_key which is the case for the default attributes
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange