0

I have added custom select field in WooCommerce checkout. This is code for field:

add_filter('woocommerce_checkout_fields', 'checkout_select_field_with_optgroup', 10, 1 );
function checkout_select_field_with_optgroup( $fields ) {
    
    $options = [
    //'' => __("Избери квартал…"),
    __("ЗОНА 1") => [
    '' => 'Избери квартал…',
    0 => 'Център Пиротска',
    1 => 'Център НДК',
    2 => 'Център Триадица',
    3 => 'Център Македония',
    4 => 'Черни връх Лозенец',
    5 => 'Лозенец горен',
    6 => 'Лозенец долен',
    7 => 'Лозенец горен изток',
    8 => 'Лозенец долен изток',
    9 => 'Южен Парк',
    10 => 'Зона Б5',
    11 => 'Зона Б18',
    12 => 'Зона Б19',
    13 => 'Банишора',
    14 => 'Оборище',
    15 => 'Оборище до Я. Сакъзов',
    16 => 'Сердика',
    17 => 'Пирогов',
    18 => 'Иван Вазов',
    19 => 'Крива река',
    ],
    __("ЗОНА 2") => [
    20 => 'Света Троица',
    44 => 'Хаджи Димитър',
    45 => 'Сухата река',
    ],
    __("ЗОНА 3")  => [
    104 => 'Драгалевци',
    105 => 'Симеоново',
    ],
    __("ЗОНА 4")  => [
    153 => 'Христо Ботев',
    154 => 'Факултета',
    155 => 'Казичане',
    156 => 'Кривина',
    ],
    ];
    
    $fields['billing']['sofiq_kvartal'] = array(
        'type'     => 'select_og', // <== Type 
        'label'    => __("Квартали в София<span style='color:red; font-weight:bold;'>&nbsp;*</span>"),
        'class'    => array( 'form-row-wide update_totals_on_change select2' ),
        'id'       => 'kvartali-v-sofiq',
        'priority' => 95,
        'required' => false,
        'options'  => $options,
        'clear'    => true,
    );
    
    return $fields;
}

I want to restrict shipping methods based on group selected from this select field.

add_filter( 'woocommerce_package_rates', 'skrivane_na_metodi_za_dostavki_pri_kvartal', 10, 2 );
function skrivane_na_metodi_za_dostavki_pri_kvartal( $rates, $package ) {
    
    // Get Info
   $kvartal = $package['destination']['sofiq_kvartal']


    // flat_rate:6
    $zona_1 = array("0", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19");
        

   
    $zona_1_check = false;

    
    if (in_array($package['destination']['sofiq_kvartal'], $zona_1)) {
        $zona_1_check = true;
    }
    
    // zona 1
    if ( $zona_1_check ) {
        unset( $rates['flat_rate:6']);
    }

    return $rates;
}

Unfortunatelly it's not working. How i can get this field in woocommerce_package_rates same way as this topic: link Or is there any other stable method to do it? I saw All the fields you can get through $package are:

$package['destination']['country']
$package['destination']['state']
$package['destination']['postcode']
$package['destination']['city']
$package['destination']['address']
$package['destination']['address_1']
$package['destination']['address_2']

Is there way to include custom field there? Thanks!

0 Answers0