I used this code to select all customer details
function getCustomers() {
/* Magento's Mage.php path
* Mage Enabler users may skip these lines
*/
require_once ("../mysite/app/Mage.php");
umask(0);
Mage::app("default");
/* Magento's Mage.php path */
/* Get customer model, run a query */
$collection = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*');
$result = array();
foreach ($collection as $customer) {
$result[] = $customer->toArray();
}
return $result;
}
But i alos want to check a field value...
That is thre have a column 'usertypecurrent ' in eav_attribute table.....
I need to check its value is 0.
That means select all customer its usertype is 0...
How can i do this?
You can use addAttributeToFilter to filter results based on attribute values
$collection = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToFilter('usertypecurrent', array('eq' =>0))
->addAttributeToSelect('*');
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
im inserting Category for the product like this.
$designer=$_POST['desig'];
$product->setCategoryIds(array($designer));
In PHP code you can put them into the category while you are importing them.
Say you have a product called $product and a category ID called $category_id
You can put it into a category by doing the following code
$product->setCategoryIds(array($category_id));
$product->save();
Use as follows :
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
// Load products
$products = Mage::getModel('catalog/product')
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->getAll();
// Load categories
$category = Mage::getModel('catalog/category');
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID);
$categories = $category->getAll();
foreach($products as $product) {
// Get relevant category
$product->setCategoryIds(array($category->getId()));
$product->save();
}
you might need to do few tweaks with above code to make it compatible to your need.
$product1 = Mage::helper('catalog/product')->getProduct(42, $storeId);
Cannot be used as it requires I give the product id.
Also the category method wants a category id.
Any suggestions?
$collection = Mage::getResourceModel('reports/product_collection');
$collection->addStoreFilter();
$total_products_in_store = $collection->getSize();
$storeId = 2;
/** #var $collection Mage_Catalog_Model_Resource_Product_Collection */
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addStoreFilter($storeId);
// Retrieve product count in collection
$size = $collection->count(); // or $collection->getSize()
I am overriding the Mage/Adminhtml/Sales/Order/Grid.php and adding some data to the prepareCollection. This is how I got the customer EAV Attribute campaign_id to be included in the collection, but it is kind of hacky. I was wondering if there was a better way.
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
foreach ($collection as &$object){
$customer = Mage::getModel('customer/customer')
->setId($object->getCustomerId())
->load();
$object->setCampaignId($customer->getCampaignId());
}
$this->setCollection($collection);
return parent::_prepareCollection();
}
You'll need to join the data from customer records onto the order collection before its loaded.
You can observe the collection before & after load events. For sales/order_grid_collection collection these events are sales_order_grid_collection_load_before and sales_order_grid_collection_load_after - you'll want to use the former. The collection can be accessed in your _before_load event observer via $observer->getOrderGridCollection().
protected function _prepareCollection() {
$collection = Mage::getResourceModel($this->_getCollectionClass());
$class = get_class($collection);
$attribute = Mage::getModel('eav/config')
->getAttribute('customer', 'campaign_id');
$attributeId = $attribute->getAttributeId();
$backendType = $attribute->getBackendType(); //probably varchar
$tableName = Mage::getSingleton('core/resource')
->getTableName('customer_entity_' . $backendType);
$collection->getSelect()
->joinLeft(array('v' => $tableName),
'main_table.customer_id = v.entity_id AND attribute_id = 153',
array('v.value', 'v.attribute_id'));
$this->setCollection($collection);
return parent::_prepareCollection();
}