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.
Related
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.
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 have a magento website
We have a store
In a store we have multiple store views (US, EU and UK)
Each store view has it's own currency, etc.
The base currency is GBP at the default config (main)
My problem is that the display currencies work well. Each store view has it's own individual price (no automatic conversion). Everything seems to be working and in order. However, on the final payment emails and actual connection with payment providers (PayPal/Sage). The base currency is always used. Although the display appears in the currency for each store view.
My question is why are the store view currencies not used with PayPal, emails, etc. Although the amounts, display currency, etc work fine?
It turns out that Base Currency can be set on each Store View. However, this option was not presented on the admin side. I had to change the system.xml
app/code/core/Mage/Directory/etc/system.xml
<label>Base Currency</label>
I have to set the appropriate to change from 0 to 1
<show_in_store>1</show_in_store>
Once this was done, I could see Base Currency under "Currency Options" even within a store view. This now works well and everything seems to be working fine.
No PHP code changes or any additional plugins required.
When I had run into this issue with a rather large Magento store, this quick fix worked pretty well for me: Magento knowledge-base paypal base currency tweak
Just note that, that fix probably won't work out of the box but it'll take some tweaking
Here it is some solutions.
You might custom some codes
If you are using Paypal Express,
\app\code\core\Mage\Paypal\Model\Express.php
protected function _placeOrder(Mage_Sales_Model_Order_Payment $payment, $amount)
{
$order = $payment->getOrder();
// prepare api call
$token = $payment->getAdditionalInformation(Mage_Paypal_Model_Express_Checkout::PAYMENT_INFO_TRANSPORT_TOKEN);
$api = $this->_pro->getApi()
->setToken($token)
->setPayerId($payment->
getAdditionalInformation(Mage_Paypal_Model_Express_Checkout::PAYMENT_INFO_TRANSPORT_PAYER_ID))
->setAmount($amount)
->setPaymentAction($this->_pro->getConfig()->paymentAction)
->setNotifyUrl(Mage::getUrl('paypal/ipn/'))
->setInvNum($order->getIncrementId())
**->setCurrencyCode($order->getOrderCurrencyCode())** // should be used getOrderCurrencyCode();
->setPaypalCart(Mage::getModel('paypal/cart', array($order)))
->setIsLineItemsEnabled($this->_pro->getConfig()->lineItemsEnabled)
;
if ($order->getIsVirtual()) {
$api->setAddress($order->getBillingAddress())->setSuppressShipping(true);
} else {
$api->setAddress($order->getShippingAddress());
$api->setBillingAddress($order->getBillingAddress());
}
// call api and get details from it
$api->callDoExpressCheckoutPayment();
$this->_importToPayment($api, $payment);
return $this;
}
\app\code\core\Mage\Paypal\Model\Standard.php
public function getStandardCheckoutFormFields()
{
$orderIncrementId = $this->getCheckout()->getLastRealOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
/* #var $api Mage_Paypal_Model_Api_Standard */
$api = Mage::getModel('paypal/api_standard')->setConfigObject($this->getConfig());
$api->setOrderId($orderIncrementId)
**->setCurrencyCode($order->getOrderCurrencyCode())** // should be used getOrderCurrencyCode();
//->setPaymentAction()
->setOrder($order)
->setNotifyUrl(Mage::getUrl('paypal/ipn/'))
->setReturnUrl(Mage::getUrl('paypal/standard/success'))
->setCancelUrl(Mage::getUrl('paypal/standard/cancel'));
// export address
$isOrderVirtual = $order->getIsVirtual();
$address = $isOrderVirtual ? $order->getBillingAddress() : $order->getShippingAddress();
if ($isOrderVirtual) {
$api->setNoShipping(true);
} elseif ($address->validate()) {
$api->setAddress($address);
}
// add cart totals and line items
$api->setPaypalCart(Mage::getModel('paypal/cart', array($order)))
->setIsLineItemsEnabled($this->_config->lineItemsEnabled)
;
$api->setCartSummary($this->_getAggregatedCartSummary());
$api->setLocale($api->getLocaleCode());
$result = $api->getStandardCheckoutRequest();
return $result;
}
In one page check out module when am calling getCustomerBllAddr() function.it fatching complete address. i want to get diffrent element of address like name,city etc.
how can i get this?
how to use format function f possible?
With the object returned by the getCustomerBillingAddress method you can do the following:
$street = $address->getStreet();//or $address->getData('street')
$country = $address->getCountryId();//or $address->getData('country_id');
Basically any element from the $_data member of the address can be called like this.
[EDIT]
You can get the customer billing address like this:
$address = $this->getCustomer()->getPrimaryBillingAddress()
Then you can work do $address->getStreet() and other.