Take the 2-minute tour ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

I created a module that looks like this:

http://screencast.com/t/TG7brmUi

And the code looks like this:

he code is as follows:

Luisvalencia_Affiliate.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Luisvalencia_Affiliate>
            <active>true</active>
            <codePool>community</codePool>
        </Luisvalencia_Affiliate>
    </modules>
</config>
config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Luisvalencia_Affiliate>
            <version>0.0.1</version>
        </Luisvalencia_Affiliate>
    </modules>
    <frontend>
        <events>
            <controller_action_predispatch>
                <observers>
                    <luisvalencia_affiliate>
                        <class>luisvalencia_affiliate/observer</class>
                        <method>captureReferral</method>
                        <type>singleton</type>
                    </luisvalencia_affiliate>
                </observers>
            </controller_action_predispatch>
        </events>
    </frontend>
</config>
and Observer.php

<?php
class Luisvalencia_Affiliate_Model_Observer
{
    const COOKIE_KEY_SOURCE = 'luisvalencia_affiliate_source';

    public function captureReferral(Varien_Event_Observer $observer)
    {
       //bla bla bla, get cookie value from admin?
    }

    protected function _getCookieLifetime()
    {
        $days = 1;
        // convert to seconds
        return (int)86400 * $days;
    }
}

As you can see the cookie lifetime is hardcoded.

I would like to add an admin section on the magento backend to be able to edit the cookie lifetime for example.

can you please guide me?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

you need create for system which is show in System >configuration section

Step1:system.xml

<?xml version="1.0"?>
<config>
      <tabs>
        <luisvalenciatab translate="label" module="affiliate">
            <label>Luisvalencia Cookie</label>
            <sort_order>0</sort_order>
        </luisvalenciatab>
      </tabs>
      <sections>
        <luisvalenciasection  translate="label" module="affiliate">                    
        <label>Luisvalencia</label>
        <tab>luisvalenciatab</tab>
        <frontend_type>text</frontend_type>
        <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>            
                    <groups>
                      <luisvalenciagroup translate="label"> 
                      <label>Luisvalencia Group</label>
                      <frontend_type>text</frontend_type>
                      <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>
                       <fields>
                          <cookie_time translate="label">
                            <label>Set Cookie Time</label>
                            <frontend_type>text</frontend_type>
                            <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>
                          </cookie_time>
                       </fields>
                       </luisvalenciagroup>
                    </groups>
        </luisvalenciasection>
      </sections>
</config>

Step2:You need to create adminhtml.xml for acl permission for system.xml

adminhtml code is like

<?xml version="1.0"?>
    <config>
        <acl>
            <resources>
                <admin>
                    <children>
                        <system>
                            <children>
                                <config>
                                    <children>
                                        <luisvalenciasection translate="title" module="affiliate">
                                            <title>Luisvalencia Section</title>
                                            <sort_order>0</sort_order>
                                        </luisvalenciasection>
                                    </children>
                                </config>
                            </children>
                        </system>
                    </children>
                </admin>
            </resources>
        </acl>
    </config>

Get config.xml value:

$config = Mage::getStoreConfig('luisvalenciasection/luisvalenciagroup/cookie_time');

enter image description here

Check system.xml Here:

  1. Tab: code is luisvalenciatab
  2. Section:code is luisvalenciasection
  3. Group: code is luisvalenciagroup
  4. fieldname:code is cookie_time

After that goto admin and login with admin login... then goto System>Permission>Role>Select Role Administrators> Click on save Role for add role permission for this configuration

More details for system setting check Alan Store block:

http://alanstorm.com/magento_loading_config_variables

And if you want to show
then out predefine value in this field in then

add this code in config.xml

  <default>
    <luisvalenciasection>
        <luisvalenciagroup>
            <cookie_time>1550</cookie_time>
        </luisvalenciagroup>
    </luisvalenciasection>
    </default>

But most importance after this add , you need admin role from

 goto `System>Permission>Role>Select Role Administrators>` Click on save `Role for  add role  permission for this configuration`
share|improve this answer
    
should these be stored under /etc? –  Esteban V Aug 14 at 13:36
    
yes.. LVM.. I will update in more details.... –  Amit Bera Aug 14 at 13:37
    
this is an amazing explanation. wow! thank you! –  Esteban V Aug 14 at 14:05
    
I am getting this error Fatal error: Class 'Mage_Affiliate_Helper_Data' not found in /home/theprint/public_html/preprod/app/Mage.php on line 547 –  Esteban V Aug 14 at 16:49
    
Beta, can you help me here?>> magento.stackexchange.com/questions/32341/… –  Esteban V Aug 14 at 20:10

Change your observer to have:

$days = Mage::getStoreConfig('affiliate/settings/cookie');

Then add this to the end of your config.xml:

<default>
    <affiliate>
        <settings>
            <days>1</days>
        </settings>
    </affiliate>
</default>

That should set a default of 1 for the days value.

In the same etc folder add a new file, system.xml:

<?xml version="1.0"?>
<config>
    <tabs>
        <luisvalencia translate="label" module="affiliate">
            <label>Whatever</label>
            <sort_order>200</sort_order>
        </luisvalencia>
    </tabs>
    <sections>
        <affiliate translate="label" module="affiliate">
            <label>Affiliate Cookie</label>
            <tab>luisvalencia</tab>
            <frontend_type>text</frontend_type>
            <sort_order>100</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <settings>
                    <label>Cookie Settings</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>10</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>0</show_in_website>
                    <show_in_store>0</show_in_store>
                    <fields>
                        <cookie translate="label">
                            <label>Cookie lifetime</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>0</show_in_website>
                            <show_in_store>0</show_in_store>
                            <comment><![CDATA[This is in days.]]></comment>
                        </cookie>
 ...

(You can close the XML tags or add other settings).

Once done clear your cache, login/out, go to system config. On the left is a new tab with 'Affiliate Cookie'. Voila.

share|improve this answer
    
I am getting this error, why? Fatal error: Class 'Mage_Affiliate_Helper_Data' not found in /home/theprint/public_html/preprod/app/Mage.php on line 547 –  Esteban V Aug 14 at 16:50
    
Add a helper class to your module or just remove the 'translate='blah'' parts and it should work fine. –  Woolfie Aug 14 at 17:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.