Cześć,
Ten fragment kodu wylicza koszt dostawy. Aktualnie w systemie mogę dodać do każdej ulicy grupę numerów (shipping_street_locals), dla których mogę dodać dodatkowy koszt dostawy (shipping_additional_cost).
W jaki sposób dodać również sprawdzanie shipping_street_locals2 ---> shipping_additional_cost aby to działało?
Z góry dziękuję za pomoc
function calculate_street_shipping( $rates, $package ) {
$found = false;
$add_cost = 0;
if( isset($_POST['post_data']) ){
$billing_street = 1;
if(isset($_SESSION['zamowien_street_id'])){
$billing_street = $_SESSION['zamowien_street_id'];
}
parse_str($_POST['post_data'], $post);
$shipping_zone = WC_Shipping_Zones::get_zone_matching_package( $package );
$shipping_methods = $shipping_zone->get_shipping_methods( true );
$zone_streets = array();
if( $shipping_methods ){
foreach ( $shipping_methods as $key => $method ) {
$streets = get_option( 'shipping_zone_streets_' . $key, array() );
if( $streets ){
foreach ($streets as $street) {
$locals = get_post_meta($street, 'shipping_street_locals', true);
$locals = ($locals) ? explode(",", $locals) : array();
$locals = array_map('trim',$locals);
$zone_streets[$street] = $locals;
if( $billing_street == $street ){
$cost = get_post_meta($street, 'shipping_street_cost', true);
$add_cost = ( $cost > 0 ) ? $cost : 0;
if(in_array($post['billing_address_1'], $locals) ){
$cost = get_post_meta($street, 'shipping_additional_cost', true);
$add_cost = ( $cost > 0 ) ? $add_cost + $cost : $add_cost;
}
$found = true;
break;
}
}
}
if( $found ){
break;
}
}
}
}
else{
$billing_street = 1;
if(isset($_SESSION['zamowien_street_id'])){
$billing_street = $_SESSION['zamowien_street_id'];
}
$shipping_zone = WC_Shipping_Zones::get_zone_matching_package( $package );
$shipping_methods = $shipping_zone->get_shipping_methods( true );
$zone_streets = array();
if( $shipping_methods ){
foreach ( $shipping_methods as $key => $method ) {
$streets = get_option( 'shipping_zone_streets_' . $key, array() );
if( $streets ){
foreach ($streets as $street) {
$locals = get_post_meta($street, 'shipping_street_locals', true);
$locals = ($locals) ? explode(",", $locals) : array();
$locals = array_map('trim',$locals);
$zone_streets[$street] = $locals;
if( $billing_street == $street ){
$cost = get_post_meta($street, 'shipping_street_cost', true);
$add_cost = ( $cost > 0 ) ? $cost : 0;
if(isset($_SESSION['zamowien_local_number']) && in_array($_SESSION['zamowien_local_number'], $locals) ){
$cost = get_post_meta($street, 'shipping_additional_cost', true);
$add_cost = ( $cost > 0 ) ? $add_cost + $cost : $add_cost;
}
$found = true;
break;
}
}
}
if( $found ){
break;
}
}
}
}
if( $found && $rates ) {
foreach ($rates as $rate_key => $rate) {
if ( $rate->method_id != 'free_shipping' ) {
$rates[$rate_key]->cost = $rates[$rate_key]->cost + $add_cost;
}
}
}
return $rates;
}