Hello I cant get from woocommerce order shipping address, I get error:
Fatal error: Call to a member function get_formatted_address() on a non-object in ..
I am using code:
$order_data = new WC_Order('12');
print_r($order_data->get_formatted_shipping_address());
Here is a workaround
// 123 Happy Coding Lane, Apt #5 Delray Beach, FL 33445
function formatted_shipping_address($order)
{
return
$order->shipping_address_1 . ', ' .
$order->shipping_address_2 . ' ' .
$order->shipping_city . ', ' .
$order->shipping_state . ' ' .
$order->shipping_postcode;
}
echo formatted_shipping_address($order);
You can access the shipping & billing properties directly.
Shipping
$order->shipping_first_name
$order->shipping_last_name
$order->shipping_company
$order->shipping_address_1
$order->shipping_address_2
$order->shipping_city
$order->shipping_state
$order->shipping_postcode
$order->shipping_country
Billing
$order->billing_first_name
$order->billing_last_name
$order->billing_company
$order->billing_address_1
$order->billing_address_2
$order->billing_city
$order->billing_state
$order->billing_postcode
$order->billing_country
You can get all the shipping address fields very simple, with this code that work for me:
Code:
$order = new WC_Order($order_id); // Order id
$shipping_address = $order->get_address('shipping');
print_r($shipping_address);
The function get_address() by default returns the billing address details but you can get the shipping address details passing the parameter 'shipping'
Here are the docs: Woocommerce docs
Regards!!
Remove single quotes because WC_Order constructor accepts integer parameter order id.
$order_data = new WC_Order(12);
print_r($order_data->get_formatted_shipping_address());
Use this
$order->get_shipping_address_1();
$order->get_shipping_address_2();
Related
I am using [_remote_ip] in the email I am reciveng from contact form 7, is there is a way to show the country name or flag instead of the received IP number?
The main issue started when I activated caching, where the php country locate stopped working, so I have to use the [_remote_ip] in the email to know the location of the contact, but it's not a good solution that I always have to locate the ip manually.
That's why I want to check if there is a way to show that IP as a name or as a flag.
This will add a form tag to Contact form 7 [country] - Add this to your functions.php or make it a plugin. TO use this, you'll need a subscription (free tier available) to https://ipstack.com.
add_action('wpcf7_init', function (){
wpcf7_add_form_tag( 'country' , 'cf7_ip_to_country_form_tag' );
});
function cf7_ip_to_country_form_tag($tag){
// Your API Key for https://ipstack.com is required
$api_key = 'xxxxxxx';
// Get API Response
$url = 'http://api.ipstack.com/' . $_SERVER['REMOTE_ADDR'] . '?access_key=' . $api_key;
$response = wp_remote_get($url);
$body = json_decode(wp_remote_retrieve_body($response));
/* $body = Object returned from ipstack.
* $body->country_name = Full Country Name
* $body->country_code = 2 Letter ISO Abbrev
* set the value based on what you want.
*/
return '<input type="hidden" name="country" value="'.$body->country_name.'">';
}
I need to send a copy of the order email to a second email adress defined by a custom field.
I have installed the iconic custom account field plugin and created a custom field on the user (I works and I can edit it in admin).
https://iconicwp.com/blog/the-ultimate-guide-to-adding-custom-woocommerce-user-account-fields/
It's named 'iconic-register-email'
I have also implemented this https://wordpress.stackexchange.com/questions/92020/adding-a-second-email-address-to-a-completed-order-in-woocommerce solution to send an extra email. It works with a hard-coded email.
Now I need to combine the solutions and I just can't get it to work. I have appended the send email code at the bottom of the iconic plugin code.
add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2);
function mycustom_headers_filter_function( $headers, $object ) {
$sae = 'a-custom-email#gmail.com';
if ($object == 'customer_completed_order') {
$headers .= 'BCC: Name <' . $sae . '>' . "\r\n";
}
return $headers;
}
This code works, but I need to use iconic-register-email field.
Need to fetch the registred email adress on a user, so when that user makes an order an order copy is sent to that adress defined by iconic-register-email.
My knowledge about coding is very limited, please help. Last thing to add before I can go live.
Managed to solve it. Not sure if it's the most elegant solution, but it works.
As it's predefined functions and arrays, this is how I did it
function mycustom_headers_filter_function( $headers, $object ) {
$sae = iconic_get_userdata( get_current_user_id(), 'iconic-register-email' );
if ($object == 'new_order') {
$headers .= 'BCC: <' . $sae . '>' . "\r\n";
}
return $headers;
}
Is there any mail-tags to use?
for instance; for tracking client's ip we can use; [_remote_ip]
First step is to create an API key. To get an API key we have to register in the site IPInfoDB.
Once API key is ready we have to download the file ip2locationlite.class.php from the site IPInfoDB.
Next step is to create our custom plugin.I named it as "country_custom_plugin.php". Its always good to create a custom plugin inside a folder, so that all the required files for the corresponding plugin stays in the folder. Named the folder as "country_custom_plugin"
Move the file "ip2locationlite.class.php" to the folder "country_custom_plugin".
/*Calling the function from contact-form-7 module and passing the result of the function stylus_ip_location_get_cc */
add_filter( 'wpcf7_special_mail_tags', 'country_custom_ip_location', 10, 2 );
/*Function to get location of an user from the ip address*/
function country_custom_ip_location( $output, $name ){
/*including the third party integration to get IP Location*/
include_once('ip2locationlite.class.php');
/*Special tag values are passed in format wpcf7.$name which we convert to _$name*/
$name = preg_replace( '/^wpcf7\./', '_', $name );
/*If location is requested in contact form enter the loop*/
if ( '_custom_ip_location' == $name ) {
$ipLite = new ip2location_lite;
/*Entering the API key value generated*/
$ipLite->setKey('"Enter your API Key Here"');
/*Getting the IP address*/
$ipaddress = preg_replace( '/[^0-9a-f.:, ]/', '', $_SERVER['REMOTE_ADDR'] );
/*Getting the Location*/
$visitorGeolocation = $ipLite->getCity($ipaddress);
if (!empty($visitorGeolocation) && is_array($visitorGeolocation)) {
$output = $visitorGeolocation['regionName'] . ', ' . $visitorGeolocation['countryName'] . ', ' . $visitorGeolocation['countryCode'];
}
}
return $output;
}
Reference.Hope this will help. Please let me know if any issue.
I am working in woocommerce add order from API. I already added order by using wc_create_order() function. In that, I have added shipping address, billing address, and product details as well. But the problem is I want to add shpping cost as per shipping zone.
Here is screenshot :
However I found class with function called : WC_Shipping_Zones::get_zones().
But it return unarranged (unmanaged) array.
I have state code and then I want to get shipping cost from state code.
However, I have found zone_id.
I have already tried to use WC_Shipping_Zones::get_zone_by('zone_id',$zone_id),WC_Shipping_Zones::get_zone($zone_id);
But none of functions return cost anyhow.
Here is the code to get the zone cost. I hope it helps.
global $post, $woocommerce;
$delivery_zones = WC_Shipping_Zones::get_zones();
foreach ((array) $delivery_zones as $key => $the_zone ) {
echo $the_zone['zone_name'];
echo "<br/>";
foreach ($the_zone['shipping_methods'] as $value) {
echo $value->cost;
}
echo "<br/>";
}
I have this error in my woocommerce web site:
Gateway Disabled: PayPal does not support your store currency.
Any one have specific solution for this?
I am using currency Saudi Riyaal(SAR) and american Dollars($).
I have been working on this problem for few days and here are the solutions I found. In my case I need BGN, so the codes are for that currency (they can be easily adapted for other currencies).
=== Solution 1 ===
The code in this solution is for a plugin file called paypal-bgn-to-euro.php (saved as UTF-8 without BOM for the Bulgarian words) that is in a folder called paypal-bgn-to-euro. The plugin tells PayPal to support BGN. Then after the user leaves the order page and goes to the PayPal site, it converts the currency (and amount) to EUR (an actual PayPal supported currency). The exchange rate is updated automatically twice daily from a free API. The user pays in EUR but the created order in WooCommerce is still in BGN. WooCommerce detects an error (or sometimes two) in the validation because the currencies (and amounts) do not match and it puts the order on hold. The plugin then changes the status to processing for any order with an order note saying something about PayPal currencies or amounts not matching. Read the comments in the code for more info.
<?php
/*
Plugin Name: Paypal BGN support
Description: Plugin description here.
Author: Nikolay Nikolov
Version: 1.0.0
*/
/////////////////////BEGIN segment 1
//this segment I got from here and changed it a little: http://devseon.com/en/wordpress-tutorials/woocommerce-add-a-paypal-unsupported-currency
//it lies to PayPal that the BGN currency is supported, and converts it to EUR when the payment is made
//it does not change the currency in the WooCommerce order, it remains the same, and WooCommerce detects an error and it puts the order on hold
//but we fix that in segment 3
add_filter( 'woocommerce_paypal_supported_currencies', 'pbte_add_bgn_paypal_valid_currency' );
function pbte_add_bgn_paypal_valid_currency( $currencies )
{
array_push ( $currencies , 'BGN' );
return $currencies;
}
add_filter('woocommerce_paypal_args', 'pbte_convert_bgn_to_eur');
function pbte_convert_bgn_to_eur($paypal_args)
{
if ( $paypal_args['currency_code'] == 'BGN')
{
$convert_rate = get_option('pbte_eur_to_bgn_rate'); //set the converting rate
$paypal_args['currency_code'] = 'EUR'; //change BGN to EUR
$i = 1;
while (isset($paypal_args['amount_' . $i]))
{
$paypal_args['amount_' . $i] = round( $paypal_args['amount_' . $i] / $convert_rate, 2);
++$i;
}
}
return $paypal_args;
}
/////////////////////END segment 1
/////////////////////BEGIN segment 2
//I made this segment so the exchange rate is updated automatically with a wordpress cron job twice daily
//runs on plugin activation
register_activation_hook( plugin_dir_path( __FILE__ )."paypal-bgn-to-euro.php", 'pbte_activate_plugin' );
//runs on plugin deactivation
register_deactivation_hook( plugin_dir_path( __FILE__ )."paypal-bgn-to-euro.php", 'pbte_deactivate_plugin' );
//when the cron job runs, we call a function to update the exchange rate option value
add_action('pbte_twicedaily_check_eur_event', 'pbte_update_eur_rate_option');
//runs on plugin activation
function pbte_activate_plugin()
{
pbte_update_eur_rate_option(); //we update the exchange rate option
if (!wp_next_scheduled('pbte_twicedaily_check_eur_event')) //adds an cron job (if it is not already added) to udpate the exchange rate twice daily
wp_schedule_event(time(), 'twicedaily', 'pbte_twicedaily_check_eur_event');
}
//runs on plugin deactivation
function pbte_deactivate_plugin()
{
wp_clear_scheduled_hook('pbte_twicedaily_check_eur_event'); //removes the cron job we added
}
//gets the exchange rate from a free api and updates our option
function pbte_update_eur_rate_option()
{
$data = json_decode(file_get_contents("http://api.fixer.io/latest?symbols=BGN&base=EUR")); //gets the exchange rate from a free api
if(!empty($data->rates->BGN))
{
if(get_option('pbte_eur_to_bgn_rate'))
update_option('pbte_eur_to_bgn_rate', floatval($data->rates->BGN));
else
add_option('pbte_eur_to_bgn_rate', floatval($data->rates->BGN)); //if the option does not exist for some reason, we create it
}
else //something went wrong while getting the data from the api so we will email the admin
{
$message = "This is a message from ".get_site_url()
.". There is a problem getting the API data in the plugin PayPal BGN support.";
$subject = "Problem with Paypal BGN support";
$to_email = get_bloginfo('admin_email');
$headers[] = 'Content-Type: text/html; charset=UTF-8';
wp_mail($to_email, $subject, $message, $headers);
}
}
/////////////////////END segment 2
/////////////////////BEGIN segment 3
//Since the currencies do not match, WooCommerce puts the order on hold. We fix this with this segment.
//this runs when a new note is added to the order
add_filter( 'woocommerce_new_order_note_data', 'pbte_fix_order_status', 10, 2 );
//if the note says that the PayPal currencies or amounts do not match, then we will change the status to processing
function pbte_fix_order_status($a_note, $a_order)
{
//the check is done in two languages
if ( strpos($a_note['comment_content'],'PayPal валутите не съвпадат') !== false
|| strpos($a_note['comment_content'],'PayPal currencies do not match') !== false
|| strpos($a_note['comment_content'],'PayPal наличността не отговаря') !== false
|| strpos($a_note['comment_content'],'PayPal amounts do not match') !== false )
{
//we create the order var
$order = new WC_Order($a_order['order_id']);
//if the current status is on-hold - we change it to processing and add an optional note
if($order->status == 'on-hold')
$order->update_status('processing', 'The PayPal BGN support plugin did this note.');
}
return $a_note;
}
/////////////////////END segment 3
?>
=== Solution 2 ===
--- Step 1 ---
You need to add this code with a small plugin or in your functions.php:
add_filter( 'woocommerce_paypal_supported_currencies', 'add_my_own_paypal_currency' );
function add_my_own_paypal_currency( $currencies )
{
array_push ( $currencies , 'BGN' );
return $currencies;
}
--- Step 2 ---
We convert the currency (and amount) to a supported one while the user is on the order page in our site, as he chooses the PayPal method.
So we need a different currency for different WooCommerce gateways. Which is exactly one of the features of a free plugin called Booster for WooCommerce (it has a paid version too). This feature is called Payment Gateways Currency.
When we activate the plugin and the selected feature we can choose a different currency only for PayPal and we enter there a conversion rate (for some reason it wants a comma for a separator and not a point). The paid version though is said to support automatic updates of the conversion rates (I haven't tested it).
After the payment is made, the order in WooCommerce is with the new currency now (not like Solution 1). Which might affect other plugins that you are using if they assume all orders are in the WooCommerce store currency.
=== Solution 3 ===
I haven't tested this one, but I found this paid plugin called "PAYPAL CURRENCY CONVERTER PRO FOR WOOCOMMERCE" and the author claims that it solves the problem.
if you are still having issues with this, here is a solution which i found.
Change the currency to correspond to your currency.
add_filter( 'woocommerce_paypal_supported_currencies', 'add_a_paypal_valid_currency' );
function add_a_paypal_valid_currency( $currencies ) {
array_push ( $currencies , 'AED' ); // AED = United Arab Emirates Dirham.
return $currencies;
}
add_filter('woocommerce_paypal_args', 'convert_to_usd');
function convert_to_usd($paypal_args){
if ( $paypal_args['currency_code'] == 'AED'){
$convert_rate = 3.67; // set the converting rate here
$paypal_args['currency_code'] = 'USD'; // change to USD
$i = 1;
while (isset($paypal_args['amount_' . $i])) {
$paypal_args['amount_' . $i] = round( $paypal_args['amount_' . $i] / $convert_rate, 2);
++$i;
}
}
return $paypal_args;
}