Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Dokan] Add support to multiple postcode ranges.
<?php
use WeDevs\DokanPro\Shipping\ShippingZone;
add_filter( 'dokan_vendor_shipping_is_available', function( $is_available, $package, $shipping ){
$seller_id = $package['seller_id'];
$destination_postcode = isset( $package['destination']['postcode'] ) ? $package['destination']['postcode'] : '';
$destination_postcode = wc_normalize_postcode( $destination_postcode );
if ( empty( $seller_id ) ) {
return false;
}
$zone = ShippingZone::get_zone_matching_package( $package );
$locations = ShippingZone::get_locations( $zone->get_id(), $seller_id );
foreach ( $locations as $location ) {
if( 'postcode' === $location['type'] ){
$code = $location['code'];
if ( strstr( $code, '...' ) ) {
$range = array_map( 'trim', explode( '...', $code ) );
if ( 2 !== count( $range ) ) {
return $is_available;
}
list( $min, $max ) = $range;
// If the postcode is non-numeric, make it numeric.
if ( ! is_numeric( $min ) || ! is_numeric( $max ) ) {
$min = str_pad( wc_normalize_postcode( $min ), strlen( $destination_postcode ), '0' );
$max = str_pad( wc_normalize_postcode( $max ), strlen( $destination_postcode ), '0' );
}
if ( $destination_postcode >= $min && $destination_postcode <= $max ) {
return true;
}
}
}
}
return $is_available;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment