create custom collection in magento - magento

We have a table with delivery dates and i want to add it to the adminhtml in sales/order/grid.
The problem that i have is that the left join examples dont work for me and im out of options.
Mage/Adminhtml/Block/Sales/Order/Grid.php
The method im trying now is to create a custom query in _prepareCollection()
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = 'SELECT * FROM mage_sales_flat_order_grid
left join mage_aw_deliverydate_delivery
on mage_sales_flat_order_grid.entity_id =
mage_aw_deliverydate_delivery.order_id';
$collection = $readConnection->fetchAll($query);
//$collection = Mage::getResourceModel($this->_getCollectionClass());
//$this->setCollection($collection);
return parent::_prepareCollection();
Then i add this column code in _prepareColumns()
$this->addColumn('delivery_date', array(
'header' => 'delivery_date',
'index' => 'delivery_date',
'type' => 'datetime',
'width' => '100px',
));
I dont even see the new column when i look at the orders grid in the backoffice.
Any help is appreciated!

If you want to add the anthor table
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft('aw_deliverydate_delivery', 'main_table.entity_id = aw_deliverydate_delivery.order_id',array('*' ) );
$this->setCollection($collection);
return parent::_prepareCollection();
}
And the below code in _prepareColumns()
$this->addColumn('aw_deliverydate_delivery', array(
'header' => Mage::helper('sales')->__('Delivery Date '),
'index' => 'aw_deliverydate_delivery',
'type' => 'datetime',
'width' => '100px',
));

Related

Display a value attribute instead id in backoffice product management

I add an attribute in product management in the backoffice (dropdown list)and I want to show the value instead of id.
here is the solution now it works i updated it :)
Grid.phtml
protected function _prepareCollection()
{
...
$collection->addAttributeToSelect('manufacturer'); //my add attribute
...
}
protected function _prepareColumns()
{
// add this code
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'manufacturer');
$options = $attribute->getSource()->getAllOptions(false);
$values = array();
foreach ($options as $option){
$values[$option['value']] = $option['label'];
}
$this->addColumn('manufacturer', //my add attribute
array(
'header'=> 'Manufacturer',
'width' => '100px',
'index' => 'manufacturer',
'type' => 'options',
'options' => $values,
));
Take a look into Mage_Adminhtml_Block_Catalog_Product_Grid::_prepareCollection() there are many attributes with addColumn.
You should customize the options index with the possible values.
There is a great tutorial in http://www.demacmedia.com/magento-commerce/mini-tutorial-adding-custom-attributes-to-the-backend-product-grid/
In resume you should use the following code.
protected function _getAttributeOptions($attribute_code)
{
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attribute_code);
$options = array();
foreach( $attribute->getSource()->getAllOptions(true, true) as $option ) {
$options[$option['value']] = $option['label'];
}
return $options;
}
And now lets update our addColumn functions to the following:
$this->addColumn('manufacturer', //my add attribute
array(
'header'=> Mage::helper('catalog')->__('Manufacturer'),
'width' => '100px',
'index' => 'manufacturer',
'type' => 'text',
'options' => $this->_getAttributeOptions('attribute_code'),
));
For getting attribute just use
$attribute = Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'manufacturer');
instead of yours!

Magento 1.9: Why aren't certain values showing on the Sales > Orders grid?

Why are the payment methods not showing on my Sales > Orders grid?
I can get the column showing with the drop down list of payment options but the payment method values are not showing on the list of orders.
This is the query that produces the orders list:
SELECT `main_table`.*, `payment`.`method`
FROM
`sales_flat_order_grid` AS `main_table`
INNER JOIN `sales_flat_order_payment` AS `payment`
ON main_table.entity_id=payment.parent_id
The column I need to display the values for is called method and returns the correct results, for example worldpay_cc. The values are returned from the query but just aren't showing in the grid.
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->join(array('payment'=>'sales/order_payment'),'main_table.entity_id=payment.parent_id','method');
$collection->addProductData();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('method', array(
'header' => $this->__('Payment Method'),
'index' => 'method',
'type' => 'options',
'width' => '70px',
'options' => array(
'worldpay_cc' => 'Worldpay',
'cashondelivery' => 'Cash on Delivery',
'pay' => 'Pay',
'paypal_express' => 'Paypal Express',
)
));
return parent::_prepareColumns();
}
Any ideas?
My guess would be that you haven't mapped the payment methods correctly maybe:
Mage_Adminhtml_Block_Sales_Order_Grid
protected function _prepareColumns()
{
$this->addColumn('method', array(
'header' => $this->__('Payment Method'),
'index' => 'method',
'type' => 'options',
'width' => '70px',
'options' => array( // <--- The mapping, here
'worldpay_cc' => 'Worldpay',
'cashondelivery' => 'Cash on Delivery',
'pay' => 'Pay',
'paypal_express' => 'Paypal Express',
)
));
return parent::_prepareColumns();
}
I would change the above to:
protected function _prepareColumns()
{
$this->addColumn('method', array(
'header' => $this->__('Payment Method'),
'index' => 'method',
'type' => 'options',
'width' => '70px',
'options' => $this->getActivePaymentMethods()
));
return parent::_prepareColumns();
}
public function getActivePaymentMethods()
{
$payments = Mage::getSingleton('payment/config')->getActiveMethods();
$methods = array();
foreach ($payments as $paymentCode=>$paymentModel) {
$paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title');
$methods[$paymentCode] = $paymentTitle;
}
return $methods;
}
With reference to my comment, addProductData is a custom function:
Mage_Sales_Model_Order_Grid_Collection
public function addProductData($attributesCodes)
{
foreach ($attributesCodes as $attributeCode) {
$attributeTableAlias = $attributeCode . '_table';
$attribute = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
$this->getSelect()->join(
array($attributeTableAlias => $attribute->getBackendTable()),
"main_table.product_id = {$attributeTableAlias}.entity_id AND {$attributeTableAlias}.attribute_id={$attribute->getId()}",
array($attributeCode => 'value')
);
$this->_map['fields'][$attributeCode] = 'value';
}
return $this;
}

add tab in admin dashboard magento 1.7.0.2

Copy Ordered.php
From
app/code/core/Mage/Adminhtml/Block/Dashboard/Tab/Products
to
app/code/local/Mage/Adminhtml/Block/Dashboard/Tab/Products
Rename New.php
I have modified the following code:
class Mage_Adminhtml_Block_Dashboard_Tab_Products_New extends Mage_Adminhtml_Block_Dashboard_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('productsNewGrid');
}
protected function _prepareCollection()
{
if (!Mage::helper('core')->isModuleEnabled('Mage_Sales')) {
return $this;
}
if ($this->getParam('website')) {
$storeIds = Mage::app()->getWebsite($this->getParam('website'))->getStoreIds();
$storeId = array_pop($storeIds);
} else if ($this->getParam('group')) {
$storeIds = Mage::app()->getGroup($this->getParam('group'))->getStoreIds();
$storeId = array_pop($storeIds);
} else {
$storeId = (int)$this->getParam('store');
}
$todayStartOfDayDate = Mage::app()->getLocale()->date()
->setTime('00:00:00')
->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$todayEndOfDayDate = Mage::app()->getLocale()->date()
->setTime('23:59:59')
->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$collection
->addStoreFilter()
->addAttributeToFilter('news_from_date', array('or'=> array(
0 => array('date' => true, 'to' => $todayEndOfDayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToFilter('news_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayStartOfDayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToFilter(
array(
array('attribute' => 'news_from_date', 'is'=>new Zend_Db_Expr('not null')),
array('attribute' => 'news_to_date', 'is'=>new Zend_Db_Expr('not null'))
)
);
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('name', array(
'header' => $this->__('Product Name'),
'sortable' => false,
'index' => 'product_name'
));
$this->addColumn('price', array(
'header' => $this->__('Price'),
'width' => '120px',
'type' => 'currency',
'currency_code' => (string) Mage::app()->getStore((int)$this->getParam('store'))->getBaseCurrencyCode(),
'sortable' => false,
'index' => 'product_price'
));
$this->addColumn('ordered_qty', array(
'header' => $this->__('Quantity Ordered'),
'width' => '120px',
'align' => 'right',
'sortable' => false,
'index' => 'qty_ordered',
'type' => 'number'
));
$this->setFilterVisibility(false);
$this->setPagerVisibility(false);
return parent::_prepareColumns();
}
/*
* Returns row url to show in admin dashboard
* $row is bestseller row wrapped in Product model
*
* #param Mage_Catalog_Model_Product $row
*
* #return string
*/
public function getRowUrl($row)
{
// getId() would return id of bestseller row, and product id we get by getProductId()
$productId = $row->getProductId();
// No url is possible for non-existing products
if (!$productId) {
return '';
}
$params = array('id' => $productId);
if ($this->getRequest()->getParam('store')) {
$params['store'] = $this->getRequest()->getParam('store');
}
return $this->getUrl('*/catalog_product/edit', $params);
}
}
Then Copy Grids.php
From
app/code/core/Mage/Adminhtml/Block/Dashboard/
to
app/code/local/Mage/Adminhtml/Block/Dashboard/
added the following code:
$this->addTab('new_products', array(
'label' => $this->__('New Product'),
'content' => $this->getLayout()->createBlock('adminhtml/dashboard_tab_products_new')->toHtml(),
'class' => 'ajax'
));
I want to add a new product tab in admin dashboard,beside customers.I don't know what wrong with the New.php.I click the new product tab,it's not working.How to fix it?
I have managed to get this working with only a few more lines to change.
Update the Dashboard controller Mage_Adminhtml_DashboardController to add the new action
public function productsNewAction()
{
$this->loadLayout();
$this->renderLayout();
}
Update the admin layout.xml design\adminhtml\default\default\layout\main.xml to add the new section
<adminhtml_dashboard_productsnew>
<block type="core/text_list" name="root" output="toHtml">
<block type="adminhtml/dashboard_tab_products_new" name="adminhtml.dashboard.tab.products.new"/>
</block>
</adminhtml_dashboard_productsnew>
The you would just need to update your code in the Grids.php to the following.
$this->addTab('new_products', array(
'label' => $this->__('New Product'),
'url' => $this->getUrl('*/*/productsNew', array('_current'=>true)),
'class' => 'ajax'
));
This should then work using a call to the url rather than the block content.
You then need to select the attributes you want to show. You can do this by selecting all or by attribute code.
$collection->addAttributeToSelect('*')
$collection->addAttributeToSelect('name');
Important is the column index defined in _prepareColumns match these attribute codes Otherwise you will just get an empty row.
I would suggest packaging these changes into a new module with a controller, layout.xml and block files. There are lots of great tutorials around on how to do this, but obviously you don't have to :)

How add custom filter to colum grid in Magento?

i have custom grid in Adminhtml.
protected function _prepareCollection()
{
/* #var $collection Mage_Catalog_Model_Resource_Product_Collection */
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('*');
$collection->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left');
$collection->groupByAttribute('entity_id');
$collection->addStaticField('category_id');
$collection->addExpressionAttributeToSelect('category_grp', 'GROUP_CONCAT(category_id)', 'category_id');
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
parent::_prepareColumns();
$this->addColumn('category_id', array(
'header' => Mage::helper('newsletter')->__('Category'),
'index' => 'category_grp',
'type' => 'categories',
'options' => $options,
'align' => 'left',
// 'filter_index' => $this->_getFlatExpressionColumn('category'),
return $this;
}
category_grp is array with int
My question is how can i add filter to header column which filters the item from the field?
for example filter only product with category_id=7 (category_grp is 3,6,7,13)...
I don't think you will need any custom filter.
Just try to set index in accord with field name:
'index' => 'category_id',
Take a look at magento filter_condition_callback option
$this->addColumn('categories', array(
....
'filter_condition_callback' => array($this, '_applyMyFilter'),
..
)
);
protected function _filterCategoriesCondition($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
return;
}
$this->getCollection()->addFieldToFilter('categories', array('finset' => $value));
}
See
Filtering a text-type column with MySQL-computed values in Magento Admin Grid
Magento: How to search or filter by multiselect attribute in admin grid?

Adding data from customer_entity_varchar to order grid magento

I am trying to add 'accountno' field to order grid from customer_entity_varchar table. To achieve this I changed _prepareCollection() method in Grid.php like this:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$customerEntityVarchar = Mage::getSingleton('core/resource')->getTableName('customer_entity_varchar');
$collection->getSelect()->join(array(
'customer_entity_varchar_table'=>$customerEntityVarchar),
'`main_table`.`customer_id`=`customer_entity_varchar_table`.`entity_id`
AND `customer_entity_varchar_table`.`attribute_id`=122',
array('accountno' => 'value'));
print_r($collection->getSelect()->__toString());
$this->setCollection($collection);
return parent::_prepareCollection();
}
In attribute_id=122' 122 is the attribute_id ofaccountno`
When I print_r the select query and execute it in phpmyadmin, it gets me the accurate results back but when I try to add Column to grid in _prepareColumns() method, it shows the field blank.
My _prepareColumns method looks like this:
protected function _prepareColumns()
{
$this->addColumn('real_order_id', array(
'header'=> Mage::helper('sales')->__('Order #'),
'width' => '80px',
'type' => 'text',
'index' => 'increment_id',
));
$this->addColumnAfter('accountno', array(
'header' => Mage::helper('sales')->__('Account No'),
'index' => 'accountno',
), 'real_order_id');
//rest of the code here
}

Resources