Domanda

I have a magento store that also has a link labeled 'shop local' that a customer can click to get information on our one retail location.

As we only have one store in one state, I would like to somehow only show that link if a customers IP is from the appropriate state. I would prefer a way with CSS, but realize this probably requires js. Thanks in advance.

Pleas note: our ecommerce store is open to all of the US. So the reason to hide the link for other 49 states is to reduce confusion.

È stato utile?

Soluzione

One possible way to go is with GeoIP. GeoIP is a database that holds geolocation infos.

There are paid versions but also free ones.

You can use various extensions to interact with GeoIP, for example Sandfox_GeoIP.


Update: according to the comments below I added some sample-code for getting/setting the country info. While adding the answers I found there's also a GeoIP JavaScript API. You might also want to have a look at this.

First: There is no possibility to detect a IP or state with CSS only.

Solution 1: Hide via CSS

Add the CSS if required (check for state) in your template file (.phtml):

<?php $geoip; // some GeoIP-model you can access ?>
<a href="http://linktoshoplocal.com" class="shoplocal <?php echo $geoip->getState()!="FL" ? "hide" : ""; ?>">shop local</a>

Please note: $geoip->getState() is just a sample method here. Replace it with the specific method of your GeoIP implementation.

Your css class needs to be:

.hide { display: none; }

Solution 2: Do not output link (better approach than 1)

<?php $geoip; // some GeoIP-model you can access ?>
<?php if($geoip->getState()=="FL"): ?>
   <a href="http://linktoshoplocal.com" class="shoplocal">shop local</a>
<?php endif; ?>

No CSS rule is required here, as the link will only be in the output if the state matches.

Solution 3: Use the GeoIP JavaScript API

  1. Load GeoIP JavaScript API
  2. Add a onload event to your document
  3. Check with GeoIP API if a state is set, if not, remove/hide link via JavaScript.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top