I'm looking for to add a new tabpage under category in magento admin, which loads the category grid. Idea is to associate multiple categories to one category and on the front end, when you click on that particular category, it should show products from all the associated categories.
Is there any better solution then create a new tabpage with categories grid?
EDITTED
Right... I have realised I could add new fields in category with install script. Now in my setup script I am doing something like this:
public function getDefaultEntities()
{
return array(
'catalog_category' => array(
'entity_model' => 'catalog/category',
'attribute_model' => 'catalog/resource_eav_attribute',
'table' => 'catalog/category',
'additional_attribute_table' => 'catalog/eav_attribute',
'entity_attribute_collection' => 'catalog/category_attribute_collection',
'attributes' => array(
'men_categories' => array(
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Men Categories',
'input' => 'select',
'class' => '',
'source' => 'pushon_config/config_source',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => 0,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'group' => 'Associated Categories',
),
),
),
);
}
In my source.php in doing this:
class PushOn_Config_Model_Config_Source extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
protected $_options = array();
public function getAllOptions($withEmpty = true, $defaultValues = false)
{
$option = Mage::getResourceModel('catalog/category_collection');
foreach($option as $id){
$this->_options[] = array('value'=> $id->getData('entity_id'), 'label' => $id->getData('name'));
}
return $this->_options;
}
}
Now when I run this it shows me error message:
a:5:{i:0;s:163:"Error in file: "C:\wamp\www\vhosts\staging.domainname.com\httpdocs\app\code\local\PushOn\Config\sql\pushon_config_setup\mysql4-install-0.1.0.php" - Wrong entity ID.";i:1;s:1148:"#0 C:\wamp\www\vhosts\staging.domainname.com\httpdocs\app\code\core\Mage\Core\Model\Resource\Setup.php(390): Mage::exception('Mage_Core', 'Error in file: ...')
I am just trying to load all the categories. Any idea why this is happening?
Related
I have created a custom column for associated products (basically every simple product will have that column under "associated products" tab of configurable product). I store information of this field/column into an attribute which belong to configurable product instead of each simple product.
Below is the code for adding a column;
config.xml
<core_block_abstract_to_html_before>
<observers>
<something>
<class>namespace_JsonProductInfo_Model_Observer</class>
<method>addPositionColumn</method>
</something>
</observers>
</core_block_abstract_to_html_before>
Oberver.php
public function addPositionColumn(Varien_Event_Observer $observer)
{
$block = $observer->getEvent()->getBlock();
if ($block instanceof Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Super_Config_Grid) {
if (!$block->isReadonly()) {
$block->addColumnAfter(
'position_simple',
array(
'header' => Mage::helper('jsonproductinfo')->__('Position'),
'type' => 'input',
'name' => 'position_simple',
'index' => 'position_simple',
'sortable' => false,
),
'name'
);
}
}
}
With above code I am able to add the column into a require grid. And I am also able to save the information which I put inside that column (which is basically an attribute). The only problem I am facing is, I am unable to display the stored information into that column again (If I edit the product). I have also created another observer of type eav_collection_abstract_load_befor but no luck so far.
Below is the code for it:
config.xml
<eav_collection_abstract_load_before>
<observers>
<jsonproductinfo>
<class>namespace_JsonProductInfo_Model_Observer</class>
<method>addPositionToCatalogProductCollection</method>
</jsonproductinfo>
</observers>
</eav_collection_abstract_load_before>
Observer.php
public function addPositionToCatalogProductCollection($observer)
{
$collection = $observer->getEvent()->getCollection();
if (!isset($collection)) {
return;
}
$collection->addAttributeToSelect('position_simple');
}
I would be grateful if someone can point to the mistake or guide me how can I display back that information again into that column.
EDIT And here is the code which creates the "position_simple" attribute.
$this->addAttribute(
'catalog_product',
'position_simple',
array(
'group' => 'General',
'type' => 'varchar',
'input' => 'hidden',
'backend' => '',
'frontend' => '',
'label' => 'Simple Position',
'class' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false,
'apply_to' => Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE,
'is_configurable' => false,
)
);
change 'index' => 'position_simple' to 'index' => 'entity_id' in your Observer.php (function addPositionColumn)
you need to get value of your position_simple attribute from configurable product and use it in added column - one way to do that:
create function in Observer.php
protected function _getDefaultConfigurationId() {
/** #var Mage_Catalog_Model_Product $product */
$product = Mage::registry('current_product');
if ($product) {
return array($product->getData('position_simple'));
}
return '';
}
and use that in your addPositionColumn function like that:
...
'values' => $this->_getDefaultConfigurationId(),
...
After that, there is no need of observer type eav_collection_abstract_load_befor at all.
I have created a custom extension just like a customer module and I want backend just like a customer.
My extension has two tables and two models.
My modules are:
Mage::getModel('custommod/reg') - just like Mage::getModel('customer/customer'), reg saves data of registration
Mage::getModel('custommod/personal') - just like Mage::getModel('customer/address'), //personal data of a reg records.
Please check the image below:
Now I am facing the problem to show the data and edit .
In Magento customer admin section, Customer edit position has multiple tabs: Account information, Address etc.
Here, Account information tab saves data in customer/customer
and Address information tab saves data in customer/address.
I like this type of section.
After a long time work i have done it ,Here the solution
The tabs.php show left panel
<?php
class Amit_Vendor_Block_Adminhtml_List_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
public function __construct()
{
parent::__construct();
$this->setId('vendor_tabs');
$this->setDestElementId('edit_form');
$this->setTitle(Mage::helper('vendor')->__('Manage Vendor'));
}
protected function _beforeToHtml()
{
$this->addTab('form_section', array(
'label' => Mage::helper('vendor')->__('General Information'),
'title' => Mage::helper('vendor')->__('General Information'),
'content' => $this->getLayout()->createBlock('vendor/adminhtml_list_edit_tab_form')->toHtml(),
));
$this->addTab('vendor_details',array(
'label'=>Mage::helper('vendor')->__('Vendor Store Details'),
'title'=>Mage::helper('vendor')->__('Vendor Store Details'),
'content'=>$this->getLayout()->createBlock('vendor/adminhtml_list_edit_tab_storedetails')->toHtml(),
));
return parent::_beforeToHtml();
}
}
after the form.php
<?php
class Amit_Vendor_Block_Adminhtml_List_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$vendor = Mage::registry('vendor_data');
$form = new Varien_Data_Form();
$fieldset = $form->addFieldset('vendor_form', array(
'legend' => Mage::helper('vendor')->__('Vendor Registration')
));
$fieldset->addField('name', 'text', array(
'name' => 'name',
'label' => Mage::helper('vendor')->__('Name'),
'required' => true,
));
$fieldset->addField('email', 'text', array(
'name' => 'email',
'label' => Mage::helper('vendor')->__('Email'),
'required' => true,
));
$fieldset->addField('user_name', 'text', array(
'name' => 'user_name',
'label' => Mage::helper('vendor')->__('User name'),
'required' => true,
));
$fieldset->addField('password', 'password', array(
'name' => 'password',
'class' => 'required-entry',
'label' => Mage::helper('vendor')->__('Password'),
'required' => true,
));
$this->setForm($form);
$form->setValues($vendor->getData());
return parent::_prepareForm();
}
public function filter($value)
{
return number_format($value, 2);
}
}
Second form Storedetails.php
<?php
class Amit_Vendor_Block_Adminhtml_List_Edit_Tab_Storedetails extends Mage_Adminhtml_Block_Widget_Form{
protected function _prepareForm(){
$vendorStore = Mage::registry('vendor_store_details');// new registry for different module
$form = new Varien_Data_Form();
//$form->setFieldNameSuffix('vendor_store');
$fieldset = $form->addFieldset('vendor_form', array(
'legend' => Mage::helper('vendor')->__('Vendor deatsilsn')
));
$fieldset->addField('alternative_email','text',array(
'name' =>'alternative_email',
'label' => Mage::helper('vendor')->__('Alternative Email'),
'required'=> false
));
$fieldset->addField('shopname','text',array(
'name' =>'shopname',
'label' => Mage::helper('vendor')->__('Shop Name'),
'required'=> true,
'class' => 'required-entry',
));
$fieldset->addField('company', 'text', array(
'name' => 'company',
'label' => Mage::helper('vendor')->__('Company'),
'required' => true,
'class' => 'required-entry',
));
$fieldset->addField('street','text',array(
'name' =>'vendor_details[street]',
'label' => Mage::helper('vendor')->__('Street Address'),
'required'=> false
));
$this->setForm($form);
$form->addValues($vendorStore->getData());
return parent::_prepareForm();
}
}
I try to create new attribute with type select in the installer. But I don't know whether it's possible to create such an attribute without setting any options. Here is my code:
$installer = $this;
$config = array(
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'input' => 'select',
'label' => 'Theme',
'type' => 'varchar',
'apply_to' => null,
'searchable' => false,
'required' => false,
'user_defined' => 1,
'option' => array(
'values' => array()
)
);
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'custom_attr', $config);
Attribute is created but it has text type. Please help!
I created a custom multiselect product attribute through installer. It works and I can save the product if I only select one option from the multiselect values. But if I select 2 values, the product still can be saved but came back up with 1 selected value again. In short, I can't save the attribute with 2 selected values.
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'attr_id',array(
'label' => 'Frontend Name',
'type' => 'int',
'input' => 'multiselect',
'backend' => 'eav/entity_attribute_backend_array',
'frontend' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'option' => array (
'value' => array(
'0' => array('First Option'),
'1' => array('Second Option'),
'2' => array('Third Option'),
)
),
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false
));
$installer->endSetup();
The problem comes from the type of your attribute.
'type'=> 'int',
The values from multiselect attributes are saved concatenated by comma 1,4,6. For this you need the attribute to be varchar or text. I recommend varchar if you are not going to have hundreds of options for the attribute.
The way is configured now, when it's saved, the value 1,4,6 is converted to int and it ends up being 1.
Modify you option array from
'option' => array (
'value' => array(
'0' => array('First Option'),
'1' => array('Second Option'),
'2' => array('Third Option'),
)
),
to
'option' => array (
'value' => array(
'first_option' => array('First Option'),
'second_option' => array('Second Option'),
'third_option' => array('Third Option'),
)
),
Multiselect will accept associated array.
I have found the solution myself .
open app/code/core/Mage/Adminhtml/controllers/Catalog/CategoryController.php
on save action after this
$category->setAttributeSetId($category->getDefaultAttributeSetId());
Please change language is your attribute name . you can change attribute name accordingly
$ga = "";
if($data['general']['language']){
foreach($data['general']['language'] as $a){
$ga .= $a.",";
}
$category->setLanguage(substr_replace($ga, "", -1));
}
Please replace language to your attribute name and it works...**
I am using SOAP API for entering products in magento shops. here is the full code
In the case of multiselect custom attribute.
$arrProductTime = explode(',', '136,139');
$result = $client->catalogProductCreate($session, 'simple', $attributeSet->set_id, 'product_sku1234', array(
'categories' => array(36),
'websites' => array(1),
'name' => 'my_pdt1008',
'description' => 'my_pdt1',
'short_description' => 'my_pdt1000',
'weight' => '11',
'status' => '1',
'url_key' => 'product-url-key1',
'url_path' => 'product-url-path1',
'visibility' => '4',
'price' => '100',
'tax_class_id' => 1,
'meta_title' => 'Product meta title1',
'meta_keyword' => 'Product meta keyword1',
'meta_description' => 'Product meta description1',
'stock_data' => array('qty'=>'100','is_in_stock'=>1,'manage_stock'=>1),
'additional_attributes' => array('multi_data' => array(array('key' => 'product_time', 'value' => $arrProductTime)))
));
I have faced a problem in my custom category attribute. It does not save the multiselect value in data base and does not show multiselect values on the category backend admin.
<?php
require_once("app/Mage.php");
Mage::app('default');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$entityTypeId = $installer->getEntityTypeId('catalog_category');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$installer->addAttribute('catalog_category', 'cutomcity', array(
'label' => 'Test Select',
'type' => 'varchar',
'input' => 'multiselect',
'visible' => true,
'user_defined' => true,
'required' => false,
'position' => 80,
'visible_on_front' => false,
'group' => 'General Information',
'input' => 'multiselect',
'backend_model'=>'eav/entity_attribute_backend_array'
'source' => 'GA_Multiattribute_Helper_Testsource'
// eav/entity_attribute_source_table Even if i use this untill it does not show selected value on the Multiselect
));
?>
This is my helper file code
<?php
class GA_Multiattribute_Helper_Multiattributesource extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
protected $_optionsDefault = array();
public function getAllOptions($withEmpty = true, $defaultValues = false)
{
$collection = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*');;
$customers = array();
foreach($collection as $cust)
{
$fname = $cust->getFirstname();
$lname = $cust->getLastname();
$id = $cust->getId();
$customers[] = array('value'=>"$id$fname", 'label'=>"$fname $lname");
}
return $customers;
}
} ?>
I already have spent all day on this and I think I can't get this working because the possibility to add custom EAV attributes to orders was removed.
At least, I've noticed that sales_order_entity is missing.
Well, what I try to do is to add a custom field to sales orders. I thought it would work the same way as for category products, but looks like nope.
My overall point in doing all this is because I want to track who is adding products to catalog and want to relate certain order with certain user (not customer).
public function getDefaultEntities()
{
return array(
'catalog_product' => array(
'entity_model' => 'catalog/product',
'attribute_model' => 'catalog/resource_eav_attribute',
'table' => 'catalog/product',
'additional_attribute_table' => 'catalog/eav_attribute',
'entity_attribute_collection' => 'catalog/product_attribute_collection',
'attributes' => array(
'seller_id' => array(
'group' => 'MyCustom',
'label' => 'Seller ID',
'type' => 'int',
'input' => 'text',
'default' => '0',
'class' => '',
'backend' => '',
'frontend' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => false,
'required' => true,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => false,
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false,
),
),
),
'order' => array(
'entity_model' => 'sales/order',
'table' => 'sales/order',
'increment_model' => 'eav/entity_increment_numeric',
'attributes' => array(
'seller_id' => array(
'group' => 'MyCustom',
'label' => 'Seller ID',
'type' => 'int',
'input' => 'text',
'default' => '0',
'class' => '',
'backend' => '',
'frontend' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => false,
'required' => true,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => false,
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false,
),
),
),
);
}
It works for products, but not for orders. I have required entries in eav_attribute table.
I don't know if I'm doing something wrong or this is just impossible to do?
I also thought about solving this different way by creating additional table to track relations between user - order|product. This would require more work tough.
I know this is an old post but I came across it when I was looking for answers to the same problem, so thought I would share my solution.
Lets take adding an attribute to a sales order as an example.
Firstly, Magento no longer users EAV entities for Sales, if you look at core/Mage/Sales/etc/config.xml
<sales>
<class>Mage_Sales_Model</class>
<resourceModel>sales_resource</resourceModel>
</sales>
resouceModel no longer points at sales_entity(EAV), it now points at sales_resource(flat). If you look at the children of the sales_resource node, you will find the order node:
<order>
<table>sales_flat_order</table>
</order>
This means that a Mage_Sales_Model_Order has a resource model of Mage_Sales_Model_Resource_Order which has a table of sales_flat_order.
The magento developers have provided the class Mage_Sales_Model_Resource_Setup which allows you to add attributes to this new "flat" structure in pretty much the same way you would have added attributes to the EAV structure. If you take a look inside Mage_Sales_Model_Resource_Setup you will see the following function:
/**
* Add entity attribute. Overwrited for flat entities support
*
* #param int|string $entityTypeId
* #param string $code
* #param array $attr
* #return Mage_Sales_Model_Resource_Setup
*/
public function addAttribute($entityTypeId, $code, array $attr)
{
if (isset($this->_flatEntityTables[$entityTypeId]) &&
$this->_flatTableExist($this->_flatEntityTables[$entityTypeId]))
{
$this->_addFlatAttribute($this->_flatEntityTables[$entityTypeId], $code, $attr);
$this->_addGridAttribute($this->_flatEntityTables[$entityTypeId], $code, $attr, $entityTypeId);
} else {
parent::addAttribute($entityTypeId, $code, $attr);
}
return $this;
}
With this information you should now see that it is simply a case of getting an instance of Mage_Sales_Model_Resource_Setup and calling its public addAttribute method with valid parameters.
Code snippet of adding a franchise_id attribute to a sales order:
$salesResourceSetupModel = Mage::getModel('sales/resource_setup', 'core_setup');
$data=array(
'type'=>'int',
'input'=>'text',
'label'=>'Franchise',
'global'=> 1,
'is_required'=>'0',
'is_comparable'=>'0',
'is_searchable'=>'0',
'is_unique'=>'0',
'is_configurable'=>'0',
'user_defined'=>'1',
//whether it should be including in the sales order grid
'grid'=>1
);
//first param should relate to a key of the protected $_flatEntityTables array
$salesResourceSetupModel->addAttribute('order', 'franchise_id', $data);
For Versions > 1.4.1.0 you will have to create a column in the sales_flat_order table. You can take a look in this post
is magento sales eav