How to add date picker to column in Magento? - magento

I have following code which creates column for epiration date in my Grid.php. Right now it takes data from DB field which is empty because I would like to have date picker in the column which will save picked date in DB and then show it back to admin. I know that it is possible to do it with $fieldset but I need to use addColumn
$this->addColumn('expiration_date', array(
'header' => Mage::helper('AdvancedStock')->__('Expiration Date'),
'index' => $this['expiration_date'],
'type' => 'datetime',
));
Please help.

I am not sure where you are getting this from: $this['expiration_date']
but that should work, try setting filer and sortable manually.
array(
'index' => 'fieldname',
'filter' => true,
'sortable' => true,
'type' => 'datetime',
)
You will also need to make sure you have included your fieldname in the collection query if it's a none-standard field you have added, by modifying the _prepareCollection() method.
This will be slightly different depending on the collection model you are using
$collection->addFieldToSelect('fieldname');

Instead of type => 'datetime' try type='date' . hope it helps.
You have to add
<reference name="head">
<action method="addItem">
<type>js_css</type>
<name>calendar/calendar-win2k-1.css</name>
<params/><!--<if/><condition>can_load_calendar_js</condition>-->
</action>
<action method="addItem">
<type>js</type>
<name>calendar/calendar.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>-->
</action>
<action method="addItem">
<type>js</type>
<name>calendar/calendar-setup.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>-->
</action>
</reference>
in design/adminhtml/ .../layout, in your xml file corresponding your modified module.

Related

Need to create a new attribute in a new type of product

I am creating a module to insert a new attribute and a new type of product in my store. As template, I use the same registration screen of the Virtual Product type and include new options ONLY this type of product.
For this, in my config.xml, I used:
<catalog>
<product>
<type>
<incomm_virtual translate="label" module="incomm">
<label>Incomm Virtual</label>
<model>incomm/product_type</model>
<price_model>incomm/product_price</price_model>
<is_qty>1</is_qty>
<composite>0</composite>
<can_use_qty_decimals>0</can_use_qty_decimals>
</incomm_virtual>
</type>
</product>
</catalog>
and to configure the installation with the sql in same config.xml:
<resources>
<abc_incommproduct_setup>
<setup>
<module>ABC_Incomm</module>
<class>Mage_Catalog_Model_Resource_Setup</class>
</setup>
</abc_incommproduct_setup>
</resources>
The next file was used mymodule / model / product / type.php containing:
class ABC_Incomm_Model_Product_Type
extends Mage_Catalog_Model_Product_Type_Virtual
{
const TYPE_INCOMM = 'incomm';
const XML_PATH_AUTHENTICATION = 'catalog/incomm/authentication';
protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode)
{
if ($this->_isStrictProcessMode($processMode)) {
return Mage::helper('ABC_Incomm_Helper_Data')->__(
'Incomm product %s cannot be added to cart. ' .
' On the product detail page click the "Go to parent site"'.
' button to access the product.',
$product->getName()
);
}
return parent::_prepareProduct($buyRequest, $product, $processMode);
}
}
In the same folder, the virtual.php:
class ABC_Incomm_Model_Product_Virtual extends Mage_Catalog_Model_Product_Type_Virtual {
}
Finally, the SQL installation file in sql folder / abc_incommproduct_setup / mysql-install-4-0.1.0.php:
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'terms_of_use', array(
'group' => 'General',
'input' => 'textarea',
'type' => 'text',
'sort_order' => 4,
'label' => 'Terms of use',
'backend' => '',
'visible' => true,
'required' => true,
'wysiwyg_enabled' => true,
'visible_on_front' => true,
'apply_to' => 'incomm', //only in this type of product
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$this->endSetup();
My problem is this, after disabling the cache in magento, clear the cache and include a new type of product, in the Product Types list appears my NEW type of product, but the options NOT load the new attribute inserted via sql, the What's happening? my sqlinstall.php did not run ??
Probably because your new product type is incomm_virtual as specified in config.xml and not incomm. Try to edit 'apply_to' => 'incomm' with 'apply_to' => 'incomm_virtual' and re-run the installer
If you upgrade you script follow the similar pattern as install upgrade script, except the file-name is now data-upgrade-{old-version}-{new-version}.php and they are located under the data folder.
If you don't want to upgrade your script and just change it. don't forget to delete the existing entries for youmodule_setup code from core_resource table. it only loaded at once. if you don't delete that your script wont be loaded.

How add attribute to Admin Product's custom tab

i have created a tab on admin product. by following way.
<adminhtml_catalog_product_edit>
<reference name="product_tabs">
<action method="addTab" ifconfig="customoptionspricing/general/enable" ifvalue="1">
<name>customoptionspricing_tab</name>
<block>customoptionspricing/adminhtml_catalog_product_tab</block>
</action>
</reference>
</adminhtml_catalog_product_edit>
Tab shows perfectly, i have some custom data to show in its phtml file.
Now i have to show product custom attribute in this tab's contents. i do not know how can i add this by using this phtml file, or any other way.
i tried to add attribute like this:
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('catalog_product', 'is_cop_product', array(
'group' => 'Custom Options Pricing',
'label' => 'Is Custom Options Pricing (COP) Product',
'type' => 'int',
'input' => 'boolean',
'visible' => true,
'required' => true,
'position' => 1,
'global' => 'Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL',
'note' => "is product a cop product"
));
but this way it creates another tab(new Group), with this attribute.
So i need this attribute to be added on tab that i already created.?? thanks
Try setting used_in_forms.
Mage::getSingleton( 'eav/config' )
->getAttribute( 'catalog_product','is_cop_product')
->setData( 'used_in_forms', array( 'customoptionspricing_tab' ) )
->save();
This has worked for us on 1.8 and 1.9 . In our case it was a customer attribute but I don't see why it wouldn't work for products.

Create Custom Tab Attribute in Customer Edit

I'm trying to create a tab called Additonal in admin customer edit and place my custom employee attribute in it. Is this possible in via my modules sql setup? This question is relevant only to Magento >= 1.5.1.
$installer = $this;
$installer->startSetup();
$installer->addAttribute('customer', 'employee', array(
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Employee Status',
'input' => 'select',
'class' => '',
'source' => 'boilerplate/customer_attribute_status',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false
));
$attribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'employee');
$attribute->addData(array('sort_order'=>50));
$attribute->setData('used_in_forms', array('adminhtml_customer'));
$attribute->save();
Below is not working. Here I'm trying to create a tab in backend admin customer edit and place my emploee attribute in it.
$entityTypeId = $installer->getEntityTypeId('customer');
$attributeId = $installer->getAttributeId('customer', 'employee');
$attributeSets = $installer->_conn->fetchAll('select * from '.$this->getTable('eav/attribute_set').' where entity_type_id=?', $entityTypeId);
foreach ($attributeSets as $attributeSet) {
$setId = $attributeSet['attribute_set_id'];
$installer->addAttributeGroup($entityTypeId, $setId, 'Additional');
$groupId = $installer->getAttributeGroupId($entityTypeId, $setId, 'Additional');
$installer->addAttributeToGroup($entityTypeId, $setId, $groupId, $attributeId);
}
$installer->endSetup();
Here is the code to add a tab in admin's customer edit view:
At your module's admin's layout's .xml file, put:
<adminhtml_customer_edit>
<reference name="customer_edit_tabs">
<action method="addTab"><name>tabs_name</name><block>ModuleAlias/path_to_block_file</block></action>
</reference>
</adminhtml_customer_edit>
your block file should extends Mage_Adminhtml_Block_Template and implements Mage_Adminhtml_Block_Widget_Tab_Interface (meaning that you'll have to implement some methods), and in the construct you can set the template file, ie:
class Namespace_Module_Block_Adminhtml_Customer_Edit_Tab_History
extends Mage_Adminhtml_Block_Template
implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
public function __construct()
{
parent::__construct();
$this->setTemplate('path/to/file.phtml');
}
//down here are the mandatory methods you have to include
public function getTabLabel()
{
return Mage::helper('points')->__('Tab label');
}
public function getTabTitle()
{
return Mage::helper('points')->__('Tab title');
}
public function canShowTab()
{
if (Mage::registry('current_customer')->getId()) {
return true;
}
return false;
}
public function isHidden()
{
if (Mage::registry('current_customer')->getId()) {
return false;
}
return true;
}
}
You also can add AJAX tab instead of load all data with page
for example
<layout>
<adminhtml_catalog_product_edit>
<reference name="product_tabs">
<action method="addTab">
<id>mediagallery</id>
<tab>
<label>MediaGallery</label>
<class>ajax</class>
</tab>
</action>
<action method="setTabData">
<id>mediagallery</id>
<key>url</key>
<value>*/catalog_product_mediagallery/index</value>
</action>
</reference>
</adminhtml_catalog_product_edit>
<adminhtml_catalog_product_mediagallery_index>
<block type="mediagallery/adminhtml_catalog_product_edit_tab_gallery" name="root" output="toHtml" template="mediagallery/container.phtml">
</block>
</adminhtml_catalog_product_mediagallery_index>
<adminhtml_catalog_product_mediagallery_image>
<block type="mediagallery/adminhtml_catalog_product_edit_tab_gallery_image" name="root"></block>
</adminhtml_catalog_product_mediagallery_image>
</layout>
add tab link on handler
set Tab data to set correct url from wehre load conten
in controller you can render content or call layout as in my example
adminhtml_catalog_product_mediagallery_index - render content: grid contained and some custom buttons/
adminhtml_catalog_product_mediagallery_image - grid url for grid which was rendered in previous handle

Magento - Add column to customer grid

how to add a custom column to magento customer grid ?
Thanks a lot.
You should override the class Mage_Adminhtml_Block_Customer_Grid (app/code/core/Mage/Adminhtml/Block/Customer/Grid.php) and apply the following changes:
1 - add the new attribute to show in the _prepareCollection() function
2 - add the new column to show in the _prepareColumns() function
Credit: http://www.leonhostetler.com/blog/magento-add-attribute-columns-in-manage-products-grid-201205/
Magento does not provide us with the ability to choose which attributes are included as columns in the Manage Products grid but it’s fairly simple to make the necessary code changes.
The code that generates the Manage Products grid is at /app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php. The first thing you need to do is copy Grid.php into to the local directory structure. In other words, you copy Grid.php into the following location; /app/code/local/Mage/Adminhtml/Block/Catalog/Product/. If there is no such location, then you must create the necessary directories. The final location of the file must be; /app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php
Now, open Grid.php (the one in the local directory structure) and begin editing. Find the following code;
$this->addColumn('sku',
array(
'header'=> Mage::helper('catalog')->__('SKU'),
'width' => '80px',
'index' => 'sku',
));
That’s the code that adds the SKU column to the product grid. Now, let’s say you have a custom attribute called Supplier ID (supplier_ID) and you want these to appear on the Manage Products grid as well. Place the following code either before or after the above block of code, as long as it’s inside _prepareColumns().
$this->addColumn('supplier_id',
array(
'header'=> Mage::helper('catalog')->__('Supplier ID'),
'width' => '150px',
'index' => 'supplier_id',
));
Then add the following line to _prepareCollection() where the other attributes are listed like this;
->addAttributeToSelect('supplier_id')
That should be all you need to do. You might have to re-compile, refresh your caches, logout, and log back in to see the change in your product grid.
The above example is for adding an attribute with a Catalog Input Type for Store Owner of Text Field. What if your attribute uses a dropdown list? The code above will have to be modified.
Let’s say you have an attribute called Supplier (supplier) which in the product editor presents a dropdown list of suppliers to choose from. To do this, we can add the following code to _prepareColumns():
$supplier_items =
Mage::getModel('eav/entity_attribute_option')->getCollection()->setStoreFilter()->join('attribute','attribute.attribute_id=main_table.attribute_id',
'attribute_code'); foreach ($supplier_items as $supplier_item) :
if ($supplier_item->getAttributeCode() == 'supplier')
$supplier_options[$supplier_item->getOptionId()] = $supplier_item->getValue(); endforeach; $this->addColumn('supplier',
array(
'header'=> Mage::helper('catalog')->__('supplier'),
'width' => '150px',
'type' => 'options',
'index' => 'supplier',
'options' => $supplier_options, ));
And let’s not forget to add the following line to _prepareCollection() where the other attributes are listed like this;
->addAttributeToSelect('supplier')
That should do it for you. Re-compile, refresh your caches, and logout and then back in if you need to.
I have publish here with real example.
If you need to add custome attribute, you may need to take care of join statements carefully.
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
Rewrite customer grid block with your custom module.
app/code/[local or community]/YourCompany/YourModule/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<yourcompany_yourmodule>
<version>0.1.0</version>
</yourcompany_yourmodule>
</modules>
<global>
<blocks>
<adminhtml>
<rewrite>
<customer_grid>YourCompany_YourModule_Block_Customer_Grid</customer_grid>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
app/code/[local or community]/YourCompany/YourModule/Block/Customer/Grid.php
<?php
class YourCompany_YourModule_Block_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{
public function setCollection($collection)
{
$collection->addAttributeToSelect('confirmation');
parent::setCollection($collection);
}
protected function _prepareColumns()
{
parent::_prepareColumns();
$this->addColumn('confirmation', array(
'header'=> Mage::helper('sales')->__('Confirmed'),
'index' => 'confirmation',
'type' => 'text',
'width' => '100px',
));
return parent::_prepareColumns();
}
}
Detailed explanation can be found here:
http://tipsmagento.blogspot.com/2011/03/add-new-column-on-customers-grid.html
Make sure to check out TigerMin for Magento. It's a tool with which you can easily add columns to productsgrid and even inline edit values instantly. Here`s a live demo: http://demo.emvee-solutions.com/tigermin/

Magento - Sort by Date Added

How can I make Magento sort products in the catalog by the date they were added? This isn't an option in the admin so guessing it needs to be done in the code somewhere.
Thanks.
It is quite easy to add a sorting by date option if you're OK (you shouldn't be) with modifying core files. Simply modify app/code/core/Mage/Catalog/Model/Config.php file as in example below:
public function getAttributeUsedForSortByArray()
{
$options = array(
'position' => Mage::helper('catalog')->__('Position'),
// HERE IS OUR NEW OPTION
'created_at' => Mage::helper('catalog')->__('Date')
);
foreach ($this->getAttributesUsedForSortBy() as $attribute) {
/* #var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
$options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
}
return $options;
}
It is not so easy, if you are not into modifying core files. In that case you must create this bunch of files:
app/etc/modules/Stackoverflow_Catalog.xml
<?xml version="1.0"?>
<config>
<modules>
<Stackoverflow_Catalog>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Catalog />
</depends>
</Stackoverflow_Catalog>
</modules>
</config>
app/code/local/Stackoverflow/Catalog/etc/config.xml
<?xml version="1.0"?>
<config>
<global>
<models>
<catalog>
<rewrite>
<config>Stackoverflow_Catalog_Model_Config</config>
</rewrite>
</catalog>
</models>
</global>
</config>
app/code/local/Stackoverflow/Catalog/Model/Config.php
<?php
class Stackoverflow_Catalog_Model_Config extends Mage_Catalog_Model_Config {
public function getAttributeUsedForSortByArray() {
$options = parent::getAttributeUsedForSortByArray();
if (!isset($options['created_at'])) {
$options['created_at'] = Mage::helper('catalog')->__('Date');
}
return $options;
}
}
TIP: Go for a clean way, it will pay of in a long run.
Place this code into your local.xml no need to override any Magento core files.
If you override any Magento core files in future upgrade problem will occur
<layout>
<catalog_category_default>
<reference name="product_list">
<action method="setAvailableOrders" json="value">
<value><![CDATA[
{"created_at" : "Latest","price":"Price"}
]]>
</value>
</action>
</reference>
<reference name="product_list_toolbar">
<action method="setDefaultDirection">
<dir>desc</dir>
</action>
</reference>
</catalog_category_default>
</layout>
I solved this by copying app/code/core/Mage/Catalog/Block/Product/List.php into app/code/local and adding some sorting code at the end of its _getProductCollection() method:
// sort by created_at date or entity_id
if(!isset($_GET['order'])) {
$this->_productCollection->getSelect()->reset( Zend_Db_Select::ORDER );
$this->_productCollection->getSelect()->order('e.entity_id desc');
}
return $this->_productCollection;
You can use either 'e.entity_id desc' or 'e.created_at desc' to sort.
Like this
$_newest_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('visibility', $visibility)
->setOrder('created_at', 'desc')
$_newest_productCollection->load();
Yust for update (works with Mage 1.7.0.2):
in an setupscript of an own module:
$installer = $this;
$installer->startSetup();
$productEntityTypeId = Mage::getModel('catalog/product')->getResource()->getEntityType()->getId();
//lets change created_at properties
//////////////////////////////////////////////////
$installer->updateAttribute($productEntityTypeId, 'created_at', array(
'visible_on_front' => true,
'used_in_product_listing' => true
'used_for_sort_by' => 1,
'frontend_label' => 'Created at'
));
$installer->endSetup();
// mark index as "reindex required"
$indexerCodes = array(
'catalog_product_flat'
);
$indexer = Mage::getModel('index/process');
foreach ($indexerCodes as $code) {
$indexer->load($code, 'indexer_code')
->changeStatus(Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX);
}
and in the catalog.xml layout handle:
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
...
<action method="setDefaultDirection"><dir>desc</dir></action>
</block>
After this you can select created_at as default sorting in the system configuration or in the catagory display
settings
I'm not sure that there is an easy way to do that, without digging into the core code. However, I haven't tried this but it seems like it should work:
Create a new Date attribute. You'll see that there is an option at the bottom of the attribute options called "Used for sorting in product listing". Select Yes for that. Then and add it to your attribute group. Then when you add a product, just select the current date, and you should be able to use that for sorting. To set the default of that, go into System >> Configuration >> Catalog >> Frontend and you'll see your attribute in the "Product listing sort by" option.
Hope that works out for you.
You can try below
app/code/core/mage/catalog/model/resource/eav/mysql4/product/collection.php
in "public function addAttributeToSort($attribute, $dir='asc')"
after
$this->getSelect()->order("cat_index_position {$dir}");
add this
$this->getSelect()->order("e.entity_id desc");
I've done it by rewriting the class:
Mage_Catalog_Model_Category_Attribute_Source_Sortby
and function:
public function getAllOptions()
{
if (is_null($this->_options)) {
$this->_options = array(array(
'label' => Mage::helper('catalog')->__('Best Value'),
'value' => 'position'
));
$this->_options = array(array(
'label' => Mage::helper('catalog')->__('Created At'),
'value' => 'created_at'
));
foreach ($this->_getCatalogConfig()->getAttributesUsedForSortBy() as $attribute) {
$this->_options[] = array(
'label' => Mage::helper('catalog')->__($attribute['frontend_label']),
'value' => $attribute['attribute_code']
);
}
}
return $this->_options;
}

Resources