I have created an custom attribute using the Magento installer script below:
$installer = new Mage_Eav_Model_Entity_Setup();
$installer->startSetup();
$installer->addAttribute('customer', 'organisation_id', array(
'input' => 'select', //or select or whatever you like
'type' => 'int', //or varchar or anything you want it
'label' => 'Organisation ID',
'visible' => 1,
'required' => 0, //mandatory? then 1
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));
$installer->endSetup();
On the onepage checkout screen I have a dropdown where a customer selects an organisation from a dropdown menu - once they click the 'Continue' button it posts an ajax request and goes to the next step 'Billing' e.g the post request looks like this..
org[organisation_id] - 12345
org[name] - ACME Inc
My function that deals with this is follows - can anyone assist how to add this to the customer & order object?
public function saveOrgAction()
{
if ($this->_expireAjax()) {
return;
}
if ($this->getRequest()->isPost()) {
$data = $this->getRequest()->getPost('org', array());
// todo:hook up saving the org id passed here to customer & order object
// UPDATE... I believe this is correct?
$customer = Mage::getSingleton('customer/session')->getCustomer(); // get customer object
$customer->setOrganisationId($org_id)->save();
if (!isset($result['error'])) {
$result['goto_section'] = 'billing';
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
}
What is the best way to add this 'organisation_id' attribute to the customer object (note I am using Magento 1.7.2)
$customer->setOrganisationId('123')->save();
if you need it in the order you have to set it in the current quote object
via
$quote->setOrganisationId('123');
and use this explanation to push the attribute automatically through quote to order conversion. But dont forget to create the correct labled column in sales_flat_order!
explanation how to push any attribute automatically through quote to order conversion
Related
I have created a custom module in Magento2 where currently I'm selecting a customer and entered multiple order numbers, it will saved into a custom table in the database.
Currently I have to entered order numbers in a textarea but now I want to display all orders after choosing the customer which are not in Complete status.
From this section admin choose orders by clicking on the right side checkbox and Saved it.
Can anyone help me to solve this problem?
Existing Module’s Layout
Want to modify the above layout to the following one: In this desired system when admin change the customer name order numbers associated with that customer will be listed.
New invoice
Code I have used to create the customer dropdown field:
$fieldset->addField('customer_id', 'select', array(
'label' => __('Customer'),
'name' => 'customer_id',
'title' => __('Customer'),
'values' => $this->getCustomerOptionArray()
));
protected function getCustomerOptionArray()
{
$options = array();
$options[] = array(
'value' => 0,
'label' => __('Select Customer'),
);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\Customer')->getCollection();
foreach ($customerObj as $customerObjdata ){
$options[] = array(
'value' => $customerObjdata->getId(),
'label' => $customerObjdata->getName(),
);
}
return $options;
}
I tried to search this but couldn't find any. When creating a custom product attribute with select type programmatically, Magento always assigns eav/entity_attribute_source_table as the source model.
There are 2 issues with this default source model:
I can't auto populate the field with data taken programmatically from somewhere else other than have to type the data list manually one by one.
Although I have specified the "default" or "default_value" (I can see in the database that the value is there), the field is still showing empty as the first line.
How I can change the default source_model to my own source model for select type?
Thank you
The key you are looking for is to pass a source value in your SQL setup. Make sure your $installer is an EAV setup object.
You would do the following in your setup script:
$installer = $this;
$installer->starSetup();
// Setup customer multiselect attribute
$attr = array(
'backend' => 'eav/entity_attribute_backend_array',
'input' => 'multiselect',
'label' => 'Permissions',
'note' => 'Used for group-based frontend permissions.',
'required' => false,
'sort_order' => '1000',
'source' => 'eav/entity_attribute_source_table', // Change it here
'user_defined' => true
);
$installer->addAttribute('customer', 'permissions', $attr);
// Add options for permissions
$options = array(
'attribute_id' => $installer->getAttributeId('customer', 'permissions'),
'value' => array(
'place_order' => array('Can Place Orders'),
'view_catalog' => array('Can View the Catalog'),
)
);
$installer->addAttributeOption($options);
$installer->endSetup();
Utimately, I believe the source model can be anything that provides a toOptionArray() function.
There is a great example of this in Mage_Customer, installer: mysql4-upgrade-1.5.9.9-1.6.0.0.php
In it, a country source model is being assigned to customer address attribute country_id.
$installer->updateAttribute(
'customer_address',
'country_id',
'source_model',
'customer/entity_address_attribute_source_country'
);
Change this to catalog_product, your attribute, and source model.
You set the source type as shown below.
'source' => 'categoryattr/attribute_source_type',
and create the file Attribute\Source\Type.php and the create the options and set the value 0 for default option.
$this->_options[] = array (
'label' => 'Select Category',
'value' => '0'
);
please refer below for file structure and step by step explanation.
http://www.pearlbells.co.uk/how-to-create-custom-attribute-source-type-in-magento/
Ok so I want to allow the end user (who most likely won't know anything about code) to be able to change the theme colors I'm creating. I saw you can create custom variables, but it's not really efficient to put your CSS as a php file.
So how do I go about this where the user can just put the hex code of a color for the navigation background or button background or whatever.. via the admin panel?
One way of doing this is by adding a customer attribute and letting your customer change it.
Adding a customer attribute can be achieved with this sort of method:
public function addCustomerAttribute($params){
try{
$setup = Mage::getModel('customer/entity_setup', 'core_setup');
$setup->addAttribute('customer', $params['code'], array(
'type' => $params['type'] ,
'input' => $params['input'],
'label' => $params['name'],
'global' => $params['scope'],
'visible' => 1,
'required' => $params['required'],
'user_defined' => 1,
'default' => $params['defaultvalue'],
'visible_on_front' => $params['visible_on_front'],
'used_in_forms', array('adminhtml_customer','customer_account_edit')
));
Mage::getSingleton('eav/config')
->getAttribute('customer', $params['code'] )
->setData(
'used_in_forms', array('adminhtml_customer','customer_account_edit')
)
->save();
return true;
}catch(Exception $e){
throw new Exception('Probleme de création d\'attribut client ' . $e->getMessage());
}
}
Then if your customer is logged in, just let him change the value and save it.
You can then retrieve it with
$customerColor = Mage::getSingleton('customer/session')->getCustomer()->getAttributecode()
and use this value to set CSS classes on your templates
Does anyone know how to make entity_id visible on the frontend as a sortable attribute? I went to manage attributes and I can't add entity_id as an attribute as it says
"The attribute code 'entity_id' is reserved by system. Please try another attribute code"
So I tried to do a search in the whole database I had Magento on using the following SQL CMD:
select attribute_id from eav_attribute where attribute_code = 'updated_at';
and it returned zero results, I tried other attributes and those showed...
Does anyone know how I can add entity_id as an attribute, as I can't even make it visible because I have no idea what the attribute_id # is even while searching the whole database for that value.
Here is the code I use to make a attribute visible on the admin section of magento:
UPDATE `catalog_eav_attribute`
SET `is_visible` = '1'
WHERE `catalog_eav_attribute`.`attribute_id` = 105;
I've tried searching high and low for days, and trying different variations - so stuck at this point, any help would be great.
I'm using Magento Enterprise 12.2 if that helps, but to be honest the DB is not much different than the community version the only differences are the added modules but when it comes to products it's pretty much the same.
This is surprisingly nasty - I can't see a way to do this simply in the DB, as entity_id isn't a standard attribute (it's really just a key field) and the core code repeats the same logic in three place (urgh).
It's also changed a lot since the older versions of Magento, so many of the tutorials and forum posts on this no longer apply.
What you can do is add "entity_id" in as a new sorting option, similar to the way "Best Value" exists as a sorting option. In the following example I've labelled it "Newest"
The usual caveats apply: You should do this in an extension (or at the least in /local/ overrides) but the three core file methods that need overriding in Community Edition 1.7 are:
Mage_Adminhtml_Model_System_Config_Source_Catalog_ListSort
public function toOptionArray()
{
$options = array();
$options[] = array(//benz001
'label' => Mage::helper('catalog')->__('Newest'),
'value' => 'entity_id'
); //end benz001
$options[] = array(
'label' => Mage::helper('catalog')->__('Best Value'),
'value' => 'position'
);
foreach ($this->_getCatalogConfig()->getAttributesUsedForSortBy() as $attribute) {
$options[] = array(
'label' => Mage::helper('catalog')->__($attribute['frontend_label']),
'value' => $attribute['attribute_code']
);
}
return $options;
}
Then Mage_Catalog_Model_Category_Attribute_Source_Sortby
/**
* Retrieve All options
*
* #return array
*/
public function getAllOptions()
{
if (is_null($this->_options)) {
$this->_options = array(
array(//benz001
'label' => Mage::helper('catalog')->__('Newest'),
'value' => 'entity_id'
), //end benz001
array(
'label' => Mage::helper('catalog')->__('Best Value'),
'value' => 'position'
));
foreach ($this->_getCatalogConfig()->getAttributesUsedForSortBy() as $attribute) {
$this->_options[] = array(
'label' => Mage::helper('catalog')->__($attribute['frontend_label']),
'value' => $attribute['attribute_code']
);
}
}
return $this->_options;
}
And then Mage_Catalog_Model_Config
public function getAttributeUsedForSortByArray()
{
$options = array(
'entity_id' => Mage::helper('catalog')->__('Newest'), //benz001
'position' => Mage::helper('catalog')->__('Position'),
);
foreach ($this->getAttributesUsedForSortBy() as $attribute) {
/* #var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
$options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
}
return $options;
}
With those in place, flush the cache and refresh and you can then select to sort by Newest/entity_id in the config and on the category page and on the frontend.
Note: even if you have caching turned off it's still a good idea to flush the cache - parts of the admin are cached even when caching is disabled.
I have installed drupal 6, added some cck fields in one content type. Added two select box fields.
I am taking the selected value of parent select box and as per that selection passing relates options to next select box field using Ajax. (e.g Country -> State. When user selects country I want to pass state values into next select box.)
But when I am submitting the form it gives the following error:
"An illegal choice has been detected. Please contact the site administrator."
I don't know why it is not taking the ajaxified select box values while saving the node.
Does somebody has the solution on it. Is there any solution to handle this dynamic select options in Drupal.
Thanks in advance.
The same thing I'm working on drupal 7 and its work for me. Below is the code. Hope this help you. What I have done is on selection of car model car variant will change and the data save in the table.
function add_offer_form($form, $formstate) {
$form['add_offer_new_car_model'] = array(
'#type' => 'select',
'#required' => TRUE,
'#options' => $car_model,
'#ajax' => array(
'effect' => 'fade',
'progress' => array('type' => 'none'),
'callback' => 'variant_callback',
'wrapper' => 'replace_variant',
),
);
// Combo box to select new car variant
$form['add_offer_new_car_variant'] = array(
'#type' => 'select',
'#options' => array(),
// The prefix/suffix provide the div that we're replacing, named by #ajax['wrapper'] above.
'#prefix' => '<div id="replace_variant">',
'#suffix' => '</div>',
);
// An AJAX request calls the form builder function for every change.
// We can change how we build the form based on $form_state.
if (!empty($formstate['values']['add_offer_new_car_model'])) {
$model_id = $formstate['values']['add_offer_new_car_model'];
$rows = array();
$result = db_query("SELECT id, variant_name from {va_car_variant} where car_model_id in ($model_id,1) order by variant_name");
while ($data = $result->fetchObject()) {
$id = $data->id;
$rows[$id] = $data->variant_name;
}
$form['add_offer_new_car_variant']['#options'] = $rows;
}
}
////////////////////////////////////////////////////////
///////// FUNCTION FOR AJAX CALL BACK
function variant_callback($form, &$form_state) {
return $form['add_offer_new_car_variant'];
}