Magento Add Event onlick on System Configuration Custom Module Field - magento

Nowhere can I find a definition for which to search.
In mymodule namespace/modulename/etc/system.xml I have:
<faq_input translate="label">
<label>Question Collor: </label>
<comment>example: #000000</comment>
<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>1</show_in_store>
</faq_input>
I need add onclick event like:
<input type="text" onclik="code(myevent)" value="xxx" >

You need to use a <frontend_model>, for which you can find some examples in the core (eg in app/code/core/Mage/Catalog/etc/system.xml).
The frontend model must be a block inheriting from Mage_Adminhtml_Block_System_Config_Form_Field, which has a _getElementHtml() method where you can apply your own code to the form element before rendering it. Eg :
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
$element->setOnclick('launchExample();');
$html = $element->getElementHtml();
$html .= '<script type="text/javascript">function launchExample(){ alert("This is an example"); }</script>';
return $html;
}

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.

Add a date picker to system.xml on custom module

As stated in the subject, I am trying to add a date field with its date picker in the System > Configuration area for a custom module (thus using etc/system.xml).
I tried to get inspiration from the thread below :
Magento - Add a button to system.xml with method attached to it
but no success.
I'm sure this is a question of creating the right block or method to create a custom html field but I cannot read thru the Magento Matrix :)
I am stuck at the step where I need to code the class (Datefield.php):
<?php
class Namespace_Module_Block_Datefield extends Mage_Adminhtml_Block_System_Config_Form_Field {
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element) {
// ----> Am I wrong in calling ..._Abstract? Should I call Varien_Data_Form_Element_Date? I've tried but no success either...
$this->setElement($element);
$html = // ------------------> what to put here? Call a block or some other method?
->setFormat('d-m-Y')
->setLabel($this->__('Choose date'))
->toHtml();
return $html;
}
}
?>
Do you have a trick on how to do that ?
Thanks a lot.
Hervé
EDIT 02/19/2014: added validation
I found what I think is a more elegant way of doing this. Actually, satrun77 methods is ok but we must place a file in Varien/Data/Form/Element/ which can be overwritten if someone else working on the project unluckily uses the same file/class name. Moreover, this method places the file in the module directories which is, I think, better than distributing files all over the directory tree.
In system.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
....
<fields>
...
<run translate="label">
<label>Date</label>
<frontend_type>text</frontend_type> <!-- Use text instead of "myDateSelection" -->
<frontend_model>module/adminhtml_system_config_date</frontend_model> <!-- Call a module specific renderer model -->
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<validate>required-entry</validate> <!-- Optional -->
<show_in_store>1</show_in_store>
</run>
</fields>
...
</config>
Create a new file :
app/code/[local, community]/Namespace/Module/Block/Adminhtml/System/Config/Date
with the content below:
class Namespace_Module_Block_Adminhtml_System_Config_Date extends Mage_Adminhtml_Block_System_Config_Form_Field
{
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
$date = new Varien_Data_Form_Element_Date;
$format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
$data = array(
'name' => $element->getName(),
'html_id' => $element->getId(),
'image' => $this->getSkinUrl('images/grid-cal.gif'),
);
$date->setData($data);
$date->setValue($element->getValue(), $format);
$date->setFormat(Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT));
$date->setClass($element->getFieldConfig()->validate->asArray());
$date->setForm($element->getForm());
return $date->getElementHtml();
}
}
Create class file in app/code/local/Varien/Data/Form/Element/. Make sure the file name is prefixed with something that identify your module (this is just to differentiate your custom code from Magneto core files)
class Varien_Data_Form_Element_MyDateSelection extends Varien_Data_Form_Element_Date
{
public function getElementHtml()
{
// define image url
$this->setImage(Mage::getDesign()->getSkinUrl('images/grid-cal.gif'));
// define date format
$this->setFormat('yyyy-MM-dd');
return parent::getElementHtml();
}
}
Inside your module system.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
....
<fields>
...
<run translate="label">
<label>Run now</label>
<frontend_type>myDateSelection</frontend_type>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</run>
</fields>
...
</config>
Placing custom code inside lib/ folder or app/Mage/Core/ folder is not the best way to create custom code for Magento. These folders are for core code and not custom code.
This approach creates the least amount of code and does not change any of the core files. So, there isn't any harm from having extra file inside the lib/ folder. But you need to remember that you have extra file for your module in the lib/.
Hope this helps

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