I am currently trying to add on my homepage a custom message based on the location of the visitor. If the visitor have already been to the checkout and choosed shipping Country, then the message will be based on this country. Otherwise we will use the WooCommerce geolocation function.
function get_user_country(){
if( isset($woocommerce->customer) ){
$country = $woocommerce->customer->get_shipping_country();
}
else{
$geo = new WC_Geolocation();
$user_ip = $geo->get_ip_address();
$user_geo = $geo->geolocate_ip( $user_ip );
$country = $user_geo['country'];
}
if( $country == 'CA' ){
echo 'We offer free shipping to Canada';
}
}
My issue is that it seems that the result is store in cache.
When refreshing my homepage, the message is not updated.
And I don't want to exclude my homepage from caching.
I have read that one way to get the country dynamically is to use Ajax instead of php.
But I am a beginner in web development and I am afraid this out of my knowledge for now...
is there any other way to resolve my issue? Thanks.
Related
I have tried using woocommerce geolocate and it's only returning the country as shown below
function custom_use_geolocated_user_country(){
// Geolocation must be enabled # Woo Settings
$location = WC_Geolocation::geolocate_ip();
echo $location;
}
Its returning only the country as shown below
I have tried using other methods of WC_Geolocation Class like geolocate_ip() to get the customer IP address to use with external API services like https://ipinfo.io, which seems close to what I need.
From the image above, I need to get exact region in FCT like Gwarimpa, Kuje, Garki, etc.
Any idea to how to achieve this or getting the customer postcode is welcomed.
I am trying to get my customer's shipping address during checkout. What we are trying to do is make it so that if they attempt to ship by UPS to a PO Box, it outputs an error page. In the checkout page, there is a checkbox where they can check the different addresses that are saved into their account. I've gotten it where it correctly detects whether or not they are using UPS, however I can't get it to properly get the customer's address. No matter what I do, it records the default shipping address, even if they select another one. My question is what can I do to make it where it selects address 2 instead of address one? Here is my code. The reason the error outputs $street is so I can see what is contained in the variable $street.
$quote = $this->getOnepage()->getQuote();
$shippingAddress = $quote->getShippingAddress();
$street = $shippingAddress->getStreet1();
//Check to see if customer is trying to use UPS to ship to a PO Box.
if (preg_match("/p\.* *o\.* *box/i", $street)){
if ((($shippingMethod=="tablerate3_bestway") || ($shippingMethod=="tablerate_bestway") || ($shippingMethod=="tablerate2_bestway"))){
$result = array (
'error' => -1,
'message' => $this->__($street)
);
$this->getResponse()->setBody(Zend_Json::encode($result));
}
}
For shipping address
Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData();
For Billing Address
Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress()->getData();
I have no idea on how to output the geolocation of the user on a WP with woocommerce installed.
From my research I need to use the geolocate_ip function
https://docs.woothemes.com/wc-apidocs/class-WC_Geo_IP.html
I have tried this, but it cannot find the function. I am sure that I have no clue what I am doing.
<?php
$glctest = geolocate_ip( $ip_address = 'get_ip_address()', $fallback = true );
echo $glctest;} ?>
Aside from this, the next thing I am trying to learn is how to default the country in a dropdown form based on his geolocation. If someone can show me how to do this it would be really great...
<?php
$e = new WC_Geolocation();
echo $e->get_ip_address();
?>
Check this out.!
Have you installed Geo IP on your server or site. If not you could use wp-geoip-detect and the in the settings download the current GEO IP lite database.
https://en-gb.wordpress.org/plugins/geoip-detect/
This it what I currently use and it should make this function work.
Once installed I can get the required information required for Geo targeting
i.e. $record = geoip_detect2_get_info_from_current_ip();
$this->countryCode = $record->country->isoCode;
If you want to get the users location as in country or state you can use the below method...
$wcg = new WC_Geolocation();
echo '<pre>', print_r($wcg->geolocate_ip(), true), '</pre>';
The official documentation for this WC_Geolocation class can be found here:
https://docs.woocommerce.com/wc-apidocs/class-WC_Geolocation.html
I am using Magento 1.7.0.2. Whilst on the product page, if a customer attempts to add a quantity greater than we have in stock they receive a message stating ".. the requested quantity is not available".
Is there any way for magento to either email or log when this occurs? I.e. I receive an automatic email stating a customer has attempted to add X number of item X? This would allow me to identify lost sales due to us not having enough stock of a particular item?
Has anyone come across anything like this before or is this even possible?
Thank you in advance
Mike Prentice
yes this is possible
You have to code for this.
I came across this problem one time and what i have do like this below.
I have make one observer event to check if customer is requesting quantity more then available if so i sent email to admin.
What you can do is create one observer for chekout_cart_add_before event in this event you can put your logic.
Or otherwise you can use magento feature Backorders you can find this in inventory tab,if you enable this then customer can order even requested quantity > available quantity, customer can see one message in cart page about backorder.
There is no standart functionality to notify about low quantity products by email.
But there is RSS notification http://www.magentocommerce.com/wiki/modules_reference/english/mage_adminhtml/system_config/edit/cataloginventory
Extend this functionality to match your needs.
You could write some script which would parse RSS, and send email etc.
EDIT
Here is some extension you may like http://www.magentocommerce.com/magento-connect/low-stock-email-notification.html
But is is not free.
Here's how I've done it so that it sends a google analytics tracking event whenever a customer tries to order more than the available stock level.
First copy: app/code/core/Mage/CatalogInventory/Model/Stock/Item.php
To: app/code/local/Mage/CatalogInventory/Model/Stock/Item.php
so that you're not modifying a core file.
In app/code/local/Mage/CatalogInventory/Model/Stock/Item.php add this function
public function notifyOutOfStock($productId){
$session = Mage::getSingleton('checkout/session');
//Initialise as empty array, or use existing session data
$outOfStockItems = array();
if ($session->getOutOfStock()){
$outOfStockItems = $session->getOutOfStock();
}
try {
$product = Mage::getModel('catalog/product')->load($productId);
$sku = $product->getSKu();
if($sku){
//Add the current sku to our out of stock items (if not already there)
if(! isset($outOfStockItems[$sku]) ) {
$outOfStockItems[$sku] = 0;
}
}
} catch (Exception $e){
//Log your error
}
Mage::getSingleton('checkout/session')->setOutOfStock($outOfStockItems);
}
In that same file is another function called checkQuoteItemQty.
Inside that function you need to call your new function using $this->notifyOutOfStock($this->getProductId()); right after it sets each of the error messages and before the return statement.
So:
public function checkQuoteItemQty($qty, $summaryQty, $origQty = 0)
{
....
if ($this->getMinSaleQty() && ($qty) < $this->getMinSaleQty()) {
$result->setHasError(true)
->setMessage(
$_helper->__('The minimum quantity allowed for purchase is %s.', $this->getMinSaleQty() * 1)
)
->setQuoteMessage($_helper->__('Some of the products cannot be ordered in requested quantity.'))
->setQuoteMessageIndex('qty');
//** Call to new function **
$this->notifyOutOfStock($this->getProductId());
return $result;
}
.....
->setQuoteMessageIndex('qty');
//** Call to new function **
$this->notifyOutOfStock($this->getProductId());
return $result;
.....
What this does is add your product sku to an array in the checkout session.
This means you will have access to that info in the template file right after your page loads displaying the "Insufficient stock" notification.
So in one of your template files you can add some code to render the necessary JavaScript.
I've chosen header.phtml since it loads on every page. (Users can add quantities of items to the cart in the cart page as well as the product view page).
app/design/frontend/CUSTOMNAME/default/template/page/html/header.phtml
Somewhere down the bottom of the code add this:
<!-- GA tracking for out of stock items -->
<script>
try {
<?php
$session = Mage::getSingleton('checkout/session');
if ($session->getOutOfStock()){
$outOfStockItems = $session->getOutOfStock();
foreach($outOfStockItems as $sku=>$value) {
if($value==0){
//Render the GA tracking code
echo "_gaq.push(['_trackEvent', 'AddToCart', 'ProductQtyNotAvailable', '".$sku."']); \r\n";
//Set it to 1 so we know not to track it again this session
$outOfStockItems[$sku] = 1;
}
}
//Update the main session
Mage::getSingleton('checkout/session')->setOutOfStock($outOfStockItems);
}
?>
}
catch(err) {
//console.log(err.message);
}
</script>
Can confirm this works well and in my opinion is better than an email or RSS feed as you can analyse it along with the rest of your analytics.
Basically i need to link to my website, but i need the currency to change based on what link is used.
i need this for google adwords, if i am targeting ireland in adwords, i need my website to display euros. if i am targeting the U.K. i need it to display in Pounds and so on.
the website is developed in magento, and i have a select box at the top of my page that changes the currency throughout the website.
Any ideas how i can do this, the website is www.funkychristmasjumpers.com
Credit per this link on the Magento forums
You could always add the following bit of code to the top of your /template/directory/currency.phtml file in your theme. I've tested this in a 1.7.0.2 instance and it works nicely.
You just add cy=code to the end of the URL, so for www.funkychristmasjumpers.com it would be http://www.funkychristmasjumpers.com?cy=USD to default to USD. The code applies the currency and then redirects back to the target page
$currentCurrency = $this->getCurrentCurrencyCode();
if(!isset($currentCurrency)) $currentCurrency = 'NA';
$currencies = array("GBP","USD","EUR");
if(isset($_GET['cy']))
{
if (in_array(strtoupper($_GET['cy']),$currencies)) {
if(strtoupper($_GET['cy']) != $currentCurrency)
{
header("Location: ".$this->helper('directory/url')->getSwitchCurrencyUrl()."currency/".$_GET['cy']);
exit;
}
}
}