I am currently trying to add a new category attribute to the category admin screen in Magento 1.8.1, I am having issues with getting anything to show though.
The only code examples I can find include mysql4, however I thought this had been retired? Please can anyone point us in the right direction here.
I can see my extension under Config > Advanced and in the core_resources table. But not in the front end of the site.
We've tried this recently with 1.8.2.0. You don't really need to create a module just to add a category attribute, once. It seems such a waste to go through so much file cruft to get something installed just once.
Category attributes tend to stay permanently once installed so what worked better for us is to just use a one-off script. Save this one right at magento root.
<?php
require_once "app/Mage.php";
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$installer = new Mage_Sales_Model_Mysql4_Setup;
// change details below:
$attribute = array(
'type' => 'int',
'label'=> 'Your attribute label',
'input' => 'text',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => "",
'group' => "General Information"
);
$installer->addAttribute('catalog_category', 'your_attribute_code', $attribute);
$installer->endSetup();
Save it as add_category_attribute.php or something else memorable for you.
You can either use the browser to get to this file, or use php-cli to run this:
php -f add_category_attribute.php
Good luck.
Change the file name from mysql4-install-0.0.1.php to install-0.0.1.php
#h3nr1ke You can get attribute with:
$category = Mage::registry('current_category');
if ($category){
$value = $category->getData('YOUR_CUSTOM_ATTRIBUTE_CODE');
}
Run this script in your magento root folder to create Attribute
<?php
require_once('app/Mage.php');
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$entityTypeId = $installer->getEntityTypeId('catalog_category');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
if (!$installer->getAttributeId($entityTypeId, 'shipping_content')) {
$installer->addAttribute('catalog_category', 'shipping_content', array(
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Short description',
'input' => 'textarea',
'class' => '',
'source' => '',
'global' => '0',
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => false,
'unique' => false,
'wysiwyg_enabled' => true,
'apply_to' => '',
'is_configurable' => true
));
$installer->updateAttribute($entityTypeId, 'shipping_content', 'is_wysiwyg_enabled', 1);
$installer->updateAttribute($entityTypeId, 'shipping_content', 'is_html_allowed_on_front', 1);
}
$installer->endSetup();
?>
For Remove Category Attribute
<?php
require_once('app/Mage.php');
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$installer = new Mage_Sales_Model_Mysql4_Setup;
$installer->startSetup();
$installer->removeAttribute('catalog_category', 'shipping_content');
$installer->endSetup();
?>
Related
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 don´t get to understand the complety behavior os the collection in Magento. next, I´ll describe the problem:
For requirements of the projects, I need to add a custom attribute to exclude a severals products in the feed at GoogleShopping. Then I add this attribute with php script installation
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'in_googleshopping_feed', array(
'group' => 'General',
'type' => 'int',
'input' => 'select',
'label' => 'In GoogleShoppint feed',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => 1,
'required' => 0,
'default' => 1,
'visible_on_front' => 0,
'is_html_allowed_on_front' => 0,
'sort_order' => 32,
'is_configurable' => 0,
'source' => 'eav/entity_attribute_source_boolean',
'searchable' => 0,
'filterable' => 0,
'comparable' => 0,
'unique' => false,
'user_defined' => false,
'is_user_defined' => false,
'used_in_product_listing' => true
)
);
$installer->endSetup();
Next in a Observer I try retrieve the value of this with:
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
//->addAttributeToSelect('in_googleshopping_feed');
->addAttributeToFilter('in_googleshopping_feed',0);
This is my doubt, Why haven´t the collection this attribute?
However, I can retrieve the value of the Product_Model whit the next snippet:
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*');
$prodIds=$products->getAllIds();
foreach($prodIds as $productId):
$product = Mage::getModel('catalog/product')->setStoreId('1');
$product->load($productId);
var_dump($product->getData('in_googleshopping_feed'));
endforeach;
Then, My huge doubt is: Why I can´t filter the collection by my new attribute?
I think with the method addAttributeToSelect('*') all fields are added to the collection.
Can someone help my?
Thanks
The first that falls into glance is that 2nd parameter for addAttributeToFilter have to be array. Like this:
addAttributeToFilter('in_googleshopping_feed', array('eq', 0));
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
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?