HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux wordpress-ubuntu-s-2vcpu-4gb-fra1-01 5.4.0-169-generic #187-Ubuntu SMP Thu Nov 23 14:52:28 UTC 2023 x86_64
User: root (0)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/dinamo-shop/wp-content/themes/dinamo-shop/inc/woocommerce/shipping.php
<?php
/*
 * File containing custom WooCommerce shipping functions
 */


// Create shipping zones
add_action( 'init', function(){

  $available_zones = WC_Shipping_Zones::get_zones();
  $all_countries  = WC()->countries->get_countries();
  $available_zone_names = array();
  foreach ($available_zones as $zone ) {
  	if( !in_array( $zone['zone_name'], $available_zone_names ) ) {
  		$available_zone_names[] = $zone['zone_name'];
  	}
  }

  // Create "Hrvatska" shipping zone
  if( !in_array( 'Hrvatska', $available_zone_names ) ){
    $zone_hr = new WC_Shipping_Zone();
    $zone_hr->set_zone_name( 'Hrvatska' );
    $zone_hr->set_zone_order( 1 );
    $zone_hr->add_location( 'HR', 'country');
    $zone_hr->save();
  }

  // Create "Slovenija" shipping zone
  if( !in_array( 'Slovenija', $available_zone_names ) ){
    $zone_si = new WC_Shipping_Zone();
    $zone_si->set_zone_name( 'Slovenija' );
    $zone_si->set_zone_order( 2 );
    $zone_si->add_location( 'SI', 'country' );
    $zone_si->save();
  }

  // Create "Bosna i Hercegovina, Srbija, Crna Gora" shipping zone
  if( !in_array( 'Bosna i Hercegovina, Srbija, Crna Gora', $available_zone_names ) ){
    $zone_bsm = new WC_Shipping_Zone();
    $zone_bsm->set_zone_name( 'Bosna i Hercegovina, Srbija, Crna Gora' );
    $zone_bsm->set_zone_order( 3 );
    $zone_bsm_locations = array( 'BA', 'RS', 'ME' );
    foreach( $zone_bsm_locations as $location ){
      $zone_bsm->add_location( $location, 'country' );
    }
    $zone_bsm->save();
  }

  // Create "Europska unija" shipping zone
  if( !in_array( 'Europska unija', $available_zone_names ) ){
    $zone_eu = new WC_Shipping_Zone();
    $zone_eu->set_zone_name( 'Europska unija' );
    $zone_eu->set_zone_order( 4 );
    $zone_eu_locations = array( 'AT', 'BE', 'BG', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK', 'ES', 'SE' );
    foreach( $zone_eu_locations as $location ){
      $zone_eu->add_location( $location, 'country' );
    }
    $zone_eu->save();
  }


});

// Hide fixed delivery price if free delivery available
add_filter( 'woocommerce_package_rates', function( $rates ){
  $free = array();

  foreach( $rates as $rate_id => $rate ){
    if( 'free_shipping' === $rate->method_id ){
      $free[ $rate_id ] = $rate;
      break;
    }
  }

  return !empty( $free ) ? $free : $rates;
}, 10, 2 );


function dinamo_get_shipping_cost_by_country( $country_code, $quantity ) {
  $shipping_cost = 0;
  if ( class_exists( 'WC_Shipping_Zones' ) ) {
    $all_zones = WC_Shipping_Zones::get_zones();
    if ( ! empty( $all_zones ) ) {
      foreach ( $all_zones as $zone ) {
        if ( ! empty( $zone['zone_locations'] ) ) {
          foreach ( $zone['zone_locations'] as $code ) {
            if ( $country_code === $code->code ) {
              if ( ! empty( $zone['shipping_methods'] ) ) {
                foreach ( $zone['shipping_methods'] as $flat_rate ) {
                  $shipping_cost = str_replace( ' * [qty]', '', $flat_rate->cost );
                  if ( strpos( $flat_rate->cost, '[qty]' ) !== false ) {
                    $shipping_cost = ( $shipping_cost * $quantity );
                  }
                  break;
                }
              }
            }
          }
        }
      }
    }
  }
  return $shipping_cost;
}


// add_filter( 'woocommerce_package_rates', function(){
//   $treshold = 1000;
//
//   $ba_rs_me = 5;
//   $eu       = 4;
//   $other    = 0;
//
//   $shipping_zone_id = WC_Shipping_Zones::get_zone_matching_package( $package );
// });