1

I am implementing my own shipping API integration in magento2. I am able to get all the details finely. But all of sudden it is giving an error as follows.

Exception #0 (Exception): Notice: Undefined property: stdClass::$rate_breakup in /var/www/html/b2b/app/code/My/Shipping/Controller/Adminhtml/Order/Sendform.php on line 68

My Sendform.php is

class Sendform extends \Magento\Backend\App\Action
{
public function execute(){
    $data = $this->getRequest()->getPostValue();

    $result = $this->_objectManager->create('Shipyaari\Shipping\Model\SearchService')->avilable_service($data);
             $result=json_decode($result);


            if($result) 
            {
                $status__=0;



                     if($result->success==1)
                     {
                         $status__=1;
                     }


                if($status__==1)
                {

                  $html= '<div style="border:1px solid #e3e3e3;border-radius:5px;padding:20px;">
                        <div>
                            <h3>List Of Available Services</h3>
                            <hr>
                        </div>
                        <div>
                            <table class="table" style="border-spacing:0px;width:100%;">
                                <thead>
                                    <tr>
                                      <th><center><strong>Sr. NO.</strong></center></th>
                                      <th>Partner Name</th>
                                      <th><center><strong>Image</strong></center></th>
                                      <th><center><strong>Expected Pickup Date</strong></center></th>
                                      <th><center><strong>Service Type</strong></center></th>
                                      <th><center><strong>Grand Total</strong></center></th>
                                      <th><center><strong>Base Price</strong></center></th>
                                      <th><center><strong>Fuel Surcharge</strong></center></th>
                                      <th><center><strong>GST</strong></center></th>
                                      <th><center><strong>Action</strong></center></th>
                                    </tr>
                                    <tr><td colspan="10"><hr/></td></tr>
                                 </thead>
                                 <tbody>'; 

                                 $i=0;
                                 //foreach($result as $value)

                                    //$i++;

                                        foreach($result->couriers as $courier){
                                            $i++;

                                            if($result->success==1)
                                        {
                                            print_r($result);
                                            $html .= '<tr class="success">
                                            <th><center><strong>'.$i.'</strong></center></th>
                                            <td><center><strong>'.$courier->name.'</strong></center></td>
                                            <td><center><img width="100" height="100" src="'.$courier->logo_url.'"/></center></td>
                                            <td><center>'.$courier->exp_pickup_date.'</center></td>';
                                            foreach($courier->service_types as $service_types){
                                                $service_type=$service_types->service_name;

                                            $goods_services_tax=$service_types->rate_breakup->goods_services_tax;
                                            $base_rate=$service_types->rate_breakup->base_rate;

                                             $html .= '</tr><td></td><td></td><td></td><td></td><td><center>'.$service_types->service_name.'</center></td>
                                            <td><center>'.$service_types->rate_breakup->total_charge.'</center></td>
                                            <td><center>'.$base_rate.'</center></td>
                                            <td><center>'.$service_types->rate_breakup->fuel_surcharge.'</center></td>
                                            <th><center><strong>'.$goods_services_tax.'</strong></center></th>
                                            <td><center>';
                                             $html .="<button class='submit' id='".$courier->courier_id."' onclick='send_to_zepo(\"$courier->courier_id\", \"$service_types->service_id\",\"$service_type\");'>Select</button></center></td>";

                                        }
                                        $html .= '
                                        <tr><td colspan="8"><hr style="background-color: #e3e3e3; height: 1px; border: 0;"/></td></tr>';
                                    }
                                        if($result->success!=1)
                                    {
                                         $html .= '
                                        <tr class="danger">
                                            <th><center>'.$i.'</center></th>
                                            <td><center><strong>'.$courier->name.'</center></strong></td>
                                            <td><center>-</center></td>
                                            <td><center>-</center></td>
                                            <td><center>-</center></td>
                                            <td><center>-</center></td>
                                            <td><center><strong></center></strong></td>
                                            <td><center><button class="submit" id="disabled" style="cursor: not-allowed !important;">Select</button></center></td>
                                        </tr>
                                        <tr><td colspan="8"><hr style="background-color: #e3e3e3; height: 1px; border: 0;"/></td></tr>';
                                    }


                                    }



                            $html .= '</table>
                                        </div>
                                   </div>
                                  </div>
                                    <div style="margin-top:30px;float:right">
                                        <button id="back" class="back" style="float:right;">Back</button>
                                    </div>
                                <div>

                                <script type="text/javascript">
                                    require([\'jquery\'], function($) {
                                        $(document).ready(function($) {
                                            $(".back").click(function(){
                                                $("#search_service_response_container").show();
                                                $("#form-container").show();
                                            });
                                        });
                                    });
                                </script>';
                                echo $html; die();
                }
}
}

My json response from API is as follows.

(
    [success] => 1
    [code] => 200
    [user_id] => 1585
    [display_weight] => 0.32
    [display_services] => Prepaid
    [security_deposit] => 2000
    [pickup_details] => stdClass Object
        (
            [pincode] => 500034
            [city] => Hyderabad
            [state] => Telangana
            [country] => India
        )

    [delivery_details] => stdClass Object
        (
            [pincode] => 500049
            [city] => Hyderabad
            [state] => Telangana
            [country] => India
        )

    [couriers] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Fedex
                    [courier_id] => 1
                    [logo_url] => https://s3-ap-southeast-1.amazonaws.com/cdntest.zepo.in/ving_images/fedex.png
                    [exp_pickup_date] => 05/04/2019
                    [expected_pickup_date] => 1554467400000
                    [cut_off_Time] => 18:00:00
                    [pickup_success_rate] => 93
                    [service_types] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [service_id] => 1
                                    [service_name] => PRIORITY_OVERNIGHT
                                    [display_name] => Priority Air
                                    [volumetric_weight] => 0.27
                                    [form_required] => 0
                                    [expected_delivery_days] => 3
                                    [min_delivery_days] => 1
                                    [max_delivery_days] => 3
                                    [rate_breakup] => stdClass Object
                                        (
                                            [base_rate] => 45.65
                                            [fuel_surcharge] => 9.13
                                            [service_tax] => 0
                                            [goods_services_tax] => 9.86
                                            [other_charges] => 0
                                            [insurance_charges] => 0
                                            [COD_charges] => 0
                                            [sub_total] => 54.78
                                            [service_charge] => 0
                                            [total_charge] => 65
                                            [courier_goods_services_tax] => 8.46
                                            [courier_other_charges] => 0
                                            [courier_insurance_charges] => 0
                                            [courier_base_rate] => 39.2
                                            [courier_fuel_surcharge] => 7.8
                                            [courier_service_tax] => 0
                                            [courier_COD_charges] => 0
                                            [courier_sub_total] => 47
                                            [courier_service_charge] => 0
                                            [courier_total_charge] => 55
                                            [advancement_fees] => 0
                                            [entry_or_octroi_tax] => 0
                                        )

                                )

                            [1] => stdClass Object
                                (
                                    [service_id] => 3
                                    [service_name] => FEDEX_EXPRESS_SAVER
                                    [display_name] => By Surface
                                    [volumetric_weight] => 0.3
                                    [form_required] => 0
                                    [expected_delivery_days] => 10
                                    [min_delivery_days] => 3
                                    [max_delivery_days] => 7
                                    [rate_breakup] => stdClass Object
                                        (
                                            [base_rate] => 83.64
                                            [fuel_surcharge] => 8.36
                                            [service_tax] => 0
                                            [goods_services_tax] => 16.56
                                            [other_charges] => 0
                                            [insurance_charges] => 0
                                            [COD_charges] => 0
                                            [sub_total] => 92
                                            [service_charge] => 0
                                            [total_charge] => 109
                                            [courier_goods_services_tax] => 13.64
                                            [courier_other_charges] => 0
                                            [courier_insurance_charges] => 0
                                            [courier_base_rate] => 63.2
                                            [courier_fuel_surcharge] => 12.6
                                            [courier_service_tax] => 0
                                            [courier_COD_charges] => 0
                                            [courier_sub_total] => 75.8
                                            [courier_service_charge] => 0
                                            [courier_total_charge] => 89
                                            [advancement_fees] => 0
                                            [entry_or_octroi_tax] => 0
                                        )

                                )

                        )

                    [codlimit] => 20000
                    [cod_limit] => 20000
                )

            [1] => stdClass Object
                (
                    [name] => Aramex
                    [courier_id] => 2
                    [logo_url] => https://s3-ap-southeast-1.amazonaws.com/cdntest.zepo.in/ving_images/aramex.png

                    [exp_pickup_date] => 05/04/2019
                    [expected_pickup_date] => 1554459300000
                    [cut_off_Time] => 15:00
                    [pickup_success_rate] => 80
                    [service_types] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [service_id] => 4
                                    [service_name] => STANDARD
                                    [display_name] => By Air
                                    [volumetric_weight] => 0.27
                                    [form_required] => 0
                                    [expected_delivery_days] => 5
                                    [min_delivery_days] => 1
                                    [max_delivery_days] => 3
                                    [rate_breakup] => stdClass Object
                                        (
                                            [base_rate] => 28.56
                                            [fuel_surcharge] => 10.57
                                            [service_tax] => 0
                                            [goods_services_tax] => 7.04
                                            [other_charges] => 0
                                            [insurance_charges] => 0
                                            [COD_charges] => 0
                                            [sub_total] => 39.13
                                            [service_charge] => 0
                                            [total_charge] => 46
                                            [courier_goods_services_tax] => 4.44
                                            [courier_other_charges] => 0
                                            [courier_insurance_charges] => 0
                                            [courier_base_rate] => 18
                                            [courier_fuel_surcharge] => 6.66
                                            [courier_service_tax] => 0
                                            [courier_COD_charges] => 0
                                            [courier_sub_total] => 24.66
                                            [courier_service_charge] => 0
                                            [courier_total_charge] => 29
                                            [advancement_fees] => 0
                                            [entry_or_octroi_tax] => 0
                                        )

                                )

                        )

                    [codlimit] => 20000
                    [cod_limit] => 20000
                )

Can any body tell me what is wrong in this. When i print or echo rate_breakup->fuel_charge it is printing normally like 9.88

  • what did you try so far? – magefms Apr 5 at 5:51
  • I tried $service_types->rate_breakup->goods_services_tax; $service_types['rate_breakup']goods_services_tax; foreach($service_types->rate_breakup as $rate_breakup){ echo $rate_breakup->total_charge; } But none of them worked. – Dinesh Apr 5 at 6:05
  • did this one work? $service_types['rate_breakup']goods_services_tax; or try this one `$service_types['rate_breakup']->goods_services_tax; – magefms Apr 5 at 6:15
  • I tried both none of them worked . Uncaught Error: Cannot use object of type stdClass as array in – Dinesh Apr 5 at 8:11
  • 1
    When i am placing exit then its printing the correct result. With out exit it is giving an error. like follow echo $service_types->rate_breakup->goods_services_tax; exit; its printing the value finely. – Dinesh Apr 5 at 9:12
0

Try this one :

class Sendform extends \Magento\Backend\App\Action
{
    public function execute(){
        $data = $this->getRequest()->getPostValue();

        $result = $this->_objectManager->create('Shipyaari\Shipping\Model\SearchService')->avilable_service($data);
        $result=json_decode($result);


        if($result)
        {
            $status__=0;



            if($result->success==1)
            {
                $status__=1;
            }


            if($status__==1)
            {

                $html= '<div style="border:1px solid #e3e3e3;border-radius:5px;padding:20px;">
                        <div>
                            <h3>List Of Available Services</h3>
                            <hr>
                        </div>
                        <div>
                            <table class="table" style="border-spacing:0px;width:100%;">
                                <thead>
                                    <tr>
                                      <th><center><strong>Sr. NO.</strong></center></th>
                                      <th>Partner Name</th>
                                      <th><center><strong>Image</strong></center></th>
                                      <th><center><strong>Expected Pickup Date</strong></center></th>
                                      <th><center><strong>Service Type</strong></center></th>
                                      <th><center><strong>Grand Total</strong></center></th>
                                      <th><center><strong>Base Price</strong></center></th>
                                      <th><center><strong>Fuel Surcharge</strong></center></th>
                                      <th><center><strong>GST</strong></center></th>
                                      <th><center><strong>Action</strong></center></th>
                                    </tr>
                                    <tr><td colspan="10"><hr/></td></tr>
                                 </thead>
                                 <tbody>';

                $i=0;
                //foreach($result as $value)

                //$i++;

                foreach($result->couriers as $courier){
                    $i++;

                    if($result->success===1)
                    {
                        print_r($result);
                        $html .= '<tr class="success">
                                            <th><center><strong>'.$i.'</strong></center></th>
                                            <td><center><strong>'.$courier->name.'</strong></center></td>
                                            <td><center><img width="100" height="100" src="'.$courier->logo_url.'"/></center></td>
                                            <td><center>'.$courier->exp_pickup_date.'</center></td>';
                        foreach($courier->service_types as $serviceTypes){

                            $service_type= $serviceTypes->service_name;

                            $goods_services_tax= $serviceTypes->rate_breakup->goods_services_tax;

                            $base_rate=$serviceTypes->rate_breakup->base_rate;

                            $html .= '</tr><td></td><td></td><td></td><td></td><td><center>'.$serviceTypes->service_name.'</center></td>
                                            <td><center>'.$serviceTypes->rate_breakup->total_charge.'</center></td>
                                            <td><center>'.$base_rate.'</center></td>
                                            <td><center>'.$serviceTypes->rate_breakup->fuel_surcharge.'</center></td>
                                            <th><center><strong>'.$goods_services_tax.'</strong></center></th>
                                            <td><center>';
                            $html .="<button class='submit' id='".$courier->courier_id."' onclick='send_to_zepo(\"$courier->courier_id\", \"$serviceTypes->service_id\",\"$service_type\");'>Select</button></center></td>";

                        }
                        $html .= '
                                        <tr><td colspan="8"><hr style="background-color: #e3e3e3; height: 1px; border: 0;"/></td></tr>';
                    }
                    if($result->success!=1)
                    {
                        $html .= '
                                        <tr class="danger">
                                            <th><center>'.$i.'</center></th>
                                            <td><center><strong>'.$courier->name.'</center></strong></td>
                                            <td><center>-</center></td>
                                            <td><center>-</center></td>
                                            <td><center>-</center></td>
                                            <td><center>-</center></td>
                                            <td><center><strong></center></strong></td>
                                            <td><center><button class="submit" id="disabled" style="cursor: not-allowed !important;">Select</button></center></td>
                                        </tr>
                                        <tr><td colspan="8"><hr style="background-color: #e3e3e3; height: 1px; border: 0;"/></td></tr>';
                    }


                }



                $html .= '</table>
                                        </div>
                                   </div>
                                  </div>
                                    <div style="margin-top:30px;float:right">
                                        <button id="back" class="back" style="float:right;">Back</button>
                                    </div>
                                <div>

                                <script type="text/javascript">
                                    require([\'jquery\'], function($) {
                                        $(document).ready(function($) {
                                            $(".back").click(function(){
                                                $("#search_service_response_container").show();
                                                $("#form-container").show();
                                            });
                                        });
                                    });
                                </script>';
                echo $html; die();
            }
        }
    }
0

I found the issue. Issue is with API it self . some times i am getting rate_breakup and some times i am getting nothing. so i placed below if condition.

  if(isset($service_types->rate_breakup)){
                                              $html .= '
                                            <td><center>'.$service_types->rate_breakup->total_charge.'</center></td>
                                            <td><center>'.$service_types->rate_breakup->base_rate.'</center></td>
                                            <td><center>'.$service_types->rate_breakup->fuel_surcharge.'</center></td>
                                            <th><center><strong>'.$service_types->rate_breakup->goods_services_tax.'</strong></center></th>
                                            <td><center>';
                                              $html .="<button class='submit' id='".$courier->courier_id."' onclick='send_to_zepo(\"$courier->courier_id\", \"$service_types->service_id\",\"$service_type\");'>Select</button></center></td>";
                                        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.