Skip to content

Call to an undefined method MagentoQuoteModelQuoteAddressRate::setCost()

<?php
namespace RavedigitalCustomModel;

use MagentoShippingModelRateResult;

/**
 * Class Checker
 *
 * @category Ravedigital
 * @package  Ravedigital_Custom
 */
class Carrier extends MagentoFedexModelCarrier
{
    /**
     * Parse calculated rates
     *
     * @param string $response
     * @return Result
     * @link http://www.usps.com/webtools/htm/Rate-Calculators-v2-3.htm
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     * phpcs:disable Generic.Metrics.NestingLevel.TooHigh
     */
    protected function _prepareRateResponse($response)
    {
        $costArr = [];
        $priceArr = [];
        $errorTitle = 'For some reason we can't 
        retrieve tracking info right now.';

        if (is_object($response)) {
            if ($response->HighestSeverity == 'FAILURE' || $response->HighestSeverity == 'ERROR') {
                if (is_array($response->Notifications)) {
                    $notification = array_pop($response->Notifications);
                    $errorTitle = (string)$notification->Message;
                } else {
                    $errorTitle = (string)$response->Notifications->Message;
                }
            } elseif (isset($response->RateReplyDetails)) {
                $allowedMethods = explode(",", $this->
                getConfigData('allowed_methods'));

                if (is_array($response->RateReplyDetails)) {
                    foreach ($response->RateReplyDetails as $rate) {
                        if(property_exists($rate, "ServiceType")) {
                            $serviceName = (string)$rate->ServiceType;
                            if (in_array($serviceName, $allowedMethods)) {
                                $amount = (string) $this->
                                _getRateAmountOriginBased($rate);
                                $costArr[$serviceName] = $amount;
                                $priceArr[$serviceName] = $this->getMethodPrice($amount, $serviceName);
                            }
                        }
                    }
                    asort($priceArr);
                } else {
                    $rate = $response->RateReplyDetails;
                    $serviceName = (string)$rate->ServiceType;
                    if (in_array($serviceName, $allowedMethods)) {
                        $amount = (string)$this->_getRateAmountOriginBased($rate);
                        $costArr[$serviceName] = $amount;
                        $priceArr[$serviceName] =  $this->
                        getMethodPrice($amount, $serviceName);
                    }
                }
            }
        }
         /** @var Result $result */
        $result = $this->_rateFactory->create();
        if (empty($priceArr)) {
            /** @var MagentoQuoteModelQuoteAddressRate $error */
            $error = $this->_rateErrorFactory->create();
            $error->setCarrier($this->_code);
            $error->setCarrierTitle($this->getConfigData('title'));
            $error->setErrorMessage($errorTitle);
            $error->setErrorMessage($this->getConfigData('specificerrmsg'));
            $result->append($error);
        } else {
            foreach ($priceArr as $method => $price) {
                 /** @var MagentoQuoteModelQuoteAddressRate $rate */
                $rate = $this->_rateMethodFactory->create();
                $rate->setCarrier($this->_code);
                $rate->setCarrierTitle($this->getConfigData('title'));
                $rate->setMethod($method);
                $rate->setMethodTitle((string)$this->getCode('method', $method));
                $rate->setCost($costArr[$method]); //line 86
                $rate->setPrice($price);

                $result->append($rate);
            }
        }
        return $result;
    }
}