Is there a way in Magento to find all orders that contain a given product? It would be even better if it could be done from the administration panel.
Reports -> Products -> Products Ordered gives me the day the product was sold and how many orders contain it, but I need to know which specific orders include the product.
Thank you!
I answered this question in another question;
Get a list of orders in magento extension that have a certain product
For quick reference:
$productId = {PRODUCT_ID};
$orders = array();
$collection = Mage::getResourceModel('sales/order_item_collection')
->addAttributeToFilter('product_id', array('eq' => $productId))
->load();
foreach($collection as $orderItem) {
$orders[$orderItem->getOrder()->getIncrementId()] = $orderItem->getOrder();
}
You can get by simple mysql query:-
select order_id from sales_flat_order_item where product_id=//given product id
OR
You can customize the reports according to your needs.
If you want to get the increment_id (the incremental Order Number) instead of the order_id, you can simply use this Statement:
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;
Related
I have the following DB structure
Table product (which holds my products information including the stock)
prod_id
prod_name
prod_desc
prod_price
stock
table store_orders_item (which is updated every time a customer buys from the my site)
id
order_id
sel_prod_id
sel_prod_price
sel_prod_qty
I need that every time I make a sale online and store_orders_items is updates, that reduce the stock field of the product sold on the table product
I think i need to use the code below, plus some sort of join or subquery to update my product table according to the prod_id, but i could not figure out how.
UPDATE product SET stock = stock - (SELECT SUM(sel_prod_qty) FROM store_orders_items);
I also tried:
UPDATE product
SET stock = stock - (SELECT s.sel_prod_qty, s.sel_prod_id, p.prod_id
FROM store_orders_items as s left join product as p ON
s.sel_prod_id=p.prod_id WHERE order_id=(SELECT MAX(order_id) from orders_number));
And got this error:
"You can't specify target table 'product' for update in FROM clause"
You must use join updates, for example in MySQL:
UPDATE
product p
INNER JOIN
store_orders_item s
ON
p.prod_id = s.sel_prod_id
SET
p.stock = p.stock - s.sel_prod_qty
WHERE
s.order_id = NNNNN
where NNNNN is the id of order just inserted.
I need to write an SQL query to list a customers prices for our products. We have a standard price list, customer_no = 0, and a customer specific price list, customer_no = XXXX.
I am having trouble understanding how I can get the query to return the customer specific price for a product, if they've been given such, or if not fall back to the standard price.
To get all products and prices on the standard price list
select prices.product_id, products.product_desc, prices.m2
from prices, products
where prices.product_id = products.product_id
and prices.customer_no = 0
order by prices.product_id asc
To get all products and prices that the customer has been specifically quoted for
select prices.product_id, products.product_desc, prices.m2
from prices, products
where prices.product_id = products.product_id
and prices.customer_no = $_SESSION['customer']
order by prices.product_id asc
How can I perform the first query, but if the customer has their own price then replace it with that? Is this even possible?
Thanks in advance.
Steve
Edit: Sorry, missed the third line in both queries in the original post.
You have to join with the prices table twice, once for the list prices and then for the quoted prices. Use a LEFT JOIN for the latter, since some products won't have a quoted price. Then use NVL to default from the quoted price to the list price.
SELECT products.product_id, products.product_desc, NVL(p2.m2, p1.m2)
FROM products
JOIN prices p1 ON p1.product_id = products.product_id
LEFT JOIN prices p2 ON p2.product_id = products.product_id
AND p2.customer_no = $_SESSION['customer']
WHERE p1.customer_no = 0
Try:
select prices.product_id, products.product_desc, prices.m2
from prices, products
where prices.customer_no = nvl($_SESSION['customer'], 0)
order by prices.product_id asc
While i am coding a shopping site, I need to update product stock.
But the thing is, naturally shopping cart can have the same items a couple of times.
What is the best way for updating it?
I tried IN but the following SQL query returns 3 items.
SELECT *
FROM `products`
WHERE id
IN ( 3, 4, 4, 6 )
LIMIT 0 , 30
Here is my solution but, I don't think this is the best one.
$cart = array(1,3,4,4,5,8,22,22);
$itemlist = array_count_values($cart);
foreach($itemlist as $itemid=>$ocurrence){
$SQL = "UPDATE products SET stock = stock-".$ocurrence." WHERE id = ".$itemid;
mysql_query($SQL);
}
You can do something like this:
SELECT * FROM menu WHERE item_id = 1
UNION ALL
SELECT * FROM menu WHERE item_id = 1
UNION ALL
SELECT * FROM menu WHERE item_id = 2
Check this link:
MySQL table -> Can you return the same row multiple times, in the same query?
if possible create a seperate item_cart, table which will have (cart_id, item_id , product_id) as primary key. from here you can get do a group by on product_id to see how many no of product sold.
select product_id, count(product_id) as "No of Product Sold" from item_cart
group by product_id
your php code will update no of products in stock coloumn perfectly.
If possible you try setting triggers for updatin stock columns whenever any product is sold.
I've the following SQL query to retrieve products:
$query = "SELECT *,m.m_name,m.m_website FROM belvg_countdown c
INNER JOIN catalog_product_entity e ON e.entity_id=c.entity_id
LEFT JOIN manufacturers_products mp ON c.entity_id=mp.product_id
LEFT JOIN manufacturers m ON mp.manufacturers_id=m.manufacturers_id
WHERE DATE_FORMAT(expire_datetime_off , '%Y-%m-%d') > DATE_FORMAT(NOW() , '%Y-%m-%d')
AND DATE_FORMAT(expire_datetime_on , '%Y-%m-%d')!=DATE_FORMAT(NOW() , '%Y-%m-%d')
AND entity_type='product'";
Now I'd also add a statement to load only products that are in a category named "featured".
Can you please help me to understand what should I compare/query to get only "featured" products?
Thanks.
See below URL it is very help full to you
Magento ->addCategoryFilter - filtering product collection by root category
or try it
OK, I think this works, haven't tested too much but seems to have done the trick. You need to first get your stores root category id, then join some fields so you have access to the products "category_id", then filter using that:
$_rootcatID = Mage::app()->getStore()->getRootCategoryId();
$_testproductCollection = Mage::getResourceModel('catalog/product_collection')
->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
->addAttributeToFilter('category_id', array('in' => $_rootcatID))
->addAttributeToSelect('*');
$_testproductCollection->load();
foreach($_testproductCollection as $_testproduct){
echo $this->htmlEscape($_testproduct->getName())."<br/>";
};
You don't need all this query to get active product from specificy category.
Use collection.
$producs = Mage::getModel('catalog/category')->load(#categoryid or $_category->getId())
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('is_saleable', 1)
->joinField('is_in_stock', 'cataloginventory/stock_item', 'is_in_stock', 'product_id=entity_id', '{{table}}.stock_id=1', 'left');
Doing an allnighter on a project and my mind is blank atm... Simple question really:
I have two MySQL tables, product and category. Each product belongs to exactly one category. Every category has several products.
SELECT
p.uid as product_uid, p.name_NL as product_name, p.price as product_price,
c.uid as category_uid, c.name_NL as category_name
FROM
product p, category c
WHERE
p.category_uid = c.uid
This gives me a nice overview of all products in their respective category. My question is about outputting this data on the page. I'm aiming for this:
<h1>Category name</h1>
<p>Product in this category</p>
<p>Other product in this category</p>
<h1>Next category</h1>
<p>Product in next category</p>
My mind is completely blank right now. How would one go about doing this?
I would like to avoid doing subqueries (if possible).
Kind regards,
M
What about adding ORDER BY category_uid so that the products are ordered by category in your SQL query. Then using PHP, loop through each product (row) and when you encounter a new category, add a new header.
Example:
<?php
// ...
$previous_category_uid = null;
// Loop through each row.
while ( $row = $result->fetch_assoc() )
{
// If the category UID is not the same as the one from the previous row, add a header.
if ( $previous_category_uid != $row['category_uid'] )
{
echo '<h1>' . $row['category_name'] . '</h1>';
$previous_category_uid = $row['category_uid'];
}
}
The benefit of this method is that you don't have to nest queries. A single query will suffice.
Don't you just need to use a GROUP BY category_uid ?
Generally speaking you have two options:
Get all the data at once (like you are doing currently) then use PHP to either pre-sort the data by category. Then do your output looping over this array. So 1 query, 2 + n loops (where n is the number of categories).
Get all your categories and then loop over those for output. In each iteration you will need to query all products for that loop. So 1 + n queries, 1 + n loops (where, again, n is the number of categories).
Option 2 might be more straightforward, but clearly there are more queries. In the end, it's your call.
Assuming you're looking for alphabetical ordering for category names and products within each category:
SELECT
p.uid as product_uid, p.name_NL as product_name, p.price as product_price,
c.uid as category_uid, c.name_NL as category_name
FROM product p INNER JOIN category c ON p.category_uid = c.uid
ORDER BY category_name, product_name
This also converts your query's Cartesian product and WHERE to an inner join.
To output with the headers you want, just loop over the returned rows, and keep track of the category you're in. Whenever the category of the current row is different from the previous one, you print a new h1 for the new category and update the stored "current" category.