Display category name in product grid - magento

I am trying to show category name for each product in admin grid. I did it like this and it works, but I am not sure is it correct way:
protected function _prepareCollection()
{
/**
* Tell Magento which collection to use to display in the grid.
*/
$collection = Mage::getResourceModel(
'catalog/product_collection'
);
$collection ->addAttributeToSelect('sku')
->addAttributeToSelect('name')
->addAttributeToSelect('type_id');
$collection->joinTable(
'catalog/category_product',
'product_id=entity_id',
array('my_cat'=>'category_id'),
null,
'left'
);
$collection->joinTable(
'catalog_category_entity_varchar',
'entity_id=my_cat',
array('mv'=>'value'),
null,
'left'
);
$collection->groupByAttribute('entity_id');
$this->setCollection($collection);
return parent::_prepareCollection();
}
I would appreciate if somebody could tell me is it correct, and is there a better way to do this?

Related

Magento - Select stock status in category->getProductCollection

I am trying to get the stock status of all products within a particular category. My current code is below but getIsInStock is not part of the default collection. How can i get this value in the same way as ->addAttributeToSelect('product_type')
$collection = Mage::getModel('catalog/category')->load($_cat_id)->getProductCollection()
->addAttributeToSelect('product_type');
foreach ($collection as $product) {
if( $product->getIsInStock() ) {
print $product->getProductType().' is in stock';
}
}
You can join your collection by :
$collection->joinField(
'is_in_stock',
'cataloginventory/stock_item',
'is_in_stock',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left'
)
let me know if it helpes

Join model between customer entity and custom module

In the Grid.php, I need to join customer/customer model with my custom model.
protected function _prepareCollection() {
$collection = Mage::getResourceModel('customer/customer_collection')->addNameToSelect();
$collection->getSelect()->join(
array('e' => 'event'), 'e.customer_id=main_table.entity_id', array('status')
);
$this->setCollection($collection);
return parent::_prepareCollection();
}
Basically I need to add some more information (in this example is status) to the collection. The event table contains the customer_id as reference to customer_entity.entity_id. How can I do this?
If there is any error you are getting you can share. Meanwhile try below updated code.
protected function _prepareCollection() {
$collection = Mage::getResourceModel('customer/customer_collection')->addNameToSelect();
$collection->getSelect()->join(
array('e' => 'event'), 'e.customer_id=main_table.entity_id', array('e.status') // added 'e.status' in stead of 'status'
);
$this->setCollection($collection);
return parent::_prepareCollection();
}
Hope will help!

how to give where condition in magento customer selecting code

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

Better way to add attribute to collection

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();
}

Magento: Joining left table in grid

I am trying to add a grid in which I need to do a left join.
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('admin/user_collection')->join("school/school",'ref_id = referer_id',"school_name",**"left"**);
var_dump((string)$collection->getSelect());
$this->setCollection($collection);
return parent::_prepareCollection();
}
Even though I have defined "left" when I see the dump it shows:
SELECT main_table.*, school/school.school_name FROM admin_user AS main_table INNER JOIN school AS school/school ON ref_id = referer_id
I tried to do a
$collection = Mage::getResourceModel('admin/user_collection')->**joinLeft**("school/school",'ref_id = referer_id',"school_name","left"); but for which my system does not show me any var_dump hangs.
try getSelect() before the join
with product collection if you want to place OR condition bw the attributes
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter(
array(
array('attribute'=>'my_attribute', 'eq'=>'0'),
array('attribute'=>'my_other_attribute', 'neq'=>'0')
),
'',
'left'
);

Resources