2017-12-14 11:32:53 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
|
|
|
|
2018-07-19 12:16:30 +03:00
|
|
|
function get_wc_shipping_methods_by_zones($enhanced = false) {
|
2017-12-14 11:32:53 +02:00
|
|
|
$result = array();
|
|
|
|
|
2018-07-19 12:16:30 +03:00
|
|
|
$shippingZones = WC_Shipping_Zones::get_zones();
|
|
|
|
$defaultZone = WC_Shipping_Zones::get_zone_by();
|
|
|
|
|
|
|
|
$shippingZones[$defaultZone->get_id()] = array(
|
|
|
|
$defaultZone->get_data(),
|
|
|
|
'zone_id' => $defaultZone->get_id(),
|
|
|
|
'formatted_zone_location' => $defaultZone->get_formatted_location(),
|
|
|
|
'shipping_methods' => $defaultZone->get_shipping_methods(false)
|
|
|
|
);
|
2017-12-14 11:32:53 +02:00
|
|
|
|
2018-06-19 10:03:46 +03:00
|
|
|
if ($shippingZones) {
|
|
|
|
foreach ($shippingZones as $code => $shippingZone) {
|
|
|
|
foreach ($shippingZone['shipping_methods'] as $key => $shipping_method) {
|
|
|
|
$shipping_methods = array(
|
|
|
|
'id' => $shipping_method->id,
|
|
|
|
'instance_id' => $shipping_method->instance_id,
|
2017-12-14 11:32:53 +02:00
|
|
|
'title' => $shipping_method->title
|
|
|
|
);
|
|
|
|
|
2018-06-19 10:03:46 +03:00
|
|
|
if ($enhanced) {
|
|
|
|
$shipping_code = $shipping_method->id;
|
|
|
|
} else {
|
|
|
|
$shipping_code = $shipping_method->id . ':' . $shipping_method->instance_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($result[$shipping_code])) {
|
|
|
|
$result[$shipping_code] = array(
|
|
|
|
'name' => $shipping_method->method_title,
|
|
|
|
'enabled' => $shipping_method->enabled,
|
|
|
|
'description' => $shipping_method->method_description,
|
|
|
|
'title' => $shipping_method->title
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($enhanced) {
|
|
|
|
$result[$shipping_method->id]['shipping_methods'][$shipping_method->id . ':' . $shipping_method->instance_id] = $shipping_methods;
|
|
|
|
unset($shipping_methods);
|
|
|
|
}
|
2017-12-14 11:32:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 12:16:30 +03:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_wc_shipping_methods() {
|
|
|
|
$wc_shipping = WC_Shipping::instance();
|
|
|
|
$shipping_methods = $wc_shipping->get_shipping_methods();
|
|
|
|
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
foreach ($shipping_methods as $code => $shipping) {
|
|
|
|
$result[$code] = array(
|
|
|
|
'name' => $shipping->method_title,
|
|
|
|
'enabled' => $shipping->enabled,
|
|
|
|
'description' => $shipping->method_description,
|
|
|
|
'title' => $shipping->title ? $shipping->title : $shipping->method_title
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-19 10:03:46 +03:00
|
|
|
return apply_filters('retailcrm_shipping_list', $result);
|
2017-12-14 11:32:53 +02:00
|
|
|
}
|
2018-07-19 12:16:30 +03:00
|
|
|
|
|
|
|
function retailcrm_get_delivery_service($method_id, $instance_id) {
|
|
|
|
$shippings_by_zone = get_wc_shipping_methods_by_zones(true);
|
|
|
|
$method = explode(':', $method_id);
|
|
|
|
$method_id = $method[0];
|
|
|
|
$shipping = isset($shippings_by_zone[$method_id]) ? $shippings_by_zone[$method_id] : array();
|
|
|
|
|
|
|
|
if ($shipping && isset($shipping['shipping_methods'][$method_id . ':' . $instance_id])) {
|
|
|
|
return $shipping['shipping_methods'][$method_id . ':' . $instance_id];
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|