Magento Get Customer Shipping Address During Checkout - php

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();

Related

Show custom message using WooCommerce geolocation or shipping country

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.

Get user region/town with woocommerce geolocate

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.

woocommerce Client city to be sent on ‘new order’ email

We get constant fake orders from our competition, we need to see the real city behind the IP the client placed the order, so we would like to see some field called ‘real city’ on the email that we received on each order. Until now we managed to get the IP by adding this line on function.php but its time consuming to open browser and manually search the city to see if match the client order city. As you may notice we added the real city line but its only extracting what the client selects so its not good, could I get the city name based on IP ? Thank you !
function send_customer_city( $order, $sent_to_admin, $plain_text, $email ){
if( ‘new_order’ == $email->id ){
echo ‘<br>
<p>‘.__(‘Client IP’).’: ‘. get_post_meta( $order->id, ‘_customer_ip_address’, true ).'</p>
<p>‘.__(‘Real City’).’: ‘. get_post_meta( $order->get_id(), ‘_billing_city’, true ).'</p>’;
}
}
In place of doing this You can do one thing
You can place a hidden filed on the checkout form and set the city according to IP/LAT-LONG
using any API. using woocommerce hooks woocommerce_after_checkout_billing_form
AT the time of placing an order You can get this filed data as well to check that Is This Real order Or fake?
OR
You can also check this before placing an order to avoid fake orders creations using woocommerce_review_order_before_submit or any other hook

Woocommerce - Products ship to only a few cities locally

How do I go about limiting some products to be shipped locally only (a range of Zip codes)? P.S: Other products may ship locally and/or country-wide.
A scenario of this situation would be:
the user selects a product, adds it to cart, checks out, enters the shipping address, then the system would notify him that this item is only available for shipping locally.
I checked all the settings on Woocommerce but nothing worked.
Try looking for a plugin:
https://woocommerce.com/product-category/woocommerce-extensions/shipping-methods/delivery-shipping-options/
If that doesn't work, you can code it:
You need to flag some products as 'local delivery only', but not others. That way at checkout time you can display the appropriate shipping options. You'll also need to know the user's Zip code etc.
Step 1: Add the flag to the product.
Create a check box on the product that allows you to flag it's a local ship item.
The code below will add the option in Products -> Product data -> Advanced
add_action('woocommerce_product_options_advanced', 'localship_options');
function localship_options()
{
echo '<div class="options_group">';
woocommerce_wp_checkbox(array(
'id' => 'localship',
'value' => get_post_meta(get_the_ID(), 'localship', true),
'label' => 'Local Ship',
'desc_tip' => true,
'description' => 'Only ships locally',
));
echo '</div>';
}
add_action('woocommerce_process_product_meta', 'localship_save_fields', 10, 2);
function localship_save_fields($id, $post)
{
if (!empty($_POST['localship'])) {
update_post_meta($id, 'localship', $_POST['localship']);
} else {
delete_post_meta($id, 'localship');
}
}
To see if a product is flagged or not, use get_post_meta. You'll need the id of the
product
$is_localship = get_post_meta( /*product id here */, 'localship', true);
Now you can use $is_localship to see if the product is a ship local.
Step 2: Check the Buyer's Zip Code
You'll need to get the user's order information.
The wc_get_order function should do it:
$order = wc_get_order( $order_id )
From there you should be able to pull out the user's address, zip code etc.
See if the buyer's zip code is in an array of local Zip codes.
Step 3: Display or Hide Shipping Accordingly
Now that you know if the product is local ship only and the Zip code is a match, you can show or hide the shipping option in your template.
You can use the $is_localship to display or hide the shipping box in your template.
That should get you going in the right direction.

Magento invoice get shipping address

I want to show the shipping address of an order on my invoices. This is working pretty well with:
$shipping = nl2br($order->getShippingAddress()->format('html'));
$shipping = explode('<br />',$shipping);
But if I have an order for a download product (gift card) I get a:
Fatal error: Call to a member function format()...
Because the order does not have a shipping address. How can I check, whether
$order->getShippingAddress() contains any data or, if not, not displaying it?

Categories