I'd like to add an attribute to products, and I want to do this programmatically. So I added a mysql-install-0.1.0.php to a module of mine, and I added this (inspired from existing examples) :
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'collection', array(
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'Collection',
'input' => 'text',
'class' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => false,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'apply_to' => '',
'is_configurable' => false
));
$installer->endSetup();
Syntax seems OK, but when it comes to execute this part of the code, here is the problem :
Fatal error: Call to undefined method
Mage_Core_Model_Resource_Setup::addAttribute()
in
/home/frleq/Dev/projets/AVIP/WORKSPACE/avip_magento/app/code/local/Smile/Magentaho/sql/magentaho_setup/mysql4-install-0.1.0.php
on line 7
Do you see what's wrong? Code isn't so complicated, and it is inspired from existing and worling ones...
Thank you
You are using the wrong setup class. Check your setup class declaration in config.xml. You are using Mage_Core_Model_Resource_Setup. Try Mage_Eav_Model_Entity_Setup instead.
If you want to use product-related options (filterable, searchable etc.), you should use Mage_Catalog_Model_Resource_Eav_Mysql4_Setup class.
In latest versions of Magento you should use Mage_Catalog_Model_Resource_Setup class.
Related
I have a problem when applying magento 2.1.9 to my project.
My attribute is ab_size
I have created that attribute with code blow.
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
$entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
foreach ($singleAttributeCodes as $key => $label) {
$categorySetup->removeAttribute($entityTypeId, $key);
$categorySetup->addAttribute(
$entityTypeId,
$key,
[
'type' => 'varchar',
'label' => $label,
'input' => 'select',
'required' => false,
'sort_order' => $sortOrder,
'visible' => true,
'user_defined' => true,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'filterable' => 0,
'visible_on_front' => true,
'used_in_product_listing' => true,
'group' => $group,
'apply_to' => 'simple,configurable,bundle,downloadable,grouped',
]
);
}
With config in the backend.
Enable filter (with result) done
Category Is Anchor done
Reindex done
Enable Category flat done
Enable Product flat done
Reindex all data done
Clear cache done
Use magento clean doesn't have any extension done
Check with attribute color (done it show in navigation)
Product Price Show
Category Show
about my system information
CentOS 7.0
Litespeed
Php7.0
Magento CE 2.1.9
I have debugged that the product collection buckets return empty
I think that problem with creating attribute code, has anyone got the same problem.
Thank anyone have tips.
With select attribute we must use type of int..
I think you have messed your attributes with this code.
Looking at it, it removes all the attributes and adds them but as type varchar, which may not be applicable to all attributes.
I would suggest resetting your Magento database, and then add the attribute using code like the following.
As I do not know what you are planning to use this attribute, some of the settings bellow may not apply to your use case.
$categorySetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'ab_size',
[
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'AB Size',
'input' => 'select',
'class' => '',
'source' => '',
'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => 0,
'searchable' => false,
'filterable' => true,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);
Im trying to implement a wysiwyg editor under text-option in Magentos custom options, but am failing there. I have already searched for several components, but can't get it together though.
I want that editor to appear in the field where the WYSIWYG Value-textarea is right now.
Other source either don't go into detail or aren't working for 1.9.1.
What I have so far: [WR is companyname and EPO is my module]
I've found this snippet, which is used to place a wysiwyg editor on a cms page within a _prepareForm function:
<?php
if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled() && ($block = $this->getLayout()->getBlock('head'))) {
$block->setCanLoadTinyMce(true);
}
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save'),
'method' => 'post'
));
$fieldset = $form->addFieldset('base_fieldset', array(
'legend' => Mage::helper('wr_epo')->__("Some Information"),
'class' => 'fieldset-wide',
));
$wysiwygConfig = Mage::getSingleton('cms/wysiwyg_config');
$fieldset->addField('description', 'editor', array(
'name' => 'description',
'label' => Mage::helper('wr_epo')->__('Description'),
'title' => Mage::helper('wr_epo')->__('Description'),
'style' => 'height: 600px;',
'wysiwyg' => true,
'required' => false,
'config' => $wysiwygConfig
));?>
I had an issue like this no to long ago (client wanted custom stuff in an option).
I dont know if this is exactly what you want. But heres what i did:
I made a php file in the root of my magento.
And then i added this code. I found it on stackoverflow. If anyone knows where it comes from be even better.
ini_set('display_errors',0);
require_once 'app/Mage.php';
Mage::app();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
function createNewAttributeSet($name) {
Mage::app('default');
$modelSet = Mage::getModel('eav/entity_attribute_set')
->setEntityTypeId(4) // 4 == "catalog/product"
->setAttributeSetName($name);
$modelSet->save();
$modelSet->initFromSkeleton(4)->save(); // same thing
}
// Replace your attribute name with "extra_info"
$setup->addAttribute('catalog_category', 'extra_info', array(
'group' => 'General Information',
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Extra Information',
'wysiwyg_enabled' => true,
'visible_on_front' => true,
'is_html_allowed_on_front' => true,
'input' => 'textarea',
'class' => '',
'source' => 'eav/entity_attribute_source_boolean',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => 1,
'required' => 0,
'user_defined' => 0,
'default' => '',
'searchable' => 0,
'filterable' => 0,
'comparable' => 0,
'visible_on_front' => 0,
'unique' => 0,
'position' => 1,
));
$setup->updateAttribute('catalog_category', 'extra_info', 'is_wysiwyg_enabled', 1);
$setup->updateAttribute('catalog_category', 'extra_info', 'is_html_allowed_on_front', 1);
I do however Think you want to include
'wysiwyg_enabled' => true,
Rather than
'wysiwyg' => true,
(this is refering to your code you pasted earlier)
Other reading:
Things i found that helped with my issues:
https://www.atwix.com/magento/add-category-attribute/
https://docs.magento.com/m1/ce/user_guide/catalog/product-options-custom.html
I've written a module that adds an attribute to the customer model, and it's worked fine.
I wanted to extend the sales/order_shipment model and add another attribute, so I just used the same reference that I had used for the installer for adding the new customer attribute.
I relied on Magento finding and installing the module a file with the following path and name app/code/local/[namespace]/[module_name]/sql/install-01.php
I've set the installer to the value of $this during the setup of the module initially:
$installer = $this;
$installer->startSetup();
I kept the same reference for the installer and just added a new field:
$installer->addAttribute('customer','sap_bp_id',
array(
'type' => 'varchar',
'label' => 'SAP Business One Business Partner ID',
'input' => 'text',
'required' => false,
'visible' => true,
'visible_on_front' => false,
'global' => 1,
'position' => 62,
)
);
$installer->addAttribute('shipment','sap_doc_num',
array(
'type' => 'varchar',
'label' => 'SAP Business One Document Number',
'input' => 'text',
'required' => false,
'visible' => true,
'visible_on_front' => false,
'global' => 1,
'position' => 62,
)
);
It's worked just fine, but I want to know this:
Is this the correct method for adding a new attribute for different models in the same installation file? Is there a better method? Have I missed anything?
I've searched around quite a bit for this answer and haven't been able to trace down the exact setting I need. I'm reaching out to see if anyone can help.
I'm writing a Magento extension to add some attributes to my installation. Everything is fine with the exception of one intricacy. I cannot set the attribute's "Use In Layered Navigation" property to "Filterable (no results)".
I can use the values in the attribute array in my installer file (below) to set that property to either "No" (0 value) or "Filterable (with results)" (1 value) but not without results.
Anyone have a suggestion of a property I may be missing or set incorrectly in my array?
Very much appreciated!
<?php
...
// Add the mm_framestyle attr. (filterable, non-super attr.)
$setup->addAttribute('catalog_product', 'mm_framestyle', array(
'backend' => 'eav/entity_attribute_backend_array',
'visible' => true,
'required' => false,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => true,
'label' => 'Frame Types',
'group' => 'MyMaui Attributes',
'type' => 'varchar',
'input' => 'select',
'global' => false,
'option' => array (
'value' => array('maui_flex' => array('MAUI FLEX'),
'full_frame_metal' => array('FULL FRAME'),
'rimless_metal' => array('RIMLESS'),
'shields' => array('SHIELDS'),
)
),
'visible_on_front' => true,
'unique' => false
));
...
?>
To set the is_filterable property to "Filterable (no results)", your configuration array should have filterable set to 2.
If you wish to use an update script to change the previously-established setting, the syntax would be as follows:
$setup->updateAttribute('catalog_product', 'mm_framestyle', 'is_filterable', 2);
I am using magento V1.5. I am working on Customer EAV & have tried to create another EAV module.
I have added few attributes in customer entity. Now MY requirement is those attributes must be editable from frontend as well as backend.
Please tell me how to add those attributes in forntend form (customer edit form). & Tell me what to do in backend to have those options to be editable.
I am thinking if there is a way in admin like in our Form.php we prepare form with adding elements to it. then we not need to write a code to create actual html. Magento automatically does it. SO idea is it must also load the new attributes in that was just added. (like they appear in product edit.)
Second Issue is, Can u guys tell me what should I write in my Grid.php >> prepareCollection (for other EAV module ). so that it must get all the attributes with their values ( or may be few )
here is something that I have in my Grid.php but its not working
protected function _prepareCollection()
{
$collection = Mage::getModel('pincodes/eavpincodes')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
& this is my collection file
class Namespace_Pincodes_Model_Resource_Eav_Mysql4_Eavpincodes_Collection extends Mage_Eav_Model_Entity_Collection_Abstract
{
protected function _construct()
{
$this->_init('pincodes/eavpincodes');
}
}
But its not returning anything in my grid
& here is my Attribute collection file
class Inkfruit_Pincodes_Model_Resource_Eav_Mysql4_Attribute_Collection extends Mage_Eav_Model_Mysql4_Entity_Attribute_Collection
{
public function _construct()
{
$this->_init('pincodes/resource_eav_attribute', 'eav/entity_attribute');
}
protected function _initSelect()
{
$this->getSelect()->from(array('main_table' => $this->getResource()->getMainTable()))
->where('main_table.entity_type_id=?', Mage::getModel('eav/entity')->setType('pincodes_eavpincodes')->getTypeId())
->join(
array('additional_table' => $this->getTable('pincodes/eavpincodes')),
'additional_table.attribute_set_id=main_table.attribute_id' // I think this sql need to be changed but I have no idea what it'll be
);
return $this;
}
}
Guys thank you so much. This forum has been specially very helpful to me
Regards
SAM
Ok guys I have created a separate module for customers that has one mysql4_install file & it goes as follows
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('customer', 'profile_image', array(
'label' => 'Profile Image',
'type' => 'varchar',
'input' => 'file',
'visible' => true,
'required' => false,
'user_defined' => true,
));
$setup->addAttribute('customer', 'mobile', array(
'label' => 'Mobile Number',
'type' => 'int',
'input' => 'text',
'visible' => true,
'required' => false,
'user_defined' => true,
));
$installer->endSetup();
$installer->installEntities();
But when I hit the url for this module I see no error but these attributes are not in my database.
I also have created Entity_Setup file & it goes as follows I think its incorrect but I gave it a try
<?php
class Namespace_Customer_Entity_Setup extends Mage_Eav_Model_Entity_Setup
{
public function getDefaultEntities()
{
return array (
'customer' => array(
'entity_model' => 'customer/customer',
'attribute_model' => 'customer/attribute',
'table' => 'customer/entity',
'attributes' => array(
'profile_image' => array(
//the EAV attribute type, NOT a mysql varchar
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'Profile Image',
'input' => 'file',
'class' => '',
'source' => '',
// store scope == 0
// global scope == 1
// website scope == 2
'global' => 0,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'searchable' => true,
'filterable' => true,
'comparable' => false,
'visible_on_front' => false,
'unique' => false
),
'mobile' => array(
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'Mobile Number',
'input' => 'text',
'class' => '',
'source' => '',
'global' => 0,
'visible' => true,
'required' => true,
'user_defined' => true,
'default' => '',
'searchable' => true,
'filterable' => true,
'comparable' => false,
'visible_on_front' => false,
'unique' => false
),
),
)
);
}
}
But I see nothing, not any new attribute in database.
Can u guys help here whats wrong??
Thanks
Guys here is My solution. the above answers worked for adding attribute to database only
My question has three parts.
1. Adding attributes to entity in database.
Ans. the above install scripts worked. using a Setup.php & mysql_install/ mysql_upgrade script.
2. Attribute should be editable from Frontend
Ans. We need to modify the file app/design/fontend/default//template/customer/form/edit.phtml
3. Attribute must be editable from Backend
Ans. for that we need to add this snippet in our config.xml
<global>
<fieldsets>
<customer_account>
<create>1</create><update>1</update>
</customer_account>
</fieldsets>
</global>
this will do everything
Hope this will help somebody
This worked for me, notice the additional lines:
<?php
class Millena_CustomerExportAdditions_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
{
public function getDefaultEntities()
{
return array(
'customer' => array(
'entity_model' =>'customer/customer',
'attribute_model' => 'customer/attribute',
'table' => 'customer/entity',
'additional_attribute_table' => 'customer/eav_attribute',
'entity_attribute_collection' => 'customer/attribute_collection',
'attributes' => array(
'export_status' => array(
//'group' => 'Group/Tab',
'label' => 'Customer Export Status',
'type' => 'int',
'input' => 'select',
'default' => '0',
'class' => '',
'backend' => '',
'frontend' => '',
'source' => 'millena_customerExportAdditions/customer_attribute_source_exportStatus',
'global' => 2, //global scope
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false
)
)
)
);
}
}
Install script is simply:
<?php
$installer = $this;
$installer->installEntities();
Having same issue here. Using addAttribute in 1.5.0.1 seems to add it into database but customer admin won't render it.
This method works fine with product and category attributes. And in 1.4.0.0 it works for customers too.
In Magento chagelog there is bullet point under improvements about customer attribute rendering. I'll start checking it next.