Magento: Query Database for products with no assigned categories - magento

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

Related

To get the max entity_id in customer_address_entity table in magento

I am new in magento , if anyone know how to get the max entity_id in customer_address_entity table please reply soon
By using mysql
select entity_id from customer_address_entity order by entity_id desc
By using Magento Collection
echo "<pre>";
$collection = Mage::getModel('customer/customer')->getCollection()->getLastItem();
print_r($collection->getData());

Magento The Amount of times a product has been ordered

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

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

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