Get order items (products) by Order number - magento

Here I show how to get items from a Order by the order number id:
$order = Mage::getModel('sales/order')->load($order_id);
$items = $order->getAllItems();
$itemcount = count($items);
$name = array();
$unitPrice = array();
$sku = array();
$ids = array();
$qty = array();
foreach ($items as $itemId => $item) {
$name[] = $item->getName();
$unitPrice[] = $item->getPrice();
$sku[] = $item->getSku();
$ids[] = $item->getProductId();
$qty[] = $item->getQtyToInvoice();
}
How can I do the same but in SQL, get all the items information (name, price, image..) by order number id.

// getSelect() method allowed for order collection only so you can print query for specific order as as below.
$order_id = 12345;
$orders = Mage::getResourceModel('sales/order_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', $order_id);
echo $orders->getSelect();

Use sql query like :
This is for select item name and data
select sales_flat_order.entity_id,sales_flat_order.increment_id,sales_flat_order_item.item_id, sales_flat_order_item.sku, sales_flat_order_item.name from sales_flat_order right join sales_flat_order_item on sales_flat_order.entity_id = sales_flat_order_item.order_id where increment_id = 100000042

Related

How to get a list of skus in magento

How to get a list of skus whiout using the looping in magento.
Example: I am using below code with my conditons.
$productsCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('sku');
Now I want to result as array('A001','A002'....) etc.
I don't want to iterate (loop) the product collection.
Please suggest.
If you want to retrieve the collection in that way, you will have to loop through the collection and retrieve the sku.
$skus = array();
foreach ($productsCollection as $product) {
$skus[] = $product->getSku();
}
If you don't want that, you can just use a simple query because the SKU is kept in the catalog_product_entity table.
$conn = Mage::getSingleton('core/resource')->getConnection('core_write');
$table = 'catalog_product_entity';
$q = "SELECT sku FROM {$table}";
$list = $conn->fetchOneFieldAll($q, 'sku');
Retrieve the read connection you should use ->getConnection('core_read') not ->getConnection('core_write'). Whole codes is below which runs faster.
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$skus = $readConnection->fetchCol('SELECT sku FROM `catalog_product_entity`');
foreach ($skus as $sku) {
echo $sku;
}

Magento: How to limit a collection (sales/order) by a certain category?

I'm trying to limit order for admin users by category, I can find the collection of product id by category and collect sales/order but how can I use this with an event observer?
$category_id = 44;
$category = Mage::getModel("catalog/category")->load($category_id);
$products = Mage::getModel("catalog/product")->getCollection()
->addCategoryFilter($category);
Next I collect just the product ids so I can use them:
$product_ids = array();
foreach ($products as $product)
$product_ids[] = $product->getId();
$items = Mage::getModel("sales/order_item")->getCollection()
->addFieldToFilter("product_id", array("in" => $product_ids));
You can use SetPageSize and SetCurPage for that
$col = Mage::getModel('YOUR MODEL')
->getCollection()
->setPageSize(17)
->setCurPage(1);
HTH

How to get sub-categories of two parent category

I had a main category (parent category) whose id = 5 & 37. I want to collection of its sub-categories. How can I do that?
$catid = array(5,37);
$_category = Mage::getModel('catalog/category')->load(5);
$_subcategories1 = $_category->getChildrenCategories();
$_category = Mage::getModel('catalog/category')->load(37);
$_subcategories2 = $_category->getChildrenCategories();
i want the collection which have children categories from both category id(5,37)
You can get that from one select:
$subcategories = Mage::getModel('catalog/category')
->setStoreId(Mage::app()->getStore()->getId())
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('parent_id', array(5, 37))
->setOrder('parent_id', 'ASC');//if you want them somehow grouped by parent_id
foreach ($subcategories as $category){
//do something with $category
}
here i am giving you one example to merge two collection of category in to one collection
$storeId = Mage::app()->getStore()->getId();
$categoryOneId = 5;
$categoryTwoId = 37;
$categoryOne = Mage::getModel('catalog/category')->load($categoryOneId);
$categoryTwo = Mage::getModel('catalog/category')->load($categoryTwoId);
$collectionOne = Mage::getModel('catalog/product')->getCollection()
->setStoreId($storeId)
->addCategoryFilter($categoryOne);
$collectionTwo = Mage::getModel('catalog/product')->getCollection()
->setStoreId($storeId)
->addCategoryFilter($categoryTwo);
$merged_ids = array_merge($collectionOne->getAllIds(), $collectionTwo->getAllIds());
$mergedCollection = Mage::getModel('catalog/product')->getCollection()
->addFieldToFilter('entity_id', $merged_ids);
hope this will sure help you.

Magento: Proper way to iterate over a Varien Object collection

If I were to create a collection that would retrieve all products belonging to category with a given ID, as below:
$storeId = Mage::app()->getStore()->getId();
$product = Mage::getModel('catalog/product');
$category = Mage::getModel('catalog/category')->load(39);
$catName = $category->getName();
$visibility = array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
);
$products = $product->setStoreId($storeId)
->getCollection()
->addAttributeToFilter('visibility', $visibility)
->addCategoryFilter($category)
->addAttributeToSelect(array('name'), 'inner')
->setOrder('name', 'asc')
;
$collection = $products;
How would I then iterate over the collection and access each item's data, item being a product in this case.
I would have expected something like the below to give me each items available data but doesn't seem to work:
foreach ($collection as $key => $value) {
var_dump($value->getData());
}
You're close. Try doing
foreach ($collection as $obj) {
echo $obj->getName();
}

Magento Get List of SKUs in Category

I know Magento categories can be attained with
$categories = Mage::getResourceModel('catalog/category_collection')
but then how do I get a list of these categories and their respective item SKUs?
If you simply require an array containing the category id and an array of skus...
$skuArray = array();
$categoryCollection = Mage::getModel('catalog/category')->getCollection();
foreach ($categoryCollection as $category) {
$skuArray[$category->getId()] = $category->getProductCollection()->getColumnValues('sku');
}
Alternatively, add a new field to each of the collection objects to contain the skus...
$categoryCollection = Mage::getModel('catalog/category')->getCollection();
foreach ($categoryCollection as $category) {
$skus = $category->getProductCollection()->getColumnValues('sku');
$category->setData('skus', $skus);
}
This would be an option if you have further work to do on the collection later in your code and you still need to access the product sku array.
foreach($categoryCollection as $category) {
$categorySkus = $category->getData('skus');
}
You can use :
$categories = Mage::getResourceModel('catalog/category_collection');
foreach ($categories as $category) {
$products = $category->getProductCollection();
foreach ($products as $product) {
$sku[$product->getId()] = $product->getSku();
}
}
I store all the sku in $sku as an array. Yours may be different.

Resources