Woocommerce custom countries sorting in checkout field front end - wordpress

I am trying to change the order of the countries in front end - woocommerce checkout country field.
I'd like to show specific countries first.
Found some php code for it, but it doesnt seem to work to me.
Any help/guidance much appreciated
here is what i tried
add_filter( 'woocommerce_sort_countries', '__return_false' );
add_filter( 'woocommerce_countries', 'handsome_bearded_guy_add_my_country' );
add_filter( 'woocommerce_continents', 'handsome_bearded_guy_add_my_country_to_continents' );
add_filter( 'woocommerce_countries', 'wc_custom_countries_order', 10, 1 );
function handsome_bearded_guy_add_my_country( $countries ) {
$new_countries = array(
'CENTRAL' => __( 'CanaLine.gr', 'woocommerce' ),
'ToumpaSKG' => __( 'CanaLine Τούμπας (SKG)', 'woocommerce' ),
'HlioupoliSKG' => __( 'CanaLine Ηλιούπολης (SKG)', 'woocommerce' ),
'ArgiroupoliATH' => __( 'CanaLine Αργυρούπολης (ATH)', 'woocommerce' ),
'KavalaKAV' => __( 'CanaLine Καβάλας (KAV)', 'woocommerce' ),
);
return array_merge( $countries, $new_countries );
}
function handsome_bearded_guy_add_my_country_to_continents( $continents ) {
$continents['EU']['countries'][] = 'CENTRAL';
$continents['EU']['countries'][] = 'ToumpaSKG';
$continents['EU']['countries'][] = 'HlioupoliSKG';
$continents['EU']['countries'][] = 'ArgiroupoliATH';
$continents['EU']['countries'][] = 'KavalaKAV';
return $continents;
}
function wc_custom_countries_order( $countries ) {
// replace with iso code of the country (example: US or GB)
unset($countries['CENTRAL']);
unset($countries['ToumpaSKG']);
unset($countries['HlioupoliSKG']);
unset($countries['ArgiroupoliATH']);
unset($countries['KavalaKAV']);
// replace with iso code of country AND country name (example: US | United States or GB | United Kingdom (UK)
$countries = ['CENTRAL' => 'CanaLine.gr'] + ['ToumpaSKG' => 'CanaLine Τούμπας (SKG)'] + ['HlioupoliSKG' => 'CanaLine Ηλιούπολης (SKG)'] + ['ArgiroupoliATH' => 'CanaLine Αργυρούπολης (ATH)'] + ['KavalaKAV' => 'CanaLine Καβάλας (KAV)] +$countries;
return $countries;
}

Try to add higher priorities.
add_filter( 'woocommerce_sort_countries', '__return_false', 999 );
add_filter( 'woocommerce_continents', 'assign_countries_to_continent', 999 );
add_filter( 'woocommerce_countries', 'wc_custom_countries_order', 999, 1 );
function assign_countries_to_continent( $continents ) {
$continents['EU']['countries'][] = 'Country1';
$continents['EU']['countries'][] = 'Country2';
$continents['EU']['countries'][] = 'Country3';
$continents['EU']['countries'][] = 'Country4';
$continents['EU']['countries'][] = 'Country5';
return $continents;
}
function wc_custom_countries_order( $countries ) {
$countries = ['Country1' => 'Country 1'] + ['Country2' => 'Country 2'] + ['Country3' => 'Country 3'] + ['Country4' => 'Country 4'] + ['Country5' => 'Country 5'] + $countries;
return $countries;
}

Related

Woocommerce: show the discounted price next to the variation price when the variation is selected

So, I have a woocommerce shop where a 10% discount is applied to all products in the cart and at checkout if a certain payment gateway is used.
My shop has both simple and variable products, and they are all virtual products. I thought of showing on the product page the following information:
the lowest price (for variable products)
the 10% discounted price I could get at checkout.
To do this I used the following code for variable products:
add_filter( 'woocommerce_variable_sale_price_html', 'wpglorify_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wpglorify_variation_price_format', 10, 2 );
function wpglorify_variation_price_format( $price, $product ) {
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'A partire da: %1$s (' . $prices[0]*.9 . '€ se paghi online)' , 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'A partire da: %1$s (' . $prices[0]*.9 . '€ se paghi online)' , 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
return $price;
}
For simple products I used this snippet instead:
add_filter( 'woocommerce_get_price_html', 'change_displayed_price_html', 10, 2 );
function change_displayed_price_html( $price, $product ) {
if( $product->is_type('simple') ){
$regular_price = (float) $product->get_regular_price();
$saving_price = $regular_price*.9;
$price .= sprintf( __(' (€%s se paghi online)', 'woocommerce' ), $saving_price );
}
return $price;
}
Everything works fine, but on variable products I have a problem. When I select a variation, I would like to give the same information next to the price being shown and related to that variation. I tried with the following snippet:
add_filter('woocommerce_available_variation', 'variation_custom_text', 10, 3 );
function variation_custom_text( $price, $product, $variation ) {
$variation_price = (float) $product->get_variation_price();
$saving_price = $variation_price*.9;
$price['availability_html'] .= sprintf( __(' (€%s se paghi online)', 'woocommerce' ), $saving_price );
return $price;
}
The problem is that the discounted price is calculated on the price of the first variation, regardless of which variation I select. For example, if variation A = $100, a discounted price of $90 is shown. If I select variation B = $120, a discounted price of $90 is always shown. Where am I doing wrong?
Thanks for your answers.

Display WooCommerce Unit Prices at Product variations

I like to display unit prices at variable products. I like it to do based on a product attribute whic contains the following: 5 kg, 2 l etc. So, I get the attribute, extract the numbers from it and do some math. The problem is, that all of the code displays the multipier number of 1. Here is my code:
function product_variation_unit_price( $data, $product, $variation ) {
$price = wc_get_price_to_display( $variation );
$unit = array_shift( wc_get_product_terms( $product->id, 'pa_unit', array( 'fields' => 'names' ) ) );
$unit_number_extracted = preg_replace('/[^0-9]/', '', $unit);
if ( (!empty($unit) ) AND $unit_number_extracted == 1 ) {
$unit_price = sprintf( __( 'Gross unit price: %s', 'loremipsum' ), wc_price($price * $unit_number_extracted) );
} else if( (!empty($unit) ) AND $unit_number_extracted > 1 ) {
$unit_price = sprintf( __( 'Gross unit price: %s', 'loremipsum' ), wc_price($price / $unit_number_extracted) );
}
$data['price_html'] .= '<span> ' . $unit_price . '</span>';
return $data;
}
add_filter( 'woocommerce_available_variation', 'product_variation_unit_price', 10, 3 );
I guess I should foreach the variations, but I don't know how to do that in this situation.
Thanks if you can help!

WooCommerce | Customer's total spend on completed orders in a given time period

The code below is said to give the total spent spent over 30 days (if I'm not mistaken). But in my test, it calculates for the current month. How can I not update this code for last 6 months(180 days). The important thing is to be able to easily change the coverage day and make the calculation for the completed orders. Thanks.
Code resource:https://github.com/anaymark/woocommerce-monthly-spent-by-user/blob/master/total-spent-month-by-user.php
function total_spent_for_user_30days( $user_id=null ) {
if ( empty($user_id) ){
$user_id = get_current_user_id();
}
$today_year = date( 'Y' );
$today_month = date( 'm' );
$day = date( 'd' );
if ($today_month == '01') {
$month = '12';
$year = $today_year - 1;
} else{
$month = $today_month - 1;
$month = sprintf("%02d", $month);
$year = $today_year - 1;
}
// ORDERS FOR LAST 30 DAYS (Time calculations)
$now = strtotime('now');
$gap_days = 30;
$gap_days_in_seconds = 60*60*24*$gap_days;
$gap_time = $now - $gap_days_in_seconds;
$args = array(
'post_type' => 'shop_order',
'post_status' => array( 'wc-completed' ),
// all posts
'numberposts' => -1,
// for current user id
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'date_query' => array(
//orders published on last 30 days
'relation' => 'OR',
array(
'year' => $today_year,
'month' => $today_month,
),
array(
'year' => $year,
'month' => $month,
),
),
);
// GET ALL ORDERS
$customer_orders = get_posts( $args );
$count = 0;
$total = 0;
$no_orders_message = __('No orders this month.', 'mytheme');
if (!empty($customer_orders)) {
$customer_orders_date = array();
foreach ( $customer_orders as $customer_order ){
$customer_order_date = strtotime($customer_order->post_date);
// PAST 30 DAYS
if ( $customer_order_date > $gap_time ) {
$customer_order_date;
$order = new WC_Order( $customer_order->ID );
$order_items = $order->get_items();
$total += $order->get_total();
// Going through each current customer items in the order
foreach ( $order_items as $order_item ){
$count++;
}
}
}
$monthly_spent_by_user = floatval( preg_replace( '#[^\d.]#', '', $total, $count ) );
return $monthly_spent_by_user;
} else {
return $no_orders_message;
}
}
add_shortcode( 'spent-last-month', 'total_spent_for_user_30days' );

Average post age in days per taxonomy term

I'm building some stats for a directory website and I got stuck calculating the average age of listings assigned to a particular listing type.
I figured out calculating listing age in days by ID:
$today = date( 'Y-m-d' );
$listing_date = get_the_date( 'Y-m-d', $id );
$diff = strtotime( $today ) - strtotime( $listing_date );
$age = round( $diff / 86400 );
I figured out how to pull listings assigned to a particular listing type by term ID:
$args = array(
'posts_per_page' => -1,
'fields' => 'ids',
'post_type' => 'listing',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'listing-type',
'field' => 'id',
'terms' => $id
)
)
);
$ids = get_posts( $args );
This gives me an array of IDs of all listings assigned to a particular listing type.
Now I need to calculate the total age in days for all those listings I retrieved IDs for.
I figured $cnt = count( $ids ); to get the total number of retrieved listings (IDs), but I can't figure out calculating the total age in days for those.
With a foreach loop :
<?php
// Your function to get the diff
function get_diff($id) {
$today = date( 'Y-m-d' );
$listing_date = get_the_date( 'Y-m-d', $id );
$diff = strtotime( $today ) - strtotime( $listing_date );
$age = round( $diff / 86400 );
return $age;
}
// We want to store all the diff
$diffs = [];
foreach($ids as $id) {
$diffs[] = get_diff($id);
}
// Get the average | We filter to remove empty values
$diffs = array_filter($diffs);
if(count($diffs)) {
echo $average = array_sum($diffs)/count($diffs);
}

How to calculate number of post which have particular meta_key?

I am using repeatable meta boxes what i want to achieve is display number of post which have particular meta key such as
Country Pakistan
City Islamabad
Age 12
Age 12
Meta key "Age" is used in 2 posts.
No i want structure like Age in Post 2 , Islamabad in Post 1 , Pakistan in Post 1.
/**
* Display callback for the submenu page.
*/
function iif_register_submenu_form_details_callback() {
$id = get_current_user_id();
$loop = new WP_Query( array(
'post_type' => 'iff-info-forms',
'post_status' => 'publish',
'author' => $id,
'meta_query' => array(
array(
'key' => 'repeatable_fields',
//'value' => $user_data_new,
'compare' => 'LIKE'
))
) );
//print_r($loop);
while ( $loop->have_posts() ) : $loop->the_post();
$info = get_post_meta(get_the_id(), 'repeatable_fields', true);
if ( $info ) {
// echo '<strong>Emergency Contacts:</strong><br />';
foreach ( $info as $emergency_contact_metas ) {
echo '<strong>' . esc_html( $emergency_contact_metas['iif_label'] ) .'</strong> ' . esc_html( $emergency_contact_metas['iif_details'] ) . '<br />';
}
}
endwhile;
}
OUTPUT SHOWING IS Below
Country Pakistan
City Islamabad
Age 12
Age 12

Resources