Post by TactMedic » Sun Jan 12, 2020 8:25 am

Hi there,

Been looking since yesterday for a fix. I run OpenCart 3.0.3.2 with Journal 3 theme. My server run PHP 7.0 and I can upgrade it to 7.2 or 7.3. Since I update my cart from OC 2 to OC 3, everything went fine. We need to use a shipping extension "Flagship" to give shipping rates at checkout. The problem is this extension require PHP 7.1 or above. So I upgrade to PHP 7.2 (since 7.1 is not available from my hosting Cpanel). I was able to install my extension without any issue. But when we try to buy something or add something to the cart, an error appear on a popup and then it's stuck in the top of the page.

Code: Select all

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
OK
<b>Warning</b>: A non-numeric value encountered in <b>/system/library/cart/tax.php</b> on line <b>113</b><b>Warning</b>: A non-numeric value encountered in <b>/system/library/cart/tax.php</b> on line <b>113</b><b>Warning</b>: A non-numeric value encountered in <b>/catalog/model/extension/total/handling.php</b> on line <b>26</b>{"success":"NOT IMPORTANT CODE AFTER THIS
and the sticked error at the top

Code: Select all

Warning: A non-numeric value encountered in /system/library/cart/tax.php on line 113Warning: A non-numeric value encountered in/system/library/cart/tax.php on line 113Warning: A non-numeric value encountered in /catalog/model/extension/total/handling.php on line 26Warning: A non-numeric value encountered in /system/library/cart/tax.php on line 113Warning: A non-numeric value encountered in /system/library/cart/tax.php on line 113Warning: A non-numeric value encountered in /catalog/model/extension/total/handling.php on line 26
I tried to remplace all the controller/products files with the fixed one on github and replace the two problematic files "handling.php, tax.php" from the same github files with the (int) fix. I cleared the cache from Modification menu and from the dashboard. Also completely delete all the cookies, cache etc. from my second browser (firefox) and still the same error. When I put it back to PHP 7.0 in my cPanel, no more problems, except that my shipping module didn't work anymore
Last edited by TactMedic on Mon Jan 13, 2020 4:26 am, edited 1 time in total.

Newbie

Posts

Joined
Sat Dec 24, 2016 12:56 am

Post by head_dunce » Sun Jan 12, 2020 9:45 am

So what's the code on your /system/library/cart/tax.php on line 113 ?
My guess is that you're giving it null at times when it should be a zero (kinda the same thing, but the code might not think so)

Jim
https://www.carguygarage.com
Yahoo Store since 2006 moved to OpenCart on January 24, 2020


Active Member

Posts

Joined
Thu Apr 04, 2019 11:50 pm

Post by TactMedic » Sun Jan 12, 2020 9:52 am

head_dunce wrote:
Sun Jan 12, 2020 9:45 am
So what's the code on your /system/library/cart/tax.php on line 113 ?
My guess is that you're giving it null at times when it should be a zero (kinda the same thing, but the code might not think so)
Thanks for your reply, here it's what I have at my lines 110-115 (complete script)

Code: Select all

           110          if ($tax_rate['type'] == 'F') {
			111	          $amount += $tax_rate['rate'];
			112	           } elseif ($tax_rate['type'] == 'P') {
			113	           $amount += ($value / 100 * $tax_rate['rate']);
			114            }

Newbie

Posts

Joined
Sat Dec 24, 2016 12:56 am

Post by sw!tch » Sun Jan 12, 2020 11:09 am

The problem probably lies in the shipping module passing a non numeric character and not the actual tax class. I would contact the extension developer to resolve it, further you are using Journal 3, so could be something related to their code.

Otherwise you can look into the

Code: Select all

function getQuote
method from the shipping module, your issue is probably there.

Try a different shipping module as a test see if the error goes away.

Backup and learn how to recover before you make any changes!


Active Member

Posts

Joined
Sat Apr 28, 2012 2:32 pm

Post by TactMedic » Sun Jan 12, 2020 4:11 pm

this is the catalog/extension code of the extension

Code: Select all

<?php

class ModelExtensionShippingflagship extends Model {

    function getQuote($address) {
        $this->load->language('extension/shipping/flagship');
        $method_data = [];
        $quote_data = [];

        $rates = $this->getRatesArray($address);
        $flatFee = $this->config->get('shipping_flagship_fee');
        $markup = $this->config->get('shipping_flagship_markup');
        foreach ($rates as $rate) {
            $cost = $rate->price->total;
            $cost += ($markup/100) * $cost;
            $cost += $flatFee;
            $quote_data[$rate->service->courier_name.'_'.$rate->service->courier_desc.'_'.$rate->service->courier_code] = [
                'code'         => 'flagship.'.$rate->service->courier_name.'_'.$rate->service->courier_desc.'_'.$rate->service->courier_code,
                'title'        => $rate->service->courier_name == 'FedEx' ? $rate->service->courier_name.' '.$rate->service->courier_desc : $rate->service->courier_desc,
                'cost'         => $cost,
                'tax_class_id' => 0,
                'text'         => $this->currency->format($cost, $this->session->data['currency'])
            ];
        }
        $method_data = [
            'code'       => 'flagship',
            'title'      => $this->language->get('text_title'),
            'quote'      => $quote_data,
            'sort_order' => $this->config->get('shipping_flagship_sort_order'),
            'error'      => false
        ];
        return $method_data;
    }

    protected function getRatesArray($address) : array {

        $ratesArray = [];
        $selectedCouriers = $this->getSelectedCouriers();
        $selectedRates = count($selectedCouriers) > 0 ? explode(",",$selectedCouriers[0]["value"]) : [];

        $rates = $this->getRates($this->getPayload($address));

        foreach ($rates as $rate) {
            $courierDescription =  strcasecmp($rate->service->courier_name,'fedex') === 0 ? 'Fedex '.$rate->service->courier_desc : $rate->service->courier_desc;
            $ratesArray[] = in_array($courierDescription, $selectedRates) == 1 ? $rate : NULL;
        }

        $ratesArray = array_filter($ratesArray, function($value){ return $value != NULL; });

        return $ratesArray;
    }

    /*
     * Mixed return type
     */

    protected function getRates(array $payload) {
        $url = $this->config->get('smartship_api_url').'/ship/rates';
        $token = $this->config->get('shipping_flagship_token');
        $response = $this->apiRequest($url,$payload,$token,'POST',30);
        return  array_key_exists("response", $response) ? $response["response"]->content : [];
    }

    protected function apiRequest(string $url,array $json, string $apiToken,string $method, int $timeout, string $flagshipFor='OpenCart',string $version='1.0.0') : array {

        $curl = curl_init();
        $options = [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => $timeout,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => $method,
            CURLOPT_POSTFIELDS  => json_encode($json),
            CURLOPT_HTTPHEADER => array(
                "X-Smartship-Token: ". $apiToken,
                "Content-Type: application/json",
                "X-F4".$flagshipFor."-Version: ".$version
                )
            ];
        curl_setopt_array($curl, $options);
        $response = curl_exec($curl);
        $httpcode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
        $responseArray = [
            "response"  => json_decode($response),
            "httpcode"  => $httpcode
        ];
        curl_close($curl);

        if(($httpcode >= 400 && $httpcode < 600) || ($httpcode === 0) || ($response === false) || ($httpcode === 209)){
            return [];
        }
        return $responseArray;
    }


    protected function getPayload(array $address) : array {
        $from = [
            "city" => 'Montreal',
            "country" => $this->getCountryCode($this->config->get('config_country_id')),
            "state" => $this->getZoneCode($this->config->get('config_zone_id')),
            "postal_code" => $this->config->get('shipping_flagship_postcode'),
            "is_commercial" => true
        ];
        $to = [
            "city" => $address['city'],
            "country" => $address['iso_code_2'],
            "state" => $address['zone_code'],
            "postal_code" => $address['postcode'],
            "is_commercial" => false
        ];
        $packages = [
            "items" => $this->getPackageItems(),
            "units"=> 'imperial',
            "type"=>"package",
            "content"=>"goods"
        ];
        $payment = [
            "payer" => "F"
        ];
        $options = [
            "address_correction" => true
        ];
        $payload = [
            "from" => $from,
            "to" => $to,
            "packages" => $packages,
            "payment" => $payment,
            "options" => $options
        ];
        return $payload;
    }
    protected function getPackageItems() : array {
        $packageItems = [];
        try{
            $packings = $this->getPackings();
            return $this->checkPackings($packings);
        } catch(PackingException $e){
            $this->session->data['error'] = $e->getMessage();
			return [];
        }
    }
    protected function checkPackings($packings) : array {
        if($packings == NULL){
            return [
                [
                    "length" => 1,
                    "width" => 1,
                    "height" => 1,
                    "weight" => 1,
                    "description" => "Item 1"
                ]
            ];
        }
        foreach ($packings as $packing) {
            $packageItems[] = [
                "length" => $packing->length,
                "width" => $packing->width,
                "height" => $packing->height,
                "weight" => $packing->weight,
                "description" => $packing->box_model
            ];
        }
        return $packageItems;
    }
    protected function getPackings() : ?array {
        $items = $this->getItems();
        $boxes = $this->getAllBoxes();
        if(count($boxes) == 0){
            return NULL;
        }
        $packingPayload = [
            "items" => $items,
            "boxes" => $boxes,
            "units" => 'imperial'
        ];
        $packings = $this->packingRequest($packingPayload);
        return $packings;
    }

    protected function packingRequest(array $payload) : ?array {
        $url = $this->config->get('smartship_api_url').'/ship/packing';
        $token = $this->config->get('shipping_flagship_token');
        $response = $this->apiRequest($url,$payload,$token,'POST',30);
        return array_key_exists("response", $response) ? $response["response"]->content->packages : NULL ;
    }

    protected function getItems() : array {
        $items = [];
        $products = $this->cart->getProducts();
        $imperialLengthClass = $this->getImperialLengthClass();
        $imperialWeightClass = $this->getImperialWeightClass();
        foreach ($products as $product) {
            $items[] = [
                "length" => $product["length"] == 0 ? 1 : ($product['length_class_id'] != $imperialLengthClass ? ceil($this->length->convert($product["length"],$product['length_class_id'],$imperialLengthClass)) : ceil($product["length"]) ),
                "width" => $product["width"] == 0 ? 1 : ($product['length_class_id'] != $imperialLengthClass ? ceil($this->length->convert($product["width"],$product['length_class_id'],$imperialLengthClass)) : ceil($product["width"])),
                "height" => $product["height"] == 0 ? 1 : ($product['length_class_id'] != $imperialLengthClass ? ceil($this->length->convert($product["height"],$product['length_class_id'],$imperialLengthClass)) : ceil($product["height"])),
                "weight" => $product["weight"] < 1 ? 1 : ($product['weight_class_id'] != $imperialWeightClass ? $this->weight->convert($product["weight"],$product['weight_class_id'],$imperialWeightClass) : $product["weight"]),
                "description" => $product["name"]
            ];
        }
        return $items;
    }
    protected function getCountryCode(int $country_id) : string {
        $this->load->model('localisation/country');
        $country = $this->model_localisation_country->getCountry($country_id);
        return $country['iso_code_2'];
    }
    protected function getZoneCode(int $zone_id) : string {
        $this->load->model('localisation/zone');
        $zone = $this->model_localisation_zone->getZone($zone_id);
        return $zone['code'];
    }
    protected function getImperialLengthClass() : int {
        $query = $this->db->query("SELECT length_class_id FROM ".DB_PREFIX."length_class_description where unit = 'in'");
        return $query->row['length_class_id'];
    }
    protected function getImperialWeightClass() : int {
        $query = $this->db->query("SELECT weight_class_id FROM ".DB_PREFIX."weight_class_description where unit = 'lb'");
        return $query->row['weight_class_id'];
    }
    protected function getAllBoxes() : array {
        $query = $this->db->query("SELECT box_model,length,width,height,weight,max_weight from ".DB_PREFIX."flagship_boxes");
        return $query->rows;
    }

    protected function getSelectedCouriers() : array {
        $query = $this->db->query("SELECT value FROM `".DB_PREFIX."flagship_couriers` ");
        return $query->rows;
    }
}

Newbie

Posts

Joined
Sat Dec 24, 2016 12:56 am

Post by JNeuhoff » Sun Jan 12, 2020 6:57 pm

Your error is caused by this line:

Code: Select all

$total['total'] += $this->config->get('total_handling_fee');
in file 'catalog/model/extension/total/handling.php'

Make sure to have a correctly configured handling fee, see your admin backend at Extensions > Extensions > Order Totals > Handling Fee. If you don't use a handling fee, then uninstall this order total.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by TactMedic » Mon Jan 13, 2020 4:26 am

JNeuhoff wrote:
Sun Jan 12, 2020 6:57 pm
Your error is caused by this line:

Code: Select all

$total['total'] += $this->config->get('total_handling_fee');
in file 'catalog/model/extension/total/handling.php'

Make sure to have a correctly configured handling fee, see your admin backend at Extensions > Extensions > Order Totals > Handling Fee. If you don't use a handling fee, then uninstall this order total.
Thanks ! I did this and it fix my issue !

Newbie

Posts

Joined
Sat Dec 24, 2016 12:56 am
Who is online

Users browsing this forum: No registered users and 23 guests