Unable to fetch customer (guest user) address to calculate travel cost in Woocommerce

Question

For Guest User I am adding a travel cost for a product so I am adding a filter callback as follows

add_filter( 'woocommerce_distance_rate_shipping_calculated_distance', 'woocommerce_distance_rate_shipping_calculated_distance_callback', 10, 3 );

for which I need to fetch address which I am doing in the above callback which is as follows

$address = WC()->session->get( ‘customer’ );

        $address['shipping_postcode']  = $address['postcode'];

        $address['shipping_city']      = $address['city'];
        
        $address['shipping_country']   = $address['country'];

but I dont get travelling costs even after I use this method to get address

function nv_get_formatted_shipping_name_and_address($user_id) {

     
     
    $address = array();
    //$address .= get_user_meta( $user_id, 'shipping_first_name', true );
    //$address .= ' ';
    //$address .= get_user_meta( $user_id, 'shipping_last_name', true );
    //$address .= "n";
    //$address .= get_user_meta( $user_id, 'shipping_company', true );
    //$address .= "n";
    
     if($user_id > 0) 
     {

    $address['shipping_postcode']  = get_user_meta( $user_id, 'shipping_postcode', true );

    $address['shipping_state']     = get_user_meta( $user_id, 'shipping_state', true );

    $address['shipping_country']   = get_user_meta( $user_id, 'shipping_country', true );

    $address['shipping_address_1'] = get_user_meta( $user_id, 'shipping_address_1', true );

    $address['shipping_address_2'] = get_user_meta( $user_id, 'shipping_address_2', true );

    $address['shipping_city']      = get_user_meta( $user_id, 'shipping_city', true );

    }
    else 
    {

        $address = WC()->session->get( 'customer' );

        $address['shipping_postcode']  = $address['postcode'];

        $address['shipping_city']      = $address['city'];
        
        $address['shipping_country']   = $address['country'];
    }
    
    $customeraddress               = array_filter($address);

    $customeraddress               = implode(',',$customeraddress);

    return $customeraddress;
}

function woocommerce_distance_rate_shipping_calculated_distance_callback($nvoriginaldistance, $distance_value, $unit)
{
    global $woocommerce;

    $items = $woocommerce->cart->get_cart();
    
    




    $current_user = wp_get_current_user();

      $distance = new WC_Shipping_Distance_Rate();
      $distance->init_form_fields();
      $distance->init_settings();

      $api_key = $distance->get_option( 'api_key' );
      $mode = $distance->get_option( 'mode' );
      $avoid = $distance->get_option( 'avoid' );
      $unit = $distance->get_option( 'unit' );
      $show_distance = ( $distance->get_option( 'show_distance' ) == 'yes' ) ? true : false;
      $debug = $distance->get_option( 'debug' );

      $customeraddress = nv_get_formatted_shipping_name_and_address($current_user->ID);

      $googleapi = new WC_Google_Distance_Matrix_API( $api_key, $debug );

      foreach($items as $item => $values) {

                  $vendor_address = array();
                  $vendor_distance_rate = 1;

                  $_product =  wc_get_product( $values['data']->get_id());

                  $vendor_id = get_post_field( 'post_author', $values['product_id'] );

                  $vendor_address_full = get_user_meta($vendor_id,'dokan_profile_settings',true);
                  //var_dump($vendor_address_full); exit;
                  $vendor_address['zip']      = $vendor_address_full['address']['zip'];
                  $vendor_address['state']    = $vendor_address_full['address']['state'];
                  $vendor_address['country']  = $vendor_address_full['address']['country'];
                  $vendor_address['street_1'] = $vendor_address_full['address']['street_1'];
                  $vendor_address['street_2'] = $vendor_address_full['address']['street_2'];
                  $vendor_address['city']     = $vendor_address_full['address']['city'];
                  $vendor_distance_rate       = $vendor_address_full['seller_distance_rate'];

                  $vendor_address             = array_filter($vendor_address);
                  $vendor_address             = implode(',',$vendor_address);

                  $nvdistance = $googleapi->get_distance( $vendor_address, $customeraddress, false, $mode, $avoid, $unit );

                    // Check if a valid response was received.
                    if ( ! isset( $nvdistance->rows[0] ) || 'OK' !== $nvdistance->rows[0]->elements[0]->status ) {

                        return;

                    }


                  $distance_text = ' (' . $nvdistance->rows[0]->elements[0]->distance->text . ' * '.get_woocommerce_currency_symbol().$vendor_distance_rate.') ';

                    $travel_time_minutes = round( $nvdistance->rows[0]->elements[0]->duration->value / 60 );
                    $rounding_precision = apply_filters( 'woocommerce_distance_rate_shipping_distance_rounding_precision', 0 );
                    $distance_value = $nvdistance->rows[0]->elements[0]->distance->value;

                    if ( 'imperial' == $unit ) {
                        $distance = round( $nvdistance->rows[0]->elements[0]->distance->value * 0.000621371192, $rounding_precision );
                    } else {
                        $distance = round( $nvdistance->rows[0]->elements[0]->distance->value / 1000, $rounding_precision );
                    }


              $vendor_distance_rate = $vendor_distance_rate * $distance;


              //$woocommerce->cart->add_fee( $_product->get_title().' '.$distance_text.__('Distance Fee - '. $vendor_id.'-'.$vendor_address.'-D->'.$nvoriginaldistance.' </br> -- CA -- </br> '.$customeraddress, 'woocommerce'), $vendor_distance_rate );

               $woocommerce->cart->add_fee( $_product->get_title().' '.$distance_text.__('Distance Fee', 'woocommerce'), $vendor_distance_rate );
      }


      return $nvoriginaldistance = 0;
}
0
harshal 4 weeks 0 Answers 3 views 0

Leave an answer