Onchange event in system.xml magento - magento

Can anyone tell me how i can do that.I created custom module with system.xml.In system.xml I create two elements, one select option and other text-box.I want to display text-box on specific value of select option. My code is :
Select option
<email_sender translate="label">
<label>E-mail Sender</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_email_identity</source_model>
<sort_order>0</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</email_sender>
Textbox that want to be displayed on select option is :
<interval translate="label">
<label>Interval</label>
<frontend_type>text</frontend_type>
<sort_order>4</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
.
While for select i use this chunk of code in model
public function toOptionArray()
{
return array(
array('value'=>'show_txtbx', 'label'=>Mage::helper('mymodule')->__('Show Textbox')),
array('value'=>'hide', 'label'=>Mage::helper('mymodule')->__('Hide')),
);
}
How can i do that.Thanks in advance.

Hi all I got my answer after some extra search.Thanks to Alan for such nice tutorials .Here is the link where I got my answer.Click Here

I tried above-given link but in my case, it didn't work with that syntax.
The core concept of adding dependency is correct, but the syntax is somewhat different I guess. You need to add
<depends>
<field id="custom">1</field>
</depends>
Here is a link which worked for me :
https://webkul.com/blog/create-dependant-field-admin-configuration-magento-2/

Related

Add option to Magento system admin

I would like to add a textbox to the Header block located inside System>Config>Design>Header, the location in the image below.
I know this has to be done in xml, but I am not sure where. Also how would I display that in an phtml file?
In code/core/Mage/Page/etc/system.xml you will find the configuration that Magento reads to show those fields, for example the "Small Logo Image src" is a field called logo_src_small. The needed is a module that will tell Magento about:
The extra field in the admin panel under header.
<config>
<sections>
<design>
<groups>
<header>
<fields>
<new_field translate="label">
<label>New Field</label>
<frontend_type>text</frontend_type>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</new_field>
</fields>
</header>
</groups>
</design>
</section>
</config>
Rewrite the block class code/core/Mage/Page/Block/Html/Header.php so you can add the method that will expose the new field.
In the app/design/frontend/{Package}/{Theme}/template/page/html/header.phtml you can easily call $this->getNewField() where getNewField() is the method you have in the class we overridden in point 2.
A couple of links to help you start:
http://excellencemagentoblog.com/blog/2011/09/22/magento-part8-series-systemxml/
http://www.ecomdev.org/2010/10/27/custom-configuration-fields-in-magento.html
http://inchoo.net/magento/overriding-magento-blocks-models-helpers-and-controllers/
https://magento.stackexchange.com/questions/78175/add-custom-field-in-admin-system-configuration-sales
First add a field in system.xml file which is located in
app/code/core/Mage/Page/etc/system.xml,under header section
<header translate="label">
..........
<welcome_massage translate="label">
<label>Welcome Massage</label>
<frontend_type>text</frontend_type>
<sort_order>35</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</welcome_massage>
</fields>
</header>
Then add a method in header block , app/code/core/Mage/Page/Block/Html/Header.php
public function getWelcomeMassage()
{
return $this->_data['welcome_massage'] = Mage::getStoreConfig('design/header/welcome_massage') ;
}
Last call this method in header.phtml file, like that
<?php echo $this->getWelcomeMassage() ?>
Note : You see that I have code in core files. You should rewrite the
core files.
Overriding Magento blocks, models, helpers and controllers
Overwrite system.xml

magento how add dynamic url path in system.xml

I am working on payment gateway. but i have problem i did google research but couldn't find any solution i hope i'll get solution here so i am posting here
following my system.xml code block
<title translate="label">
<label>Title</label>
<comment><![CDATA[<img src="/media/billmate/images/billmate_bank_s.png" />]]></comment>
<frontend_type>text</frontend_type>
<sort_order>3</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</title>
in this block problem is in comment tag right now here i put static link /media/billmate/images/billmate_bank_s.png please anybody suggess me how to make it dynamic
An element from system.xml can have a dynamic comment. The comment can be rendered through a model.
You need to declare the comment field like this:
<comment>
<model>module/adminhtml_comment</model>
</comment>
Now you need to create the model with alias module/adminhtml_comment:
<?php
class Namespace_Module_Model_Adminhtml_Comment{
public function getCommentText(){ //this method must exits. It returns the text for the comment
return "Some text here";
}
}
like following
<title translate="label">
<label>Title</label>
<comment>
<model>module/adminhtml_comment</model>
</comment>
<frontend_type>text</frontend_type>
<sort_order>3</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</title>
return value from the getCommentText method
Add into system.xml file
<field id="row_payment_us_free" translate="label" type="select" sortOrder="5" showInDefault="1" showInStore="1" showInWebsite="1" canRestore="1">
<label>Payment US Free</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><model>VendoreName\ModuleName\Model\SystemConfigComment</model></comment>
</field>
app/code/VendoreName/ModuleName/Model
SystemConfigComment.php
<?php
namespace VendoreName\ModuleName\Model;
use Magento\Framework\UrlInterface;
class SystemConfigComment implements \Magento\Config\Model\Config\CommentInterface
{
protected $urlInterface;
public function __construct(
UrlInterface $urlInterface
) {
$this->urlInterface = $urlInterface;
}
public function getCommentText($elementValue)
{
$url = $this->urlInterface->getUrl('adminhtml/system_config/edit/section/payment');
return 'Require to enable Zero Subtotal Checkoutpayment method for Zero Subtotal order.';
}
}

Successful registrations based of Customer group chosen

I am building an Magento site. Currently on the account registration form I have built it so there is drop down box which allows customers to choose their ‘Customer Group’.
If there are for example four different customer groups there are four different success emails the default Magento one, and 3 which I would create). What I need is based on which customer group is chosen the appropriate email is sent.
I have found the function which sends the new email in AccountController.php :
$customer->sendNewAccountEmail(
$isJustConfirmed ? 'confirmed' : 'registered',
'',
Mage::app()->getStore()->getId()
);
My initial thought would be to create the other email files in app/locale/en_US/template/email
But I don’t know which file/function chooses ‘account_new.html’ as the default email file so I could maybe implement some checks based of the customer group id.
I am unsure of the next steps to approach this such as how to edit this file and where to create the different success emails.
You'll likely need to overwrite the Mage_Customer_Model_Customer class to take control of the function sendNewAccountEmail(). This function is how the system decides which email to send and in theory you could override this function.
You probably know how to do an override, but just in case:
<models>
<customer>
<rewrite>
<customer>Namespace_Module_Model_Customer</customer>
</rewrite>
</customer>
</models>
Next, you'll want to create system configuration values, System.xml, you'll need to create a new entry for each "group" you have. This is no the most elegant solution as this is a static list and your groups could be dynamic. But to assign a template you'd either need a whole new module or update this file. But, now you can create transactional emails and assign it to each group in this system.xml file.
<?xml version="1.0"?>
<config>
<sections>
<yourmodule translate="label" module="yourmodule">
<class>separator-top</class>
<label>your module</label>
<tab>general</tab>
<frontend_type>text</frontend_type>
<sort_order>30</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
<groups>
<email translate="label">
<label>Email Templates</label>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<fields>
<group1_template translate="label comment">
<label>Group 1 Template</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_email_template</source_model>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</group1_template>
<group2_template translate="label comment">
<label>Group 2 Template</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_email_template</source_model>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</group2_template>
</fields>
</email>
</groups>
</yourmodule>
</sections>
</config>
Finally, the override for your sendNewAccountEmail():
class Namespace_Module_Model_Customer {
public function sendNewAccountEmail($type = 'registered', $backUrl = '', $storeId = '0')
{
$types = array(
'registered' => self::XML_PATH_REGISTER_EMAIL_TEMPLATE, // welcome email, when confirmation is disabled
'confirmed' => self::XML_PATH_CONFIRMED_EMAIL_TEMPLATE, // welcome email, when confirmation is enabled
'confirmation' => self::XML_PATH_CONFIRM_EMAIL_TEMPLATE, // email with confirmation link
'group1' => 'yourmodule/email/group1_template',
'group2' => 'yourmodule/email/group2_template',
);
if (!isset($types[$type])) {
Mage::throwException(Mage::helper('customer')->__('Wrong transactional account email type'));
}
if (!$storeId) {
$storeId = $this->_getWebsiteStoreId($this->getSendemailStoreId());
}
$this->_sendEmailTemplate($types[$type], self::XML_PATH_REGISTER_EMAIL_IDENTITY,
array('customer' => $this, 'back_url' => $backUrl), $storeId);
return $this;
}
}
Obviously there is a lot of room for improvement, namely coming up with a way to dynamically pull customer groups and created configurations from that and additionally adding those same dynamic checks to this function, but this is a simple static solution.

Magento: get region list for country in admin shipping module

Currently in my /etc/system.xml file I can use this to pull through a complete list of regions that are stored in Magento and display them as a multiselect. This works fine, however I would prefer to only pull through the regions for one country, e.g. the UK counties or US states:
<counties translate="label">
<label>Counties</label>
<frontend_type>multiselect</frontend_type>
<sort_order>10</sort_order>
<source_model>adminhtml/system_config_source_allregion</source_model>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</counties>
The reason for this is that I have added a lot of regions/states/counties on the system and it is now not a very user friendly multi-select box.
UPDATE:
After not immediately acting on the solutions provided below I revisited this problem some time later to put together my own solution inspired by the answers provided.
I copied app/code/core/Mage/Adminhtml/Model/System/Config/Source/Allregion.php to app/code/core/Mage/Adminhtml/Model/System/Config/Source/Ukregion.php
Then I changed the class definition to Mage_Adminhtml_Model_System_Config_Source_Ukregion.
Then I changed:
$regionsCollection = Mage::getResourceModel('directory/region_collection')->load();
to include a country filter:
$regionsCollection = Mage::getResourceModel('directory/region_collection')->addCountryFilter('GB')->load();
I now get the counties for the UK (which I had to edit myself but that is a different story-style-magento-problem).
Finally I changed my system.xml:
<counties translate="label">
<label>Counties</label>
<frontend_type>multiselect</frontend_type>
<sort_order>10</sort_order>
<source_model>adminhtml/system_config_source_ukregion</source_model>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</counties>
The use of 'UK' instead of 'GB' is entirely deliberate - GB does not include the NI counties it is just used for 'legacy reasons'. 'UK' does include Northern Ireland, as does my county list.
Take a look at the page System > Configuration > Shipping Settings, you can recreate how it's regions are adjusted to match the selected country.
Now look at the file app/code/core/Mage/Shipping/etc/system.xml. The country and region fields look like this:
<country_id translate="label">
<label>Country</label>
<frontend_type>select</frontend_type>
<frontend_class>countries</frontend_class>
<source_model>adminhtml/system_config_source_country</source_model>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</country_id>
<region_id translate="label">
<label>Region/State</label>
<frontend_type>text</frontend_type>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</region_id>
The important parts are:
The country has a class of countries and an ID of country_id.
The region has an ID of region_id.
The region is not a select and doesn't have a source model.
The javascript is already in place for config pages. It finds elements with a class of countries and uses it's ID to find a similarly named element (the region). When the first element changes the second is updated by AJAX.
When using this in the past I sometimes had trouble when there is more than one country/region pair on a page so it's best to avoid that situation.
The source_model attribute defines the class "where" are the options of this multiselect field. You can create a new class with only the options you want to show in this field and point the source_model to this new class.
You should use the toOptionArray() method to define the options. A quick way of doing this is like the below example:
public function toOptionArray()
{
return array(
array( 'value' => VALUE,
'label' => LABEL ) ),
array( 'value' => VALUE2,
'label' => LABEL2 )
);
}
Of course, get the options from a database table would be a better practice.

magento payment methods - enable for admin only

I need to enable the Cheque / Money Order payment method to enable our clients call centre team to create orders in the admin.
However we do not want customers buying online through the website to use this payment method.
Does anybody know how I might be able to do this?
Regards,
Fiona
Two options:
1)
Override (using never change the original or add a /local/Mage/ override) the payment method (or just modify it if it's your own method), and add this:
protected $_canUseInternal = true;
protected $_canUseCheckout = false;
protected $_canUseForMultishipping = false;
2)
Create an observer on the frontend for "payment_method_is_active" event, and set to inactive the methods you don't want to show on the frontend:
<config>
<frontend>
<events>
<payment_method_is_active>
<observers>
... your observer here
public function your_observer($event){
$method = $event->getMethodInstance();
$result = $event->getResult();
if( $method should not be active in frontend ){
$result->isAvailable = false;
}
}
If you enable it in the global config-view and then disable it for your store/website view does that work? (Don't have a system handy to test...)
I tried Enriques solution # 1 to hide one payment method in frontend, only to show it in admin:
protected $_canUseInternal = true;
protected $_canUseCheckout = false;
protected $_canUseForMultishipping = false;
Seems to work fine when I am testing, and in general..
BUT.. I still sometimes get normal orders that uses my "hidden" payment method. Like Magento sometimes fails to use the piece of code above.
Anyone knows why this happens, and how to avoid?
Thanks
-Espen
Check config setting like below example.
If you want to check that customcarrier_fastways is active then use code similar to this:
$shippingMethod="customcarrier_fastways";
$shippingMethodActive= Mage::getStoreConfig('carriers/'.$shippingMethod.'/active', Mage::app()->getStore()->getId());
$showAtFront= Mage::getStoreConfig('carriers/'.$shippMethod.'/showatfront', Mage::app()->getStore()->getId());
if($shippingMethodActive && $showAtFront){
// do something here...
}
While variables is defined in xml file as below:
<carriers translate="label" module="shipping">
<groups>
<customcarrier_fastways translate="label">
<label>Fastways</label>
<frontend_type>text</frontend_type>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<active translate="label">
<label>Enabled</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</active>
<showatfront translate="label">
<label>Show on checkout</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</showatfront>
</fields>
</customcarrier_fastways>
</groups>
</carriers>
Just found this, looks like it's what you're after:
http://www.magentocommerce.com/boards/viewthread/38765/P15/

Resources