Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ultimate Guide to WooCommerce Shipping Zones | http://www.ibenic.com/ultimate-guide-woocommerce-shipping-zones
<?php
function ibenic_get_all_zones() {
if( class_exists( 'WC_Shipping_Zones' ) ) {
$all_zones = WC_Shipping_Zones::get_zones();
return $all_zones;
}
return false;
}
<?php
Array
(
// 52 - Zone ID in database
[52] => Array
(
[zone_id] => 52
[zone_name] => TISAK Croatia
[zone_order] => 0
[zone_locations] => Array
(
[0] => stdClass Object
(
[code] => HR
[type] => country
)
)
[meta_data] => Array
(
)
[formatted_zone_location] => Croatia
[shipping_methods] => Array
(
// 18 -> instance_id
[18] => WOODEV_TISAK_Shipping Object
(
[wc_version] => 2.6.1
[supports] => Array
(
[0] => shipping-zones
[1] => instance-settings
)
[id] => woodev_tisak_shipping
[method_title] => TISAK Shipping
// ... Other Shipping Method definitions
)
)
)
)
<?php
WC_Shipping_Zones::delete_zone( 52 );
<?php
$available_zones = WC_Shipping_Zones::get_zones();
// Get all WC Countries
$all_countries = WC()->countries->get_countries();
//Array to store available names
$available_zones_names = array();
// Add each existing zone name into our array
foreach ($available_zones as $zone ) {
if( !in_array( $zone['zone_name'], $available_zones_names ) ) {
$available_zones_names[] = $zone['zone_name'];
}
}
// Check if our zone 'TISAK Croatia' is already there
if( ! in_array( 'TISAK Croatia', $available_zones_names ) ){
// Instantiate a new shipping zone with our object
$new_zone_cro = new WC_Shipping_Zone();
$new_zone_cro->set_zone_name( 'TISAK Croatia');
// Add Croatia as location
$new_zone_cro->add_location( 'HR', 'country' );
// Save the zone, if non existent it will create a new zone
$new_zone_cro->save();
// Add our shipping method to that zone
$new_zone_cro->add_shipping_method( 'woodev_tisak_shipping' );
}
<?php
// Getting the WOODEV_TISAK_Shipping object
$shipping_method = WC_Shipping_Zones::get_shipping_method( 18 );
<?php
function ibenic_get_zone( $zone_id = 0 ) {
if( class_exists( 'WC_Shipping_Zones' ) ) {
$the_zone = WC_Shipping_Zones::get_zone( $zone_id );
return $the_zone;
}
return false;
}
<?php
$zone = WC_Shipping_Zones::get_zone_by( 'zone_id', 52 );
// $zone = TISAK Croatia
$zone2 = WC_Shipping_Zones::get_zone_by( 'instance_id', 18 );
// $zone2 = TISAK Croatia
<?php
$zone = WC_Shipping_Zones::get_zone_matching_package( $package );
<?php
$new_zone_object = new WC_Shipping_Zone();
$new_zone_object->set_zone_name( 'My New Zone' );
<?php
// Adding United States
$new_zone_object->add_location( 'US', 'country' );
// Adding California, United States
$new_zone_object->add_location( 'US:CA', 'state' );
// Adding Africa
$new_zone_object->add_location( 'AF', 'continent' );
// Adding Postcode
$new_zone_object->add_location( '51000', 'postcode' );
<?php
$new_zone_object->add_shipping_method( 'woodev_tisak_shipping' );
<?php
// Clear only states
$new_zone_object->clear_locations( array( 'state' ) );
// Clear only state and postcodes
$new_zone_object->clear_locations( array( 'state', 'postcode' ) );
<?php
$new_zone_object->delete();
<?php
$new_zone_object->save();
<?php
$locations_to_set = array(
// Setting a Country (United States)
array(
'code' => 'US',
'type' => 'country'
),
// Setting a postcode
array(
'code' => '51000',
'type' => 'postcode'
),
// Setting a state (United States:California)
array(
'code' => 'US:CA',
'type' => 'state'
),
// Setting a continent (Africa)
array(
'code' => 'AF',
'type' => 'continent'
)
);
$new_zone_object->set_locations( $locations_to_set );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.