Created
February 26, 2021 11:47
-
-
Save luiseduardobraschi/9ff2d95930ad3333456f02cd6442cdef to your computer and use it in GitHub Desktop.
[Dokan] Add support to multiple postcode ranges.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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