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;
}
Related
I've written a method that I'm calling from my list.phtml file.
The logic should simply output the number of products in the currently viewed category and the highest and lowest prices. Pretty straight forward really.
For reasons beyond my understanding it's always returning that the current category contains no products, and I can't figure out why but I presume it's because I'm not fetching the product collection correctly.
Here's the code for my method.
public function writeCatInfo(){
if(Mage::registry('current_category')){
$_productCollection=$this->getLoadedProductCollection;
$catname = Mage::registry('current_category')->getName();
$priceArray = array();
foreach ($_productCollection as $_product){
$priceArray[] = $_product->getSpecialPrice();
}
$numItems = count($_productCollection);
$highPrice = number_format(max($priceArray),2);
$lowPrice = number_format(min($priceArray),2);
$infostring = ucwords($catname)." contains ".$numItems." products with prices ranging from £".$lowPrice." to £".$highPrice;
echo $infostring;
}
}
this will help you to get current category product collection.
$category_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
$category = Mage::getModel('catalog/category')->load($category_id);
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addCategoryFilter($category)
->setOrder('price', 'ASC')
->load();
Turns out my helper was extending the wrong class - Mage_Core_Helper_Abtract instead of Mage_Catalog_Block_Product_List. Changing this fixed the problem.
Please see the below code to get product collection by Category id
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*') // add all attributes - optional
->addAttributeToFilter('status', 1) // enabled
->addAttributeToFilter('visibility', 4) //visibility in catalog,search
->setOrder('price', 'ASC'); //sets the order by price
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
I spend a lot of hours to solve this problem, but I don't get it :(
I need a selection of all ordered items from a special category. How can I filter the Collection e.g. for categoryId '44' ?
Here my code:
<?php
require_once '/home/web/public_html/app/Mage.php';
Mage::app();
//$_category = Mage::getModel('catalog/category')->load($category_id);
$salesCollection = Mage::getModel("sales/order")->getCollection();
echo $salesCollection->getSelect();
foreach ($salesCollection as $order) {
$items = $order->getAllItems();
... ?>
Thanks everyone for helping me,
best, Rik
the sales_flat_order_item database table knows nothing about the categories.
So I guess you have to use: $collection->getSelect()->join(.....);
In sales_flat_order_item (Mage::getModel('sales/order')) you can find the product_id.
In catalog_category_product (Mage::getModel('catalog/category_product')) you can find category_id, product_id, position
Now you have to join them...
http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento
Here's one (perhaps) not so elegant approach to doing so...
First grab all products in the category you want
$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();
Grab all order items where the product id is one of the products from our category
$items = Mage::getModel("sales/order_item")->getCollection()
->addFieldToFilter("product_id", array("in" => $product_ids));
Now fetch all the unique orders referenced by the items
$orders = array();
foreach ($items as $item) {
$order_id = $item->getOrderId();
if (!isset($orders[$order_id]))
$orders[$order_id] = Mage::getModel("sales/order")->load($order_id);
}
I am trying to get product price and description for different stores in magento, I can achieve this as follows:-
foreach ($productObj->getStoreIds() as $_storeId) {
$tempStoreObj = new Mage_Core_Model_Store();
$tempStoreObj->load($_storeId);
$tempProductObj = new Mage_Catalog_Model_Product();
$tempProductObj->setStoreId($_storeId);
$tempProductObj->load($productObj->getId());
$tempPriceArray[] = array(
'websiteId' => $tempStoreObj->getWebsiteId(),
'price' => $tempProductObj->getPrice(),
'baseCurrency' => $tempStoreObj->getBaseCurrencyCode(),
);
$tempDescArray[]=array(
'descprition' => $tempProductObj->getData('description'),
'shortDescription' => $tempProductObj->getData('short_description'),
);
}
Now In the above code there, I have first fetched stores for particular product, then loaded the stores then again created an object for product and loaded w.r.t product id and store id , in this way i have achieved the required task.
Now my problems start here when there are many products and many stores performance issue comes in and the loading process makes this slow.
Is there any other way achieve the same?
One thing you can do to speed things up when you want to get this from several products is to use the product collection, so there is only one DB query which fetches the information per store for all products.
This would then look something like this:
$storeId = 1; // Current Store you want to look at
$productIds = array(10,15,26); // Enter your Ids
$product = Mage::getModel('catalog/product');
$products = $product->getCollection()
->addStoreFilter($storeId)
->addAttributeToFilter('entity_id', array('in' => $productIds))
->addAttributeToSelect('price')
->addAttributeToSelect('description');
And then loop over the products:
$currPrices = array();
foreach ($products as $prod) {
$currPrices[$prod->getId()] = $prod->getPrice();
}
To make it clear for several stores:
$currPrices = array();
$currDescriptions = array();
foreach ($productObj->getStoreIds() as $_storeId) {
$productIds = array(10,15,26); // Enter your Ids
$product = Mage::getModel('catalog/product');
$products = $product->getCollection()
->addStoreFilter($_storeId)
->addAttributeToFilter('entity_id', array('in' => $productIds))
->addAttributeToSelect('price')
->addAttributeToSelect('description');
foreach ($products as $prod) {
$currPrices[$_storeId][$prod->getId()] = $prod->getPrice();
$currDescriptions[$_storeId][$prod->getId()] = $prod->getDescription();
}
}
The fastest way is to make direct queries (of course, wrapped to Zend_Db_Select) to store and product tables.
base_currency can be got from core_config_data
product prices can be found in catalog_category_product_index
description in catalog_product_entity_text
Look to
Mage_Reports_Model_Resource_Product_Index_Abstract
Mage_Reports_Model_Resource_Report_Product_Viewed_Collection
Mage_Reports_Model_Resource_Report_Product_Viewed
Mage_Reports_Model_Resource_Quote_Collection
Magento uses Zend_Db_Select, even fetch methods of the adapter, not collections, in case of increasing speed of queries.
There are pre-defined attributes in Mangento 1.7 categories named
active from
active to
I can fetch Name of the current product's category using this:
$productId=$_helper->productAttribute($_product, $_product->getId(), 'id');
$product = Mage::getModel('catalog/product')->load($productId);
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
echo $_cat->getName();
} ?>
but I need to find the active from date of current category also.
How do I fetch this?
It is probably worth noting that these values are for the active state of a custom design for the category, and not the actual active state of the category itself...
As an array together:
$_cat = Mage::getModel('catalog/category')->load($category_id);
$_customDesignDates = $_cat->getCustomDesignDate();
Or individually:
$_cat = Mage::getModel('catalog/category')->load($category_id);
$fromDate = $_cat->getData('custom_design_from');
$toDate = $_cat->getData('custom_design_to');