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.
Related
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
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;
}
Hi i want to load multiple category id in magento, i used in this but it fetch only 1st category of subcategory not rest of category.
$category = $model->load(79,80,91);
You can use this:
$categories = array(1,2,3);
$category = Mage::getModel('catalog/category')->getCollection()->addAttributeToFilter('entity_id', array('in'=>$categories));
foreach($category as $categorys) {
//or do Somthing
}
you have to use collection instead of load
$collection = Mage::getModel('catalog/category')->getCollection()
->setStoreId(Mage::app()->getStore()->getId())
->addAttributeToSelect('name')
->addIdFilter(array(79,80,91))
->addAttributeToFilter('is_active', 1)//get only active categories if you want
->addAttributeToSort('position', 'desc'); //sort by position
and then you can use loop throw
foreach($collection as $category) {
echo $category->getName()
}
hope this will work for you.
you can use this,
$categories = array(10,13);
$_category = Mage::getModel('catalog/category');
$cats = $_category->getCollection()->addAttributeToFilter('entity_id', array('in'=>$categories));
foreach($cats as $cat) {
Zend_Debug::dump($cat);
//or
// do Somthing
}
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
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();
}