Does anyone know how to get the shipping zone from woocommerce?
I think this comes from the table rate shipping extension but has now been added to WC core.
I'm guessing it's similar to
WC()->customer->get_shipping_country();
Thanks!
You can easily get the shipping zones by following given code
$delivery_zones = WC_Shipping_Zones::get_zones();
foreach ((array) $delivery_zones as $key => $the_zone ) {
echo $the_zone['zone_name'];
}
It will return the zone information for each zone.
Related
We have a website running on woocommerce and we run a custom tax solution there. On frontend everything works perfect atleast in admin as we have set, that tax is calculated by store address, we get the basic store country into all of the orders. What we are trying to achieve is to replace this country in admin from current open order post meta. But we are unlucky and we are not getting any data. Is there any possiblity to get current open post meta value?
function custom_base_country($base_country ){
if (is_admin()){
global $woocommerce;
$order = $_GET['post'];
$country = get_post_meta($order->id, 'country_code', true);
$base_country = $country;
}
return $base_country;
}
add_filter('woocommerce_countries_base_country', 'custom_base_country', 10, 1);
Found out the answer by changing the code to $country = get_post_meta($order, 'country_code', true);
I'm trying to write a function to display different custom thank you pages according to which product you purchased. The shop is simple, you can make a one-time donation or register for monthly donations to a non-profit. I want to display different thank you pages depending on whether which you did. I have several monthly donation products and one for one-time, separated into differnt categories. What I want to do is get the category of the products just purchased (one can only order either, not both, at a time), and use that to display the correct message.
function get_order_cat($order_id) {
$order = wc_get_order($order_id);
$items = $order->get_items();
print_r($items); // Returns the data, but it's protected !
}
This returns an array of objects, but the data is :protected. Any tips on how to access this, or get the result I'm looking for another way?
you can retrieve categories with that :
$order = wc_get_order($order_id);
foreach ($order->get_items("line_item") as $id_line_item => $item) {
$product_ID = $item->get_product_id();
$categories = get_the_terms($product_ID, "product_cat");
}
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/>";
}
Our store has been setup to process orders only within Sydney city. We manage this in Woocommerce by setting the allowed postcodes for a Flat Rate Delivery.
We are now extending deliveries to other other areas but only via telephone (no online orders for these new postcodes). Woocommerce displays a standard No Shipping message but we would to customise such that
Customer enters a postcode within the city, allow online orders. No
change to current behaviour.
Customer enters a postcode for which
telephone orders are allowed, show a customised message asking the
customer to make the call.
All other postcodes, disallow orders.
Any technical direction will be greatly appreciated.
Thanks.
Below code should help you. Change the value of array variable $zip_array in both the functions as a comma separated list of zip codes, which you want to show a custom message. Also, change the string value of $custom_msg to your custom message. More details, please refer this article.
// For Cart Page.
add_filter( 'woocommerce_no_shipping_available_html', 'wf_customize_default_message', 10, 1 );
// For Checkout page
add_filter( 'woocommerce_cart_no_shipping_available_html', 'wf_customize_default_message', 10, 1 );
function wf_customize_default_message( $default_msg ) {
$zip_array = array(
'30031',
);
if ( in_array( WC()->customer->get_shipping_postcode() , $zip_array) ) {
$custom_msg = "Call us for quotation - 1-800-XXX-XXXX";
if( empty( $custom_msg ) ) {
return $default_msg;
}
return $custom_msg;
}
return $default_msg;
}
add_filter('woocommerce_package_rates', 'wf_remove_shipping_options_for_particular_zip_codes', 8, 2);
function wf_remove_shipping_options_for_particular_zip_codes($rates, $package)
{
global $woocommerce;
$zip_array = array(
'30031',
);
if ( in_array( $woocommerce->customer->get_shipping_postcode() , $zip_array) ) {
$rates = array();
}
return $rates;
}
On the checkout page, we've a currency switcher for USD, EUR, INR, GBP currencies.
However, out of 3, only 1 gateway supports all currencies, & 2 supports INR.
So, I need to hide other two gateways, if someone selects USD, EUR & GBP
Can we use selected currency & do the needed function?
TIA
I use this to remove gateways if a particular product is in the cart:
add_filter('woocommerce_available_payment_gateways','filter_gateways',1);
function filter_gateways($gateways){
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
$product_ = array(1063);
if(in_array($values['product_id'],$product_)){
unset($gateways['paypal']);
break;
}}
return $gateways;
}
I dont know what your currency switcher is to get any variable from to modify the woocommerce_available_payment_gateways filter though.
This is working code and I have implemented it on [www.edupediapublications.org][1]
add_filter('woocommerce_available_payment_gateways', 'woocs_filter_gateways', 1);
function woocs_filter_gateways($gateway_list)
{
global $WOOCS;
$exclude = array(
'paypal' => array('EUR', 'GBP'), //do not show paypal gate if current currency is EUR or GBP
'stripe' => array('USD')//do not show stripe gate if current currency is USD
);
//***
foreach ($exclude as $gateway_key => $currencies)
{
if (isset($gateway_list[$gateway_key]) AND in_array($WOOCS->current_currency, $currencies))
{
unset($gateway_list[$gateway_key]);
}
}
return $gateway_list;
}
Okay. found a solution. Giving details below, if anyone stumbles here.
The "WooCommerce Currency Switcher" plugin changes the shop currency & stores using session / transient method. Not just that, it also adds a body class for each selected currency.
e.g.: currency-usd or currency-eur or currency-gbp
This is what is possible simply using CSS
.currency-usd .payment_method_instamojo, .currency-usd .payment_method_paynimo, .currency-eur .payment_method_instamojo, .currency-eur .payment_method_paynimo, .currency-gbp .payment_method_instamojo, .currency-gbp .payment_method_paynimo {display:none}
This works as expected w/o writing a separate function. Hope this helps.