Magento The Amount of times a product has been ordered - magento

I want to show the products that have been purchased the most
so far i am using
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addOrderedQty()
->addAttributeToFilter('visibility', $visibility)
->setOrder('ordered_qty', 'desc');
BUT I want to count the amount of time the product has been purchased, disregarding the qty purchased. EG
a customer buys 20 X PRODUCT A (Count this as 1)
a customer buys 1 X PRODUCT B (Count this as 1)
This is because some products are bought in huge quantities so bestsellers data doesn't really reflect our most popular products.
Is this data available?
Cheers

If you have the access of MySQL database then you can easily get the list by query
SELECT sku, count(*)as qty FROM sales_flat_order_item
GROUP BY sku
ORDER BY qty desc
or you can also use Magento Resource Model to get the same
$query = Mage::getResourceModel('sales/order_item_collection');
$query->getSelect()->reset(Zend_Db_Select::COLUMNS)
->columns(array('sku','COUNT(*)'))
->group(array('sku'));

Related

Eloquent query distinct sub id

I have these models
I want to make a query that shows me all the products whose stock quantity> 0 and that does not repeat the products.
My query:
$stock_products_limit = Stock::distinct('product_id')->where('quantity', '!=', 0)->get();
This would be much easier using a size chart relating it to stocks ... but for now I don't have it
I need the model to return me, and then do a foreach:
#foreach($stock_products_limit as $stock_product)
#foreach($stock_product->product->product_images as $i=>$product_image)
...
#endforeach
...
#enforeach
In my models I have the hasMany and belongsTo relations made
How could I make the query? I've been trying the distinct, group by ... but nothing works for me. It only removes the ones with quantity 0 and repeats the product ID ...
Example of the query I want:
SELECT DISTINCT(stocks.product_id)
FROM stocks
INNER JOIN products ON stocks.product_id = products.id
WHERE quantity != 0
ORDER BY product_id
LIMIT 10;
Another example query (but LIMIT doesn't work with IN)
SELECT * from products where id in (SELECT DISTINCT(product_id)
FROM stocks
INNER JOIN products ON stocks.product_id = products.id
WHERE quantity != 0
ORDER BY product_id)
Instead of making the Stock model as the starting point, you might want to use the Product model. Then you don't even have to think about using DISTINCT. Let's use whereHas
return Product::whereHas('stocks', function ($query) {
$query->where('quantity', '>', 0);
})
->limit(10)
->get();

how to get the count of customers and display them against product?

I am able to get the count of customers against the product but for me it is difficult to display like how i have mentioned in the above image.
You could use a left join and count function:
select p.product,
count(c.name) as cust_count
from product p
left join customer c
on p.product = c.product
group by p.product;
This is assuming you need zero count for the products that don't have any customers.
If that's not the case, you can use simple aggregation:
select product,
count(*) as cust_count
from customer
group by product;

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

get all users shopping cart

I'm trying to migrate all user's carts to another system.
Is there a way to obtain every item in the shopping cart for every user?
What I need is only the product and customer IDs
I think when you say shopping cart items you mean order items right??
I'll do this with sql because you need to migrate the info, if you need as collection to use on magento let me know.
SELECT o.customer_id , p.name
FROM sales_flat_quote as o
LEFT JOIN sales_flat_quote_item as i ON i.quote_id = o.entity_id
LEFT JOIN catalog_product_flat_1 as p ON i.product_id = p.entity_id
$list = Mage::getModel('sales/quote')->getCollection();
foreach($list as $li){
$itens = $li->getItemsCollection(); //here we have the itens;
$li->getCustomer()->getName; // here we have the customer name.
foreach($itens as $i){
$i->getName();//itens name;
}
}
You should take a look at quote tables: sales_flat_quote, sales_flat_quote_item and sales_flat_quote_item_option

Magento Get Product SELECTED ATTRIBUTE only?

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');

Resources