Can't save multiselect attribute on Magento - magento

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;
}
} ?>

Related

Create attribute with type "select" but without options

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!

Create Magento coupons progmatically with expiry dates of 30 days from now

I need to Create Coupons in Magento on Action of account Create.
I have created observer which is executing properly on account Create but i am Struggling with Coupons create. I have fixed coupon names on of fixed amount on applicable on cart subtotal.
I found many ans but none of them has anything about expiry of coupon
No replies yet :(
Though I found the solution.
$customer = $observer->getCustomer()->getData();
$email = $customer['email'];
$email100 = $email.'-100-1';
$datei=date('d/m/Y');
$datee = date('d/m/Y', strtotime('1 day'));
$firstname=$customer['firstname'];
$couponcode1=$email.'100-1';
$data = array(
'product_ids' => null,
'name' => sprintf('coupon-100-1', Mage::getSingleton('customer/session')->getCustomerId()),
'description' => null,
'is_active' => 1,
'website_ids' => array(1),
'customer_group_ids' => array(1),
'coupon_type' => 2,
'coupon_code' => $email100,
'uses_per_coupon' => 1,
'uses_per_customer' => 1,
'from_date' => $datei,
'to_date' => $datee,
'sort_order' => null,
'is_rss' => 1,
'test' => 'test',
'rule' => array(
'conditions' => array(
'1' =>array(
'type' => 'salesrule/rule_condition_combine',
'aggregator' => 'all',
'value' => 1,
),
'1--1' => array(
'type' => 'salesrule/rule_condition_combine',
'aggregator' => 'all',
'value' => 1,
),
'1--1--1' =>array(
'type' => 'salesrule/rule_condition_address',
'attribute' => 'base_subtotal',
'operator' => '>=',
'value' => '400'
),
'1--1--2' =>array(
'type' => 'salesrule/rule_condition_combine',
'aggregator' => 'all',
'value' => 0
),
'1--1--2--1' =>array(
'type' => 'salesrule/rule_condition_product_found',
'value' => 1,
'aggregator' => 'all'
),
'1--1--2--1--1' => array(
'type' => 'salesrule/rule_condition_product',
'attribute' => 'special_price',
'operator' => '>=',
'value' => 0
)
),
'actions' => array(
'1' =>array(
'type' => 'salesrule/rule_condition_combine',
'aggregator' => 'all',
'value' => 1,
'new_child' => null
),
)
),
'simple_action' => 'cart_fixed',
'discount_amount' => 100,
'discount_qty' => 0,
'discount_step' => null,
'apply_to_shipping' => 0,
'simple_free_shipping' => 0,
'stop_rules_processing' => 0,
'store_labels' => array()
);
$model = Mage::getModel('salesrule/rule');
$validateResult = $model->validateData(new Varien_Object($data));
if ($validateResult !== true) {
foreach($validateResult as $errorMessage) {
// print_r($errorMessage);
$session->addError($errorMessage);
}
}
if (isset($data['rule']['conditions'])) {
$data['conditions'] = $data['rule']['conditions'];
}
if (isset($data['rule']['actions'])) {
$data['actions'] = $data['rule']['actions'];
}
unset($data['rule']);
//print_r($data);
$model->loadPost($data);
$model->save();
Here the name of coupon code is email id followed by -100-1.
$datei and $datee are the initial date and the expiry date. I have customized the coupon code so that it is not being used if the cart contains special price item.
Fixed amount discount is applied on the complete cart subtotal.
In Case of query do respond.

How to select multiple check box in edit grid section of magento

http://magento.localhost.com/index.php/arithmetic/adminhtml_arithmetic/edit/id/5/key/c03c12d4c338a2e4cdbb93c3d9e511a93401d19b21a13ea77cffda20cac94577/
This is what my link looks like. I am getting all values by the ID, in the edit grid page
there is a section for multiple check boxes. How can I select all the check boxes according to the result array
$fieldset-> addField('st_user_interest', 'checkboxes', array(
'label' => Mage::helper('arithmetic')->__('Interest'),
'required' => true,
'name' => 'st_user_interest[]',
'values' => array(
array(
'label' => Mage::helper('arithmetic')->__('Education'),
'value' => 'education',
'class' => 'required-one',
),
array(
'label' => Mage::helper('arithmetic')->__('Business'),
'value' => 'business',
'class' => 'required-one',
),
array(
'label' => Mage::helper('arithmetic')->__('Marketing'),
'value' => 'marketing',
'class' => 'required-one',
),
array(
'value' => 'investment',
'label' => Mage::helper('arithmetic')->__('Investment'),
'class' => 'required-one',
)
),
));
Thanks
Hi at the time of storing array field value we are storing as string after converting array to string,
So at the time of setValues() Magento looking for that same input field value as array to check the check boxes
Trick is that convert that stored string value into array and assign to that column field that will work
Package_Arithmetic_Block_Adminhtml_Arithmetic_Edit_Tab_Form
protected function _prepareForm()
{
$id = $this->htmlEscape(Mage::registry('arithmetic_data')->getIn_user_id());
$model = Mage::getModel("arithmeti/newuser")->load($id);
/* here is the stuff witch converting the stored string to array and set in st_user_interest */
$interest = $model->getSt_user_interest();
$model->setSt_user_interest(explode(',',$interest));
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('arithmetic_form', array('legend'=>Mage::helper('arithmetic')->__('User information')));
$fieldset-> addField('st_user_interest', 'checkboxes', array(
'label' => Mage::helper('arithmetic')->__('Interest'),
'required' => true,
'name' => 'st_user_interest[]',
'values' => array(
array(
'label' => Mage::helper('arithmetic')->__('Education'),
'value' => 'education',
),
array(
'label' => Mage::helper('arithmetic')->__('Business'),
'value' => 'business',
),
array(
'label' => Mage::helper('arithmetic')->__('Marketing'),
'value' => 'marketing',
),
array(
'value' => 'investment',
'label' => Mage::helper('arithmetic')->__('Investment'),
)
),
));
if ( Mage::getSingleton('adminhtml/session')->getArithmeticData() )
{
$form->setValues(Mage::getSingleton('adminhtml/session')->getArithmeticData());
Mage::getSingleton('adminhtml/session')->setArithmeticData(null);
} elseif ( $model->getData() ) {
//Mage::registry('arithmetic_data')->getData(); /* removing this line and adding $model->getData() inslde the $form->setValues() */
$form->setValues($model->getData());
}
return parent::_prepareForm();
}

Add new tab page in admin category - Magento

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?

Why doesn't the view use the database table I defined in hook_views_data()?

I have a problem with my module views integration. I need to provide information about user who added video and timestamp. Video field is a CCK Embedded Media Field and it stores in content_field_3d_party_video table.
The schema is defined from the following code.
function MODULE_schema() {
$schema = array();
$schema['video_data'] = array(
'description' => t('Users and timestamps for video field'),
'fields' => array(
'value' => array(
'description' => t('Emfield value'),
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'default' => '',
),
'uid' => array(
'description' => t('User id'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'timestamp' => array(
'description' => t('Timestamp'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('value'),
'indexes' => array(
'timestamp' => array('timestamp'),
'uid' => array('uid'),
),
);
return $schema;
}
The hook_views_data() implementation is the following.
function MODULE_views_data() {
$data = array();
$data['video_data']['table']['group'] = t('Video Data');
$data['video_data']['table']['join'] = array(
'node' => array(
'left_table' => 'content_field_3d_party_video',
'left_field' => 'field_3d_party_video_value',
'field' => 'value',
),
'users' => array(
'left_field' => 'uid',
'field' => 'uid',
),
);
$data['video_data']['value'] = array(
'title' => t('Video value'),
'relationship' => array(
'base' => 'content_field_3d_party_video',
'base field' => 'field_3d_party_video_value',
'field' => 'value',
'handler' => 'views_handler_relationship',
'label' => t('Video value'),
),
);
$data['video_data']['uid'] = array(
'title' => t('User id'),
'relationship' => array(
'base' => 'users',
'base field' => 'uid',
'field' => 'uid',
'handler' => 'views_handler_relationship',
'label' => t('User id'),
),
);
$data['video_data']['timestamp'] = array(
'title' => t('Timestamp field'),
'field' => array(
'handler' => 'views_handler_field_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
);
return $data;
}
The table isn't included in the SQL query generated for the view.
SELECT node.nid AS nid,
node.title AS node_title,
node.language AS node_language,
node_data_field_3d_party_video.field_3d_party_video_embed AS node_data_field_3d_party_video_field_3d_party_video_embed,
node_data_field_3d_party_video.field_3d_party_video_value AS node_data_field_3d_party_video_field_3d_party_video_value,
...
node.type AS node_type,
node.vid AS node_vid
FROM node node
LEFT JOIN content_field_3d_party_video content_field_3d_party_video_video_data ON value = content_field_3d_party_video_video_data.field_3d_party_video_value
LEFT JOIN users users_video_data ON uid = users_video_data.uid
LEFT JOIN content_field_3d_party_video node_data_field_3d_party_video ON node.vid = node_data_field_3d_party_video.vid
WHERE ...
May you help me?
This is the solution the OP previously posted in the question.
To correctly join the table used for a CCK field, we must specify node_data_field_3d_party_video as left_table, instead of content_field_3d_party_video.
$data['video_data']['table']['join'] = array(
'node' => array(
'left_table' => 'node_data_field_3d_party_video',
'left_field' => 'field_3d_party_video_value',
'field' => 'value',
),
'users' => array(
'left table' => 'users',
'left_field' => 'uid',
'field' => 'uid',
),
);

Resources