Magento Save Selected Country - magento

I want to add another dropdown list of countries in customer account register page.I tried
<?php echo $this->getCountryHtmlSelect() ?>
But this doesn't display the dropdown.Another one I tried is
<select name="partner_country" id="partner_country">
<option value=''>– Please Select –</option>
<?php foreach($_countries as $_country): ?>
<option value="<?php echo $_country->getId() ?>"><?php echo $_country->getName() ?></option>
<?php endforeach; ?>
</select>
This one displays country list, but the selected country doesn't show up in backend customer information page.
I know getCountryHtmlSelect() renders dropdown of countries.Do I have create similar method in my module to save the selected country?
Update
I already created a source model while adding this attribute via setup script,
$installer->addAttribute('customer_address','partner_country_id',array(
'type' => 'varchar',
'label' => 'Partner Country',
'input' => 'select',
'source' => 'wholesale/attribute_source_partnercountry',
'global' => 1,
'visible' => 1,
'required' => 0,
'visible_on_front' => 1,
'sort_order'=>220
));
Source model
class Company_Wholesale_Model_Attribute_Source_Partnercountry extends Mage_Eav_Model_Entity_Attribute_Source_Table
{
public function getAllOptions()
{
if (!$this->_options) {
$this->_options = Mage::getResourceModel('directory/country_collection')
->loadByStore($this->getAttribute()->getStoreId())->toOptionArray();
}
return $this->_options;
}
}
config.xml
<config>
<global>
<resources>
<wholesale_setup>
<setup>
<module>Company_Wholesale</module>
<class>Company_Wholesale_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</wholesale_setup>
</resources>
</global>
</config>

The issue you have is related to type of attribute you've created. For making possible selection of your custom attribute in the admin, you need to create/update it with type 'select' and 'source_model' for it. For this purpose using of customer module setup model is required, so in your module configuration of setup resources you need to specify it:
<config>
<global>
<resources>
<your_module_setup>
<setup>
<class>Mage_Customer_Model_Resource_Setup</class>
<module>Your_Module</module>
</setup>
</your_module_setup>
</resources>
</global>
</config>
And in your setup file, you need to create/modify your attribute. (If attribute exists, current code snippet will modify it, instead of creation).
<?php
$this->startSetup();
$this->addAttribute('customer', 'partner_country' , array(
'type' => 'varchar',
'label' => 'Partner Country',
'input' => 'select',
'source' => 'customer/entity_address_attribute_source_country'
));
$this->endSetup();
UPDATED:
Now I got your question, you haven't added your attribute to customer create account form, so your attribute is filtered out from data that is set to customer model during account creation process.
So you simply need to specify your customer attribute for the form where it should be possible to save your attribute.
Currently there are such forms available:
customer_account_create - Register Form
customer_account_edit - Change Account Details Form
checkout_register - Registering new account during checkout
For adding your custom attribute to form, just create one more setup script, that add a record with form code and your attribute id to table called customer/form_attribute:
<?php
$this->startSetup();
$attributeId = $this->getAttributeId('customer', 'partner_country_id');
$data = array(
array('attribute_id' => $attributeId, 'form_code' => 'customer_account_create'),
array('attribute_id' => $attributeId, 'form_code' => 'customer_account_edit'),
array('attribute_id' => $attributeId, 'form_code' => 'checkout_register')
);
$this->getConnection()->insertMultiple($this->getTable('customer/form_attribute'), $data);
$this->endSetup();
Just opt-out the forms you don't need.

If you would like to usegetCountryHtmlSelect(), then you should give it some parameters so that it can apply to your attribute, and not countryby default. For the register form, it could give something like this :
echo $this->getCountryHtmlSelect($this->getFormData()->getPartnerCountryId(), 'partner_country_id', 'partner_country', $this->__('Partner Country'))
And in the second example, you use partner_country in select name, whereas you created a partner_country_id attribute.

Related

Need to create a new attribute in a new type of product

I am creating a module to insert a new attribute and a new type of product in my store. As template, I use the same registration screen of the Virtual Product type and include new options ONLY this type of product.
For this, in my config.xml, I used:
<catalog>
<product>
<type>
<incomm_virtual translate="label" module="incomm">
<label>Incomm Virtual</label>
<model>incomm/product_type</model>
<price_model>incomm/product_price</price_model>
<is_qty>1</is_qty>
<composite>0</composite>
<can_use_qty_decimals>0</can_use_qty_decimals>
</incomm_virtual>
</type>
</product>
</catalog>
and to configure the installation with the sql in same config.xml:
<resources>
<abc_incommproduct_setup>
<setup>
<module>ABC_Incomm</module>
<class>Mage_Catalog_Model_Resource_Setup</class>
</setup>
</abc_incommproduct_setup>
</resources>
The next file was used mymodule / model / product / type.php containing:
class ABC_Incomm_Model_Product_Type
extends Mage_Catalog_Model_Product_Type_Virtual
{
const TYPE_INCOMM = 'incomm';
const XML_PATH_AUTHENTICATION = 'catalog/incomm/authentication';
protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode)
{
if ($this->_isStrictProcessMode($processMode)) {
return Mage::helper('ABC_Incomm_Helper_Data')->__(
'Incomm product %s cannot be added to cart. ' .
' On the product detail page click the "Go to parent site"'.
' button to access the product.',
$product->getName()
);
}
return parent::_prepareProduct($buyRequest, $product, $processMode);
}
}
In the same folder, the virtual.php:
class ABC_Incomm_Model_Product_Virtual extends Mage_Catalog_Model_Product_Type_Virtual {
}
Finally, the SQL installation file in sql folder / abc_incommproduct_setup / mysql-install-4-0.1.0.php:
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'terms_of_use', array(
'group' => 'General',
'input' => 'textarea',
'type' => 'text',
'sort_order' => 4,
'label' => 'Terms of use',
'backend' => '',
'visible' => true,
'required' => true,
'wysiwyg_enabled' => true,
'visible_on_front' => true,
'apply_to' => 'incomm', //only in this type of product
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$this->endSetup();
My problem is this, after disabling the cache in magento, clear the cache and include a new type of product, in the Product Types list appears my NEW type of product, but the options NOT load the new attribute inserted via sql, the What's happening? my sqlinstall.php did not run ??
Probably because your new product type is incomm_virtual as specified in config.xml and not incomm. Try to edit 'apply_to' => 'incomm' with 'apply_to' => 'incomm_virtual' and re-run the installer
If you upgrade you script follow the similar pattern as install upgrade script, except the file-name is now data-upgrade-{old-version}-{new-version}.php and they are located under the data folder.
If you don't want to upgrade your script and just change it. don't forget to delete the existing entries for youmodule_setup code from core_resource table. it only loaded at once. if you don't delete that your script wont be loaded.

Magento how to add extra fields for static block

I've been trying to add extra fields to the static block so it will be easier to update.
How can I add a simple textfield or maybe a image upload field?
See example: http://i.stack.imgur.com/2HtDC.jpg
Thanks in advance!
For this, you need to overwrite this class Mage_Adminhtml_Block_Cms_Block_Edit_Form. This class is used to add fieldsets and fields for cms_block. Take a look on the _prepareForm() method inside it.
If you put this code, just after Title field,
$fieldset->addField('sub_title', 'text', array(
'name' => 'sub_title',
'label' => Mage::helper('cms')->__('Sub Title'),
'title' => Mage::helper('cms')->__('Sub Title'),
'required' => true,
));
you can see your sub-title text field in static blocks. However Do not edit a core file directly. You need to write a custom module that should overwrite this class. Your Module config file should contain this code
File : app/code/local/Namespace/Module/etc/config.xml
<config>
<global>
<blocks>
<adminhtml>
<rewrite>
<cms_block_edit_form>Namespace_Module_Block_Adminhtml_Cms_Block_Edit_Form</cms_block_edit_form>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
This will allow you to rewrite the class. What you need to do now is define the rewrite class now and in there you need to rewrite _prepareForm(). It should some what look like this.
Location : app/code/local/Namespace/Module/Block/Adminhtml/Cms/Block/Edit/Form.php
<?php
class Namespace_Module_Block_Adminhtml_Cms_Block_Edit_Form extends Mage_Adminhtml_Block_Cms_Block_Edit_Form {
protected function _prepareForm()
{
//put all the code inside parent class here
//then place the below content in appropriate place
$fieldset->addField('sub_title', 'text', array(
'name' => 'sub_title',
'label' => Mage::helper('cms')->__('Sub Title'),
'title' => Mage::helper('cms')->__('Sub Title'),
'required' => true,
));
return parent::_prepareForm();
}
}
Try based on this idea
EDIT
Please note, it will allow you to put new field in cms > block, howver to save this, you need to define model for your module. You have two options. Add a new field to save your new field in Cms > Block table or create your own table and store this value in that field along with the referene to cms >block table. This is out of box and you should implement it your own way.
Happy coding
In adition to #Rajeev
You need get the parent form in this way...
<?php
class Namespace_Module_Block_Adminhtml_Cms_Block_Edit_Form extends Mage_Adminhtml_Block_Cms_Block_Edit_Form {
protected function _prepareForm()
{
$form = parent::_prepareForm()->getForm();
$fieldset = $form->addFieldset('fieldset_example', array('legend'=>Mage::helper('core')->__('My example fieldset')));
$fieldset->addField('sub_title', 'text', array(
'name' => 'sub_title',
'label' => Mage::helper('cms')->__('Sub Title'),
'title' => Mage::helper('cms')->__('Sub Title'),
'required' => true,
));
return $this;
}
}
My suggestion is, rather add an extra field, you can add a <div>...</div> to in the content are by removing what you see is what you get editor(just click on the show / hide Editor).
And for image you can direct upload images by clicking on the inset/edit image option from the menu.
It will be simpler than creating custom fields.

How to display Custom order attribute in Magento back end

I'm using Magento 1.7 - I am fairly new to Magento module development.
I have created a new Order custom EAV attribute called Deliverydate which the customer will enter on the Shopping Cart page. I created a custom module and installer script, and can see it in the eav_attribute table.
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('order', 'deliverydate', array(
'position' => 1,
'input' => 'text',
'backend_type' => 'varchar',
'type' => 'varchar',
'label' => 'Choose delivery date',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'global' => 1,
'visible_on_front' => 1,
));
$installer->endSetup();
But I've read many sources and can't figure out how to
1) Save the value entered by the customer.
2) Retrieve the value for display in the Admin.
I have read on these tutorials about Observers but can't seem to get one to work:
Here's my config.xml file
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Deliverydate>
<version>0.1.0</version>
</Mycompany_Deliverydate>
</modules>
<global>
<resources>
<deliverydate_setup>
<setup>
<module>Mycompany_Deliverydate</module>
<class>Mycompany_Deliverydate_Model_Resource_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</deliverydate_setup>
</resources>
<events>
<checkout_type_onepage_save_order>
<observers>
<Deliverydate_observer>
<type>singleton</type>
<class>deliverydate/observer</class>
<method>Deliverydate</method>
</Deliverydate_observer>
</observers>
</checkout_type_onepage_save_order>
</events>
</global>
</config>
And my Observer.php file, most of which I borrowed from another tutorial / question on this site.
class Mycompany_Deliverydate_Model_Observer {
public function Deliverydate($observer) {
$event = $observer->getEvent();
$order = $event->getOrder();
$order->setDeliverydate(Mage::app()->getRequest()->getPost('delivery_date'));
}
}
I don't think this is being entered at all - I put in a throw new Exception and went through an entire order without ever seeing the exception. In any case I am not sure I'm checking the right event - checkout_type_onepage_save_order - when is this fired? And I don't know where the setDeliverydate() function would be defined but as I said this sort of comes from another source and it wasn't defined there either.
And where does the value entered get stored?
Please check this following post, It would help you. If it doesn't uplift you from this issue, please comment here
http://chillydraji.wordpress.com/2014/03/03/how-to-create-new-attribute-to-order-in-magento/

Magento: can't set the default value of custom order attribute using installation script

I've added custom attribute to orders using mysql4-install-1.0.0.php in my module:
$installer = $this;
$installer->startSetup();
$installer->addAttribute('order', 'custom_status', array(
'type' => 'varchar',
'label' => 'My Status',
'note' => '',
'default' => "my_default_value",
'visible' => false,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'unique' => false
));
It works - when I look at sales_flat_order table, I see new varchar field *custom_status* in the table. But the default value is NULL instead of "my_default_value" as expected there. Any ideas, why?
PS. Installation script is really executed, I reset all to initial state each time.
UPD. config.xml
<resources>
<mymodule_setup>
<setup>
<module>Company_Mymodule</module>
<class>Mage_Sales_Model_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</mymodule_setup>
***
Magento Sales module has pseudo emulation of old EAV functionality. It takes only type property from your array to create a column in the database. Also it uses "grid" property for determining is it required to make the same column in grid representation table.
BTW, previous sales module that was based on EAV, was not using default, label, required and other properties as well. If you want to set a default value for this attribute you need create an observer for order before save event and set this data in it if field is empty.
Make sure you have it like this in your config.xml:
<resources>
<customattr_setup>
<setup>
<module>Yourcompany_ModuleName</module>
<class>Mage_Sales_Model_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</customattr_setup>
</resources>
The <class>Mage_Sales_Model_Mysql4_Setup</class> is extremely important!
Then in the install script mysql4-install-0.1.0.php:
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute(
'order',
'customattr',
array(
'type' => 'float', // or whatever you want here...
'grid' => false
)
);
$installer->endSetup();
?>

Magento - Add column to customer grid

how to add a custom column to magento customer grid ?
Thanks a lot.
You should override the class Mage_Adminhtml_Block_Customer_Grid (app/code/core/Mage/Adminhtml/Block/Customer/Grid.php) and apply the following changes:
1 - add the new attribute to show in the _prepareCollection() function
2 - add the new column to show in the _prepareColumns() function
Credit: http://www.leonhostetler.com/blog/magento-add-attribute-columns-in-manage-products-grid-201205/
Magento does not provide us with the ability to choose which attributes are included as columns in the Manage Products grid but it’s fairly simple to make the necessary code changes.
The code that generates the Manage Products grid is at /app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php. The first thing you need to do is copy Grid.php into to the local directory structure. In other words, you copy Grid.php into the following location; /app/code/local/Mage/Adminhtml/Block/Catalog/Product/. If there is no such location, then you must create the necessary directories. The final location of the file must be; /app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php
Now, open Grid.php (the one in the local directory structure) and begin editing. Find the following code;
$this->addColumn('sku',
array(
'header'=> Mage::helper('catalog')->__('SKU'),
'width' => '80px',
'index' => 'sku',
));
That’s the code that adds the SKU column to the product grid. Now, let’s say you have a custom attribute called Supplier ID (supplier_ID) and you want these to appear on the Manage Products grid as well. Place the following code either before or after the above block of code, as long as it’s inside _prepareColumns().
$this->addColumn('supplier_id',
array(
'header'=> Mage::helper('catalog')->__('Supplier ID'),
'width' => '150px',
'index' => 'supplier_id',
));
Then add the following line to _prepareCollection() where the other attributes are listed like this;
->addAttributeToSelect('supplier_id')
That should be all you need to do. You might have to re-compile, refresh your caches, logout, and log back in to see the change in your product grid.
The above example is for adding an attribute with a Catalog Input Type for Store Owner of Text Field. What if your attribute uses a dropdown list? The code above will have to be modified.
Let’s say you have an attribute called Supplier (supplier) which in the product editor presents a dropdown list of suppliers to choose from. To do this, we can add the following code to _prepareColumns():
$supplier_items =
Mage::getModel('eav/entity_attribute_option')->getCollection()->setStoreFilter()->join('attribute','attribute.attribute_id=main_table.attribute_id',
'attribute_code'); foreach ($supplier_items as $supplier_item) :
if ($supplier_item->getAttributeCode() == 'supplier')
$supplier_options[$supplier_item->getOptionId()] = $supplier_item->getValue(); endforeach; $this->addColumn('supplier',
array(
'header'=> Mage::helper('catalog')->__('supplier'),
'width' => '150px',
'type' => 'options',
'index' => 'supplier',
'options' => $supplier_options, ));
And let’s not forget to add the following line to _prepareCollection() where the other attributes are listed like this;
->addAttributeToSelect('supplier')
That should do it for you. Re-compile, refresh your caches, and logout and then back in if you need to.
I have publish here with real example.
If you need to add custome attribute, you may need to take care of join statements carefully.
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
Rewrite customer grid block with your custom module.
app/code/[local or community]/YourCompany/YourModule/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<yourcompany_yourmodule>
<version>0.1.0</version>
</yourcompany_yourmodule>
</modules>
<global>
<blocks>
<adminhtml>
<rewrite>
<customer_grid>YourCompany_YourModule_Block_Customer_Grid</customer_grid>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
app/code/[local or community]/YourCompany/YourModule/Block/Customer/Grid.php
<?php
class YourCompany_YourModule_Block_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{
public function setCollection($collection)
{
$collection->addAttributeToSelect('confirmation');
parent::setCollection($collection);
}
protected function _prepareColumns()
{
parent::_prepareColumns();
$this->addColumn('confirmation', array(
'header'=> Mage::helper('sales')->__('Confirmed'),
'index' => 'confirmation',
'type' => 'text',
'width' => '100px',
));
return parent::_prepareColumns();
}
}
Detailed explanation can be found here:
http://tipsmagento.blogspot.com/2011/03/add-new-column-on-customers-grid.html
Make sure to check out TigerMin for Magento. It's a tool with which you can easily add columns to productsgrid and even inline edit values instantly. Here`s a live demo: http://demo.emvee-solutions.com/tigermin/

Resources