Magento 1.7 - show order quantity in grid without decimals - magento

I have this column definition in a grid for the ordered pieces of an order item:
$this->addColumn('qty_ordered',
array(
'header'=> $this->__('Quantity'),
'align' =>'right',
'type=' => 'number',
'index' => 'qty_ordered'
)
);
xxx http://img688.imageshack.us/img688/6690/scr031.jpg
How can I show the quantity without any decimals?

One way to do this is to create a custom module
$this->addColumn('qty_ordered',
array(
'header'=> $this->__('Quantity'),
'align' =>'right',
'type=' => 'number',
'index' => 'qty_ordered'
'renderer' = new MageIgniter_RemoveQtyDecimals_Block_Adminhtml_Renderer_Date()
)
);
class MageIgniter_RemoveQtyDecimals_Block_Adminhtml_Renderer_Date extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Action
{
public function render(Varien_Object $row)
{
return $this->_getValue($row);
}
public function _getValue(Varien_Object $row)
{
$val = $row->getData($this->getColumn()->getIndex());
return intval($val);
}
}
See https://stackoverflow.com/a/12695286/1191288

I modified the sku array definition instead of creating qty_ordered.
$collection = Mage::getResourceModel($this->_getCollectionClass())
->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'skus' => new Zend_Db_Expr('group_concat( `sales/order_item`.sku, "(", floor(`sales/order_item`.qty_ordered), ")" SEPARATOR ", ")'),
));
I believe you can use floor(sales/order_item.qty_ordered) on your qty_ordered definition as the same.
SKU column on Order Grid results something like:
MBA001(2), MOF004(1)

Related

Magento invoice grid filter by joined attribute value

I'm using the following code to include a custom attribute, titled "sales rep" within the Magento invoice grid:
protected function _prepareCollection() {
$sales_rep = Mage::getResourceSingleton('customer/customer')->getAttribute('sales_rep');
$collection = Mage::getResourceModel('sales/order_invoice_grid_collection');
$collection->join('invoice', 'main_table.entity_id = invoice.entity_id',array('order_id as order_id'));
$collection->join('order', 'invoice.order_id = order.entity_id',array('customer_id as customer_id'));
$collection->getSelect()->joinLeft(
array('customer_sales_rep_table' => Mage::getSingleton('core/resource')->getTableName($sales_rep->getBackend()->getTable())),
'customer_sales_rep_table.entity_id = order.customer_id
AND customer_sales_rep_table.attribute_id = '.(int) $sales_rep->getAttributeId() . '
',
array('sales_rep'=>'value')
);
$this->setCollection($collection);
return parent::_prepareCollection();
}
...
$this->addColumn('sales_rep', array(
'header' => Mage::helper('sales')->__('Sales Rep'),
'index' => 'sales_rep',
'filter' => false
));
This is working perfectly, just as long as the "addColumn" property "filter" is set to "false".
How would I go about allowing users to filter by this joined attribute?
You should add your column like this:
$this->addColumn('sales_rep', array(
'header' => Mage::helper('sales')->__('Sales Rep'),
'index' => 'sales_rep',
'filter_index' => 'customer_sales_rep_table.value'
));

Magento: only existing countries in drop-down filter for "Country" column in Grid View

I have created a custom Grid View with the "Country" column:
$this->addColumn('ship_country', array(
'header' => $this->__('Country'),
'index' => 'countrycode',
'type' => 'country'
));
The collection list which is displayed in the grid has only 2 countries: Ireland and UK.
Problem: the Filter Header for country column shows the drop-down list with all possible, over 200, countries stored inside Magento.
Question: is it possible to force filter to show only Ireland and UK in the drop-down?
Like this:
I have solved this problem with the following code:
$this->addColumn('ship_country', array(
'header' => $this->__('Country'),
'index' => 'countrycode',
'type' => 'options',
'options' => Mage::helper('mymodule')->getCountries(),
));
Where Helper getCountries() method looks something like this:
public function getCountries() {
$collection = Mage::getModel('mymodule/entity')->getCollection();
$collection->getSelect()->group('countrycode');
$countries = array();
foreach($collection as $item)
$countries[$item->getcountrycode()] = Mage::getModel('directory/country')->load($item->getcountrycode())->getName();
return $countries;
}

magento - add customer name to order grid in magento 1.7.0.2

I'm trying to add a new column for Customer Name in the Sales Order Grid located here:
App/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php
I want add Customer Name like Name in Manage Customers.
I have added the following code:
protected function _getCollectionClass()
{
return 'sales/order_grid_collection';
}
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
/*junpeng add start*/
$collection->getSelect()
->join(
'customer_entity',
'main_table.customer_id = customer_entity.entity_id', array('email' => 'email'));
$collection->getSelect()->join(
'customer_entity_varchar',
'main_table.entity_id = customer_entity_varchar.entity_id', array('name' => 'value')
);
/*junpeng add end*/
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('name', array(
'header' => Mage::helper('sales')->__('Customer Name'),
'index' => 'name',
));
$this->addColumn('email', array(
'header' => Mage::helper('Sales')->__('Customer Email'),
'index' => 'email',
'type' => 'text',
));
}
Customer Email is OK,but add Customer Name does not work it!
Can someone please help me solve this problem?
You can't get customer name in just one line code join. Firstname and Lastname are different attributes and you will need to join them with your original collection and then concatenate them to display as Fullname.
So basically, Replace
$collection->getSelect()->join(
'customer_entity_varchar',
'main_table.entity_id = customer_entity_varchar.entity_id', array('name' => 'value')
);
with this code
$fn = Mage::getModel('eav/entity_attribute')->loadByCode('1', 'firstname');
$ln = Mage::getModel('eav/entity_attribute')->loadByCode('1', 'lastname');
$collection->getSelect()
->join(array('ce1' => 'customer_entity_varchar'), 'ce1.entity_id=main_table.customer_id', array('firstname' => 'value'))
->where('ce1.attribute_id='.$fn->getAttributeId())
->join(array('ce2' => 'customer_entity_varchar'), 'ce2.entity_id=main_table.customer_id', array('lastname' => 'value'))
->where('ce2.attribute_id='.$ln->getAttributeId())
->columns(new Zend_Db_Expr("CONCAT(`ce1`.`value`, ' ',`ce2`.`value`) AS customer_name"));
And replace your addColumn('name', code in _prepareColumns method where you are getting the customer name, with this:
$this->addColumn('customer_name', array(
'header' => Mage::helper('sales')->__('Customer Name'),
'index' => 'customer_name',
'filter_name' => 'customer_name'
));
There is a free magento extension for this:
http://www.magentocommerce.com/magento-connect/enhanced-admin-grids-editor.html
It's currently alpha, but I have tested it and it works perfectly on 1.6 and 1.7!
I inspire my answer from Kalpesh
so you should replace this code
$collection->getSelect()->join(
'customer_entity_varchar',
'main_table.entity_id = customer_entity_varchar.entity_id', array('name' => 'value')
);
with these lines
$customerTable = Mage::getResourceSingleton('customer/customer')->getEntityTable();
$firstnameAttribute=Mage::getResourceSingleton('customer/customer')->getAttribute('firstname');
$firstnameAttributeTable = $firstnameAttribute->getBackend()->getTable();
$lastnameAttribute=Mage::getResourceSingleton('customer/customer')->getAttribute('lastname');
$lastnameAttributeTable = $lastnameAttribute->getBackend()->getTable();
$collection->getSelect()
->join( array('customer_varchar1'=>$firstnameAttributeTable),'main_table.customer_id =customer_varchar1.entity_id and customer_varchar1.attribute_id='.$firstnameAttribute->getId(),
array('customer_varchar1.value'))
->join( array('customer_varchar2'=>$lastnameAttributeTable),'main_table.customer_id =customer_varchar2.entity_id and customer_varchar2.attribute_id='.$lastnameAttribute->getId(),
array('customer name'=>'CONCAT(customer_varchar2.value ," " ,customer_varchar1.value)'));

Searching sales order grid redirects to dashboard in magento 1.7

I have used a custom module to add new columns to Sales_Order_Grid in Magento but If I search for Order_Id the page redirects to the Dashboard.
If I try to select sales/orders again I get an error:
a:5:{i:0;s:104:"SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'increment_id' in where clause is ambiguous";i:1;s:6229:"#0
I have no idea how to solve this and could really do with some guidance please?
Here's the Grid.php code:
<?php
class Excellence_Salesgrid_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
protected function _addColumnFilterToCollection($column)
{
if ($this->getCollection()) {
if ($column->getId() == 'shipping_telephone') {
$cond = $column->getFilter()->getCondition();
$field = 't4.telephone';
$this->getCollection()->addFieldToFilter($field , $cond);
return $this;
}else if ($column->getId() == 'shipping_city') {
$cond = $column->getFilter()->getCondition();
$field = 't4.city';
$this->getCollection()->addFieldToFilter($field , $cond);
return $this;
}else if ($column->getId() == 'shipping_region') {
$cond = $column->getFilter()->getCondition();
$field = 't4.region';
$this->getCollection()->addFieldToFilter($field , $cond);
return $this;
}else if ($column->getId() == 'shipping_postcode') {
$cond = $column->getFilter()->getCondition();
$field = 't4.postcode';
$this->getCollection()->addFieldToFilter($field , $cond);
return $this;
}else if($column->getId() == 'product_count'){
$cond = $column->getFilter()->getCondition();
$field = ( $column->getFilterIndex() ) ? $column->getFilterIndex() : $column->getIndex();
$this->getCollection()->getSelect()->having($this->getCollection()->getResource()->getReadConnection()->prepareSqlCondition($field, $cond));
return $this;
}else if($column->getId() == 'skus'){
$cond = $column->getFilter()->getCondition();
$field = 't6.sku';
$this->getCollection()->joinSkus();
$this->getCollection()->addFieldToFilter($field , $cond);
return $this;
}else{
return parent::_addColumnFilterToCollection($column);
}
}
}
protected function _prepareColumns()
{
$this->addColumnAfter('shipping_description', array(
'header' => Mage::helper('sales')->__('Shipping Method'),
'index' => 'shipping_description',
),'shipping_name');
$this->addColumnAfter('method', array(
'header' => Mage::helper('sales')->__('Payment Method'),
'index' => 'method',
'type' => 'options',
'options' => Mage::helper('payment')->getPaymentMethodList()
),'shipping_description');
$this->addColumnAfter('shipping_city', array(
'header' => Mage::helper('sales')->__('Shipping City'),
'index' => 'shipping_city',
),'method');
$this->addColumnAfter('shipping_telephone', array(
'header' => Mage::helper('sales')->__('Shipping Telephone'),
'index' => 'shipping_telephone',
),'method');
$this->addColumnAfter('shipping_region', array(
'header' => Mage::helper('sales')->__('Shipping Region'),
'index' => 'shipping_region',
),'method');
$this->addColumnAfter('shipping_postcode', array(
'header' => Mage::helper('sales')->__('Shipping Postcode'),
'index' => 'shipping_postcode',
),'method');
/*$this->addColumnAfter('product_count', array(
'header' => Mage::helper('sales')->__('Product Count'),
'index' => 'product_count',
'type' => 'number'
),'increment_id');
/*$this->addColumnAfter('skus', array(
'header' => Mage::helper('sales')->__('Product Purchased'),
'index' => 'skus',
),'increment_id');*/
return parent::_prepareColumns();
}
}
This is what worked in my case:
$collection->addFilterToMap('increment_id', 'main_table.increment_id');
Looks like you are adding some more columns to the admin sales grid? It also sounds like you are working with a collection containing a join against a second table, you need to add the table alias name into the where statement with the increment_id column definition, so table_alias.increment_id.
To check this, and assuming calling $this->getCollection() from Excellence_Salesgrid_Block_Adminhtml_Sales_Order_Grid returns the collection, get the select object from the collection:
$select = $this->getCollection()->getSelect();
Hopefully you are using xdebug with an IDE and can set breakpoints in your code. If so set a breakpoint and inspect the the $select object pulled by the line above. Inside this object you will see a _parts array which describes the way your select statement is constructed. Inside this you will see a from array which contains information about the tables which are part of the statement. If you have a JOIN, this will contain more than one entry.
Under here you can also see where which will describe the where clauses which are part of the statement - this is where the problem likely lies. You need to identify the alias for the correct table inside the from array and then where the where clause is added, instead of doing something like:
$this->getCollection()->getSelect()->where('increment_id = ?', $id);
instead do:
$this->getCollection()->getSelect()->where('table_alias.increment_id = ?', $id);

Magento Sales Order Grid shows incorrect number of records when added Names and Skus columns

I'm working with Magento version 1.4 and I added extra grid columns (names and skus) to Sales Order Grid, the returned data is correct but I'm having problems with pagination and total number of records, my code as follow:
First I Edited Mage_Adminhtml_Block_Sales_Order_Grid:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass())
->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'skus' => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ", ")'),
'names' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ", ")'),
)
);
$collection->getSelect()->group('entity_id');
$this->setCollection($collection);
return parent::_prepareCollection();
}
Then I override this method to return correct results when filter by names or skus
protected function _addColumnFilterToCollection($column)
{
if($this->getCollection() && $column->getFilter()->getValue())
{
if($column->getId() == 'skus'){
$this->getCollection()->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'skus' => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ", ")'),
)
)->getSelect()
->having('find_in_set(?, skus)', $column->getFilter()->getValue());
return $this;
}
if($column->getId() == 'names'){
$this->getCollection()->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'names' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ", ")'),
)
)->getSelect()
->having('find_in_set(?, names)', $column->getFilter()->getValue());
return $this;
}
}
return parent::_addColumnFilterToCollection($column);
}
Then I edited this method getSelectCountSql() in Mage_Sales_Model_Mysql4_Order_Collection class:
public function getSelectCountSql()
{
$countSelect = parent::getSelectCountSql();
//added
$countSelect->reset(Zend_Db_Select::HAVING);
//end
$countSelect->resetJoinLeft();
return $countSelect;
}
How can I calculate number of rows?
Maybe its a bit to late but in your code try using GROUP insted of HAVING:
$countSelect->reset(Zend_Db_Select::GROUP);
Because you are using this statemen:
$collection->getSelect()->group('entity_id');
$collection->getSelect()->join(array(
'item'=>$collection->getTable('sales/order_item')),
'item.order_id=`main_table`.entity_id AND item.product_type="simple"',
array(
'skus' => new Zend_Db_Expr('group_concat(item.sku SEPARATOR ", ")'),
'name' => new Zend_Db_Expr('group_concat(item.name SEPARATOR ", ")')
));
$this->addColumn('skus', array(
'header' => Mage::helper('sales')->__('SKU'),
'index' => 'skus',
'type' => 'text',
));
$this->addColumn('name', array(
'header' => Mage::helper('sales')->__('NAME'),
'index' => 'name',
'type' => 'text'
));
I had this issue and i have got it working by implementing custom getSize() function in the collection i am using
public function getSize()
{
$select = clone $this->getSelect();
$select->reset();
$select = $this->getConnection()->fetchOne('SELECT COUNT(*) FROM Table GROUP BY FIELD'); // or you can use select count(distinct field) from table
return $select;
}
and to achieve Grid storing i have override
protected function _setCollectionOrder($column)
{
$collection = $this->getCollection();
if ($collection) {
$columnIndex = $column->getFilterIndex() ?
$column->getFilterIndex() : $column->getIndex();
$collection->getSelect()->order(array($columnIndex.' '.$column->getDir()));
}
return $this;
}
and Set filter_index of the columns TO
in _prepareColumns() function
'filter_index' => 'SUM(tablename.field)'
and you can use Callback function on filters for the columns

Resources