Create Custom Tab Attribute in Customer Edit - magento

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

Related

Magento block rewrite not working properly

I am trying to rewrite product_new block. Purpose is that i want to add category_id field and fetch new products of one category only. Here is the code
app\code\local\Foo\Bar\Block\Product\New.php
class Foo_Bar_Block_Product_New extends Mage_Catalog_Block_Product_New
{
protected function _beforeToHtml()
{
// echo "aaaaaaasdfa";
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter('news_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToSort('news_from_date', 'desc')
->setPageSize($this->getProductsCount())
->setCurPage(1)
;
if($categoryId=$this->getData('category_id')){
$category = Mage::getModel('catalog/category')->load($categoryId);
$collection->addCategoryFilter($category);
}
$this->setProductCollection($collection);
return parent::_beforeToHtml();
}
}
Added block in local.xml using below code
<block type="foo_bar/product_new" name="new_products_list" template="catalog/product/new.phtml">
<action method="setCategoryId"><category_id>4</category_id></action>
<action method="setColumnCount"><columns>4</columns></action>
</block>
module's config.xml is
<global>
<blocks>
<catalog>
<rewrite>
<product_new>Foo_Bar_Block_Product_New</product_new>
</rewrite>
</catalog>
</blocks>
</global>
Please guide me where I am making mistake
Thanks
Issue is on parent::_beforeToHtml(); statement it will call _beforeToHtml method of parent class which is Mage_Catalog_Block_Product_New. So parent function will override collection which you have set.
Solution:
Replace :
parent::_beforeToHtml();
to :
Mage_Catalog_Block_Product_Abstract::_beforeToHtml();

Add drop down list attribute to category - set source

I added "brand" attribute (drop down list) for products.
I'm trying add new attribute (drop down list) to category with values from "brand" attribute.
What should I do to set correct source for this category attribute.
Please see my piece of code in mysql setup file:
$this->startSetup();
$this->addAttribute('catalog_category', 'brand', array(
'group' => 'General',
'type' => 'int'
'backend' => '',
'frontend_input' => '',
'frontend' => '',
'label' => 'brand',
'input' => 'select'
'class' => '',
'source' => 'mymodule/selecattributes',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'frontend_class' => '',
'required' => false,
'user_defined' => true,
'default' => '',
'position' => 100,
));
$this->endSetup();
Thanks in advance.
EDIT 1
I added class MyPackage_MyModule_Model_SelectAttributes extends extends Mage_Eav_Model_Entity_Attribute_Source_Abstract :
class MyPackage_MyModule_Model_SelectAttributes extends Mage_Eav_Model_Entity_Attribute_Source_Abstract{
public function getAllOptions()
{
$attributeCode = 'brand';
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attributeCode); //here, "color" is the attribute_code
$allOptions = $attribute->getSource()->getAllOptions(true, true);
foreach ($allOptions as $instance) {
$myArray[$instance['value']] = $instance['label'];
}
return $myArray;
}
}
EDIT 2
When I open category admin page I get this error:
"Source model "mymodule/selectattributes" not found for attribute "brands""
I'm guessing that your source model is in a file named Selectattributes.php. If you are using a case-sensitive filesystem you will need to do one of two things:
Rename your class definition file to Selectattributes.php
or
Change the source_model value for your brands attribute to mymodule/selectAttributes
When Magento is attempting to instantiate the source model for your attribute, the classname (and hence the autoload include path) being calculated work as follows:
MyPackage_MyModule_Model_Selectattributes
MyPackage/MyModule/Model/Selectattributes.php
Note the issue with the filename. Note that this should be working in a non-case-sensitive filesystem.

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.

How can I use <source_model> for a textbox in Magento for system.xml?

Because it's for a textbox, I'm not sure how the array is supposed to work.
This fails.
public function getDefault_dailymatches()
{
return array(
array('value' => 0, 'label' => Mage::helper()->__('First item')),
array('value' => 1, 'label' => Mage::helper()->__('Second item')),
array('value' => 2, 'label' => Mage::helper()->__('third item')),
// and so on...
);
}
This also fails.
public function getDefault_dailymatches()
{
$default = 100;
return $default;
}
I found the answer out myself by searching Magento.zip for the evident default textbox variables.
Add this to config.xml inbetween the <config></config> tags.
<default>
<showdown> <!-- section -->
<dagroup> <!-- group -->
<dailymatches>1</dailymatches>
<maxmatchvotes>100</maxmatchvotes>
<field>default value</field>
<!-- <blah><![CDATA[<strong>html tags</strong>]]></blah>
<blah2><![CDATA[cdata#foremail.addresses]]></blah2> -->
</dagroup>
</showdown>
</default>

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