Get user geolocated country name in Woocommerce 3 - php

I would like to add "We ship to {country name}" in WooCommerce header based on user geoip country name?
I would like to write html content in the header of my WooCommerce store, such as We ship to "Your Country name".
Any help will be really appreciated?
I have WooCommerce geolocation enabled already.

You can make a custom function based on WC_Geolocation Class this way:
function get_user_geo_country(){
$geo = new WC_Geolocation(); // Get WC_Geolocation instance object
$user_ip = $geo->get_ip_address(); // Get user IP
$user_geo = $geo->geolocate_ip( $user_ip ); // Get geolocated user data.
$country = $user_geo['country']; // Get the country code
return WC()->countries->countries[ $country ]; // return the country name
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
USAGE
You will add the following code in your child theme's header.php file:
1) in between html code:
<?php printf( '<p>' . __('We ship to %s', 'woocommerce') . '</p>', get_user_geo_country() ); ?>
2) or in between php code:
printf( '<p>' . __('We ship to %s', 'woocommerce') . '</p>', get_user_geo_country() );
Converting this to Shortcode:
function get_user_geo_country(){
$geo = new WC_Geolocation(); // Get WC_Geolocation instance object
$user_ip = $geo->get_ip_address(); // Get user IP
$user_geo = $geo->geolocate_ip( $user_ip ); // Get geolocated user data.
$country = $user_geo['country']; // Get the country code
return sprintf( '<p>' . __('We ship to %s', 'woocommerce') . '</p>', WC()->countries->countries[ $country ] );
}
add_shortcode('geoip_country', 'get_user_geo_country');
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Normal shortcode usage (in the backend text editor):
[geoip_country]
or in php code:
echo do_shortcode( "[geoip_country]" );

Related

WooCommerce order received url link shortcode displays nothing

I am needing help on this custom code I have created. The purpose of this shortcode is to display a clickable link to the customers order received page on the order received email. I want the link to be accessible for all customers including those that do not have an account. The order status when order is placed is on-hold if that helps.
function emailurlorder()
{
$order = wc_get_order( $order_id );
if ( $order ) {
$order->get_checkout_order_received_url();
return;
{
$checkout_order_received_url = $order->get_checkout_order_received_url();
if ($order_checkout_order_received_url) {
return $order_checkout_order_received_url;
}
ob_start();
$pay_link = $checkout_order_received_url;
$payment_text = __('Click here to pay','text_domain');
echo '' . $payment_text . '';
$contents = ob_get_contents();
ob_end_clean();
return $contents; '' . $payment_text . '';
}
}
}
add_shortcode('orderlink','emailurlorder');
What it is suppose to do is display a clickable link labeled as "Click here to pay" and the url is my websites order received page for that specific order.
Problem- Shortcode displays absolutely nothing not even the shortcode text ([orderlink]).

Require theme with a wordpress plugin

I have created an elementor widgets plugin for my theme and now want to package my theme with my plugin. Like if a user installed an individual plugin without my theme then will throw the error 'Require my theme'
You can check the name of the active (parent) theme and throw a notice if it doesn't match with your theme. And set i.e. a link to the download page.
Put the code into your main plugin file:
function check_my_theme() {
$theme = wp_get_theme();
$my_theme_name = 'My Theme'; // Edit Theme name.
if( $theme->name != $my_theme_name && $theme->parent_theme != $my_theme_name ) {
$class = 'notice notice-error';
$message = __( 'You are using the wrong Theme. Take instead ', 'your-plugin' );
$link_to_theme = '#'; // Edit link to theme.
printf( '<div class="%1$s"><p>%2$s%4$s</p></div>',
esc_attr( $class ),
esc_html( $message ),
esc_url( $link_to_theme ),
esc_html( $my_theme_name )
);
}
}
add_action( 'admin_notices', 'check_my_theme');

Access downloadable data from WooCommerce downloadable products

I'm trying to fetch the WooCommerce products meta data using $product = new WC_Product( get_the_ID() ); I'm getting the product price and all the other values the products are downloadable WooCommerce products, I want to fetch the following data:
Whenever i try to fetch $product->downloads->id or $product->downloads->file i'm getting null in return. Please tell me what i'm doing wrong over here.
To access all product downloads from a downloadable product you will use WC_Product get_downloads() method.
It will give you an array of WC_Product_Download Objects which protected properties are accessible through WC_Product_Download available methods (since WooCommerce 3):
// Optional - Get the WC_Product object from the product ID
$product = wc_get_product( $product_id );
$output = []; // Initializing
if ( $product->is_downloadable() ) {
// Loop through WC_Product_Download objects
foreach( $product->get_downloads() as $key_download_id => $download ) {
## Using WC_Product_Download methods (since WooCommerce 3)
$download_name = $download->get_name(); // File label name
$download_link = $download->get_file(); // File Url
$download_id = $download->get_id(); // File Id (same as $key_download_id)
$download_type = $download->get_file_type(); // File type
$download_ext = $download->get_file_extension(); // File extension
## Using array properties (backward compatibility with previous WooCommerce versions)
// $download_name = $download['name']; // File label name
// $download_link = $download['file']; // File Url
// $download_id = $download['id']; // File Id (same as $key_download_id)
$output[$download_id] = ''.$download_name.'';
}
// Output example
echo implode('<br>', $output);
}
Related answers:
Add downloads in Woocommerce downloadable product programmatically
Add programmatically a downloadable file to Woocommerce products
If you're trying to get the download link, try:
$downloads = $product->get_downloads();
This should return an array, so you can use it for example like:
foreach( $downloads as $key => $download ) {
echo 'Download File';
}

Add customer email in a text on WooCommerce Order received page

In WooCommerce, on top of my thank you / order-received page, I've added a custom text, with the following code:
add_action( 'woocommerce_thankyou', 'my_order_received_text', 1, 0);
function my_order_received_text(){
echo '<div class="my_thankyou2"><p>' . __('Your download link was sent to: ') . '</p></div>' ;
}
How can I get the email address of the customer added to the end of the custom text?
To get the customer billing email, you can use one of those:
The Woocommerce WC_Order method get_billing_email()
The WordPress function get_post_meta() with the meta key _billing_email from order ID.
Now you can set the text in 2 different locations:
1) On top of Order received page:
add_filter( 'woocommerce_thankyou_order_received_text', 'my_order_received_text', 10, 2 );
function my_order_received_text( $text, $order ){
if( ! is_a($order, 'WC_Order') ) {
return $text;
}
// Get Customer billing email
$email = $order->get_billing_email();
return $text . '<br>
<div class="my_thankyou2"><p>' . __('Your download link was sent to: ') . $email . '</p></div>' ;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
2) On bottom of Order received page:
Using the WC_Order method get_billing_email() this way:
add_action( 'woocommerce_thankyou', 'my_order_received_text', 10, 1 );
function my_order_received_text( $order_id ){
if( ! $order_id ){
return;
}
$order = wc_get_order( $order_id ); // Get an instance of the WC_Order Object
$email = $order->get_billing_email(); // Get Customer billing email
echo '<div class="my_thankyou2"><p>' . __('Your download link was sent to: ') . $email . '</p></div>' ;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Alternatively, using WordPress get_post_meta() function, replacing in the function:
$order = wc_get_order( $order_id ); // Get an instance of the WC_Order Object
$email = $order->get_billing_email(); // Get Customer billing email
By the following line:
$email = get_post_meta( $order_id, '_billing_email', true ); // Get Customer billing email

Wordpress function advice - is this possible to target a specific function and execute another function after it?

Ok... so working on a theme designed by Astoundify... which although beautiful has been disgustingly made.
Also using WP Job Manager.
Have created a new custom field, 'fax'. Want fax to display after the phone number. Astoundify has nicely made this display on the theme using a widget - making the WP Job Manager hooks completely useless.
What i need to do is get that fax number to display immediately after phone. Is this possible to do with the functions file so that i don't have to modify the core files?
The following is the code which is outputting the phone number:
public static function the_phone() {
global $post;
$phone = $post->_phone;
if ( ! $phone ) {
return;
}
?>
<div class="job_listing-phone">
<span itemprop="telephone"><a href="tel:<?php echo esc_attr( preg_replace( "/[^0-9,.]/", '', $phone ) ); ?>"><?php echo
esc_attr( $phone ); ?></a></span>
</div>
<?php
}
And i am trying to output the fax using the following:
add_action( 'single_job_listing_start', 'display_fax_number_data', 50 );
function display_fax_number_data() {
global $post;
$fax = get_post_meta( $post->ID, '_fax', true );
if ( $fax ) {
echo '<li>' . __( 'Fax:' ) . ' ' . $fax . '</li>';
}
}
And its working... just not in the right place.
Have lost a day trying to do something that should have been so simple.
Any help appreciated.

Categories