Jump to content

How to create a simple module with one variable


Recommended Posts

Hello I have found a php Code, which works nicely, but I don't know how to make this to a module. I need a module, since in prestashop I can't use {php} tags.

$xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=".getRealIpAddr());
    $country = $xml->geoplugin_countryName ;
    if($country != "Switzerland")
    {
		$werbung=="yes";	
	}
	
	else { $werbung=="no";
	
	}


//For getting IP Address

    function getRealIpAddr()
    {
        if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
        {
          $ip=$_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
        {
          $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
          $ip=$_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

This is my Code. Only thing I need, is to bring $werbung variable to smarty template. I would like to use this variable for example on cms page. 

 

I want to do this:

{if $werbung =="yes"}Show something{/if}

should be very easy or not?

 

Would be very thankful if you could help me!

Edited by wakabayashi (see edit history)
Link to comment
Share on other sites

Oh maybe I should say, what I want to achieve. Basically I have a blog, where I show Google Adsense. My customers are 99% from switzerland. I want to have a module, which only shows Adsense, if the visitor is NOT from switzerland.

 

Actually best solution for me is, to have a own module, which adds a block to the right_column. This way I can use it in my blog and on cms pages. 

 

Thanks for your help!

Link to comment
Share on other sites

it can be done without module, you can use FrontController modification / override , at the end (inside class) put this:
 

    public static function CheckGuestCountry($country_to_check)
    {
        if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
        {
          $ip=$_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
        {
          $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
          $ip=$_SERVER['REMOTE_ADDR'];
        }
        
        $xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=".$ip);
        $country = $xml->geoplugin_countryName ;
        if($country != $country_to_check)
        {
    	   return false;
    	} else { 
    	   return true;
    	}
    }

then in your theme .tpl file you can use this:

{if FrontController::CheckGuestCountry('Switzerland')}
Customer from switzerland
{else}
Customer from somewhere else
{/if}

if you really need module for this, you can put the same function to the module, use public function hookRightColumn($params) to show .tpl file

and then in .tpl file of the module that is called in hookRightColumn use this:
 

{if ModuleName::CheckGuestCountry('Switzerland')}
Customer from switzerland
{else}
Customer from somewhere else
{/if}

where ModuleName is the name of your module

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...