New Shipping Plugin Worked but Caused all add to cart and page loading slow

  • Posts: 567
  • Thank you received: 11
  • Hikashop Business
5 years 7 months ago #305204

-- HikaShop version -- : 4.0.3
-- Joomla version -- : 3.8
-- PHP version -- : 5.6
-- Error-message(debug-mod must be tuned on) -- : Slow Response

We developed a new shipping plugin and confirm it is working as it return all the required shipping charges and method

But after it is enabled and running, all the "add to cart" action become slow response (5 seconds & more delays) including checkout process

Is there any error in our code cause this ?

attached for your reference

File Attachment:

File Name: easyparcel2.0.zip
File Size:4 KB

Attachments:

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
5 years 7 months ago #305211

Hi,

You're missing the attribute:
var $use_cache = true;
in your plugin's class. If you check all the shipping plugins in HikaShop (besides the manual, which doesn't require caching), they all have that line. This activates the caching for the shipping plugin so that the system only asks the shipping plugin for the shipping methods when something changes in the cart.
If won't solve all the slow loadings as your plugin still has to contact the shipping gateway to get the fees calculated, but it will at least remove all the ones that can be removed.
Now you should check the ping between your test server and the shipping gateway server.
Finally, note that it will do the request to the shipping gateway once for each shipping method you have configured for that shipping plugin (and same for other shipping plugins like UPS, USPS, Fedex, etc). So the more shipping methods you have requiring such connection to shipping gateways, the more it will take time and slow down the page loading.

Please Log in or Create an account to join the conversation.

  • Posts: 567
  • Thank you received: 11
  • Hikashop Business
4 years 9 months ago #315533

Hi Nicolas

We developed one more new plugin

But this plugin sometimes worked, sometimes do not work

not sure if we are doing something wrong

Can you help to take a look ?

it get super slow when clicking next page during checkout


File Attachment:

File Name: delyva.zip
File Size:5 KB


This message contains confidential information

Attachments:
Last edit: 4 years 9 months ago by ler@singmuiheng.com.

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
4 years 9 months ago #315544

Hi,

The main issue you have is that you removed the code:

$local_usable_rates = array();
		$local_messages = array();
		$ret = parent::onShippingDisplay($order, $dbrates, $local_usable_rates, $local_messages);
		if($ret === false)
			return false;
from the beginning of the onShippingDisplay function.
This code handles the caching of the calculated shipping methods.
So this means that each time an AJAX request happens in the checkout, your plugin has to do the curl call to get the shipping methods available.
That's why it's slow.
For your development, it makes sense to remove that code so that you can get clear tests without caching, but once it's working, you want to add that code back to improve speeds on the checkout.

Then, you have some other problems, but not that important:
- before the call to your delyvacurl function, you should check if you have anything in $delyva. If the merchant doesn't have configured a shipping method for your plugin but your plugin is enabled, this could happen and in that case, you don't want to do anything and just return.
- If the merchant configured several shipping methods for your plugin (it could happen if he wants to add some extra cost for some products for example) you'll only process the last shipping method of your plugin and ignore the others.
- you copy/pasted the function shippingMethods from the FedEx plugin, but you didn't create the $this->fedex_methods array that it relies on at the beginning of your class. This function allows for the display of the proper shipping method name in the backend, once the order is created. It will work without it, but the shipping method displayed won't be as nice.

The following user(s) said Thank You: ler@singmuiheng.com

Please Log in or Create an account to join the conversation.

  • Posts: 567
  • Thank you received: 11
  • Hikashop Business
4 years 9 months ago #315557

i had added the line as mentioned

but it always become falst and local_usable_rate always empty

did i miss out some part?

File Attachment:

File Name: delyva_2020-02-10.zip
File Size:5 KB

Attachments:

Please Log in or Create an account to join the conversation.

  • Posts: 12953
  • Thank you received: 1778
4 years 9 months ago #315563

Hello,

The fact that the local_usable_rate rate is empty means that you shipping method shouldn't be used for that order, can you follow each stop of the "Shipping methods" section of our FAQ to be sure that default restriction options are not preventing your from using it ?
www.hikashop.com/support/faq#ship

Kind regards,
Mohamed.

Please Log in or Create an account to join the conversation.

  • Posts: 567
  • Thank you received: 11
  • Hikashop Business
4 years 9 months ago #315580

Dear Nicolas

Nope, we do not do any restriction on it

We have try to print_r when we published manual shipping plugin, the $usable_methods is also empty

What is the step we need to take?

if we remove

if($ret === false)
return false;

it works again, but extremely slow on loading

when we added back, it is super fast, but do not show shipping methods

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
4 years 9 months ago #315586

Hi,

Can you provide a screenshot of the settings of your shipping method ?

Please Log in or Create an account to join the conversation.

  • Posts: 567
  • Thank you received: 11
  • Hikashop Business
4 years 9 months ago #315625

This is the screen shot

Attachments:

Please Log in or Create an account to join the conversation.

  • Posts: 567
  • Thank you received: 11
  • Hikashop Business
4 years 9 months ago #315627

<?php
/**
* @copyright Copyright (c) 2019 Hikashop delyva Shipping Plugin. All rights reserved.
* @license www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*/

// no direct access
defined('_JEXEC') or die;

/**
* hikashopshipping - delyva Plugin
*
* @package Joomla.Plugin
* @subpakage Hikashop delyva Shipping Plugin.delyva
*/
class plgHikashopshippingDelyva extends hikashopShippingPlugin {
var $multiple = true;
var $name = 'delyva';
var $use_cache = true;

var $pluginConfig = array(
'customerid' => array('Action ID', 'input'),
'passphrase' => array('API Key', 'input'),
'url' => array('Url v2', 'input'),
'disable' => array('Diabled method (eg.poslaju, nationwide, etc)', 'input'),
'config_sender_address' => array('Pick up Address', 'input'),
'config_sender_postcode' => array('Pick up Postcode', 'input'),
'config_sender_country' => array('Pick up Country', 'input'),
'config_sender_pickup' => array('Pick up (Y/N)', 'input'),
'item_type' => array('Item Type', 'list', array('DOCUMENT','PARCEL','FOOD','FLOWER','CHILL','FROZEN','CAKE','OTHER','TASK', 'BULKY'))
);

public function shippingMethods(&$main) {
$methods = array();
if(!empty($main->shipping_params->methodsList))
$main->shipping_params->methods = hikashop_unserialize($main->shipping_params->methodsList);

if(!empty($main->shipping_params->methods)) {
foreach($main->shipping_params->methods as $method) {
$selected = null;
foreach($this->ups_methods as $ups) {
if($ups == $method) {
$selected = $ups;
break;
}
}
if($selected)
$methods[$main->shipping_id .'-'. $selected['key']] = $selected;
}
}
return $methods;
}

public function onShippingDisplay(&$order,&$methods,&$usable_methods,&$messages)
{
$input = JFactory::getApplication()->input;

// should be article, categories, featured, blog...
$view = $input->get('view');

if(!hikashop_loadUser())
return false;

if(empty($order->shipping_address)) {
$messages = 'No shipping address is configured.';
return true;
}


//if($this->loadShippingCache($order, $usable_methods, $messages))
//return true;

//print_r($methods);
print_r($usable_methods);print_r("1");

if(!function_exists('curl_init')) {
$app = JFactory::getApplication();
$app->enqueueMessage('The Delyva shipping plugin needs the CURL library installed but it seems that it is not available on your server. Please contact your web hosting to set it up.','error');
return false;
}

//$usable_methods = $methods;

$local_usable_rates = array();
$local_messages = array();

$ret = parent::onShippingDisplay($order, $methods, $local_usable_rates, $local_messages);

if($ret === false)
return false;
//print_r("test");
//write file to check output;
//$myfile = fopen("testing.txt", "w") or die("Unable to open file!");
//fwrite($myfile,print_r($this,true));
//fclose($myfile);

//$ret = parent::onShippingDisplay($order, $methods, $usable_methods, $messages);
foreach($methods as $method)
{
if($method->shipping_type == "delyva")
{
$delyva = $method;
}
}


//get response by using curl;
$data = $this->delyvacurl($order,$delyva);

print_r($data);

//write file to check output;
$myfile = fopen("testing.txt", "w") or die("Unable to open file!");
fwrite($myfile,print_r($data,true));
fclose($myfile);

//display all delyva's shipping methods;
foreach($data as $newmethods)
{
$shipping_id = $newmethods->service_code;
$shipping_name = $newmethods->name;
$shipping_description = $newmethods->description;
$shipping_price = $newmethods->price;
$shipping_days = $newmethods->deliveryDays;
$newmethods->shipping_id = $shipping_id;
$newmethods->shipping_type = "delyva";
$newmethods->shipping_zone_namekey = "";
$newmethods->shipping_tax_id = "0";
$newmethods->shipping_price = $shipping_price;
$newmethods->shipping_currency_id = "1";
$newmethods->shipping_name = $shipping_name;
$newmethods->shipping_description = $shipping_days;
$newmethods->shipping_published = "1";
$newmethods->shipping_ordering = "1";
$newmethods->shipping_currency = "";
$newmethods->shipping_params = $delyva->shipping_params;
if ($newmethods->shipping_params->disable){
$disable=$newmethods->shipping_params->disable;
}
$newmethods->shipping_images = "";
$newmethods->shipping_access = "all";

//if($usable_methods){
array_push($usable_methods, $newmethods);
array_push($local_usable_rates, $newmethods);
//print_r($usable_methods);
//}
}
return true;
}

//postparam
private function delyvacurl($order, $delyva)
{
$url = $delyva->shipping_params->url;
$customerid = $delyva->shipping_params->customerid;
$apikey = $delyva->shipping_params->passphrase;
$sender_address = $delyva->shipping_params->config_sender_address;
$sender_postcode = $delyva->shipping_params->config_sender_postcode;
$sender_country = $delyva->shipping_params->config_sender_country;
$receiver_address = $order->shipping_address->address_street;
$receiver_postcode = $order->shipping_address->address_post_code;
$send_country = "MY";
if (substr_count($order->shipping_address->address_country[0], "Malaysia")>=1) {
$send_country = "MY";
}
if (substr_count($order->shipping_address->address_country[0], "Singapore")>=1) {
$send_country = "SG"; }
if (substr_count($order->shipping_address->address_country[0], "Taiwan")>=1) {
$send_country = "TW"; }
if (substr_count($order->shipping_address->address_country[0], "United Kingdom")>=1) {
$send_country = "GB"; }
if (substr_count($order->shipping_address->address_country[0], "Australia")>=1) {
$send_country = "AU"; }
if (substr_count($order->shipping_address->address_country[0], "Afghanistan")>=1) {
$send_country = "AF"; }
if (substr_count($order->shipping_address->address_country[0], "Argentina")>=1) {
$send_country = "AR"; }
if (substr_count($order->shipping_address->address_country[0], "Austria")>=1) {
$send_country = "AT"; }
if (substr_count($order->shipping_address->address_country[0], "Brazil")>=1) {
$send_country = "BR"; }
if (substr_count($order->shipping_address->address_country[0], "Cambodia")>=1) {
$send_country = "KH"; }
if (substr_count($order->shipping_address->address_country[0], "Canada")>=1) {
$send_country = "CA"; }
if (substr_count($order->shipping_address->address_country[0], "Colombia")>=1) {
$send_country = "CO"; }
if (substr_count($order->shipping_address->address_country[0], "Denmark")>=1) {
$send_country = "DK"; }
if (substr_count($order->shipping_address->address_country[0], "Egypt")>=1) {
$send_country = "EG"; }
if (substr_count($order->shipping_address->address_country[0], "France")>=1) {
$send_country = "FR"; }
if (substr_count($order->shipping_address->address_country[0], "Germany")>=1) {
$send_country = "DE"; }
if (substr_count($order->shipping_address->address_country[0], "Hong Kong")>=1) {
$send_country = "HK"; }
if (substr_count($order->shipping_address->address_country[0], "India")>=1) {
$send_country = "IN"; }
if (substr_count($order->shipping_address->address_country[0], "Indonesia")>=1) {
$send_country = "ID"; }
if (substr_count($order->shipping_address->address_country[0], "Iraq")>=1) {
$send_country = "IQ"; }
if (substr_count($order->shipping_address->address_country[0], "Italy")>=1) {
$send_country = "IT"; }
if (substr_count($order->shipping_address->address_country[0], "Japan")>=1) {
$send_country = "JP"; }
if (substr_count($order->shipping_address->address_country[0], "Jordan")>=1) {
$send_country = "JO"; }
if (substr_count($order->shipping_address->address_country[0], "Macau")>=1) {
$send_country = "MO"; }
if (substr_count($order->shipping_address->address_country[0], "Maldives")>=1) {
$send_country = "MV"; }
if (substr_count($order->shipping_address->address_country[0], "Mexico")>=1) {
$send_country = "MX"; }
if (substr_count($order->shipping_address->address_country[0], "Myanmar")>=1) {
$send_country = "MM"; }
if (substr_count($order->shipping_address->address_country[0], "New Zealand")>=1) {
$send_country = "NZ"; }
if (substr_count($order->shipping_address->address_country[0], "Nigeria")>=1) {
$send_country = "NG"; }
if (substr_count($order->shipping_address->address_country[0], "Pakistan")>=1) {
$send_country = "PK"; }
if (substr_count($order->shipping_address->address_country[0], "Philippines")>=1) {
$send_country = "PE"; }
if (substr_count($order->shipping_address->address_country[0], "Portugal")>=1) {
$send_country = "PT"; }
if (substr_count($order->shipping_address->address_country[0], "Saudi Arabia")>=1) {
$send_country = "SA"; }
if (substr_count($order->shipping_address->address_country[0], "Spain")>=1) {
$send_country = "ES"; }
if (substr_count($order->shipping_address->address_country[0], "Sri Lanka")>=1) {
$send_country = "LK"; }
if (substr_count($order->shipping_address->address_country[0], "Switzerland")>=1) {
$send_country = "CH"; }
if (substr_count($order->shipping_address->address_country[0], "Thailand")>=1) {
$send_country = "TH"; }
if (substr_count($order->shipping_address->address_country[0], "Turkey")>=1) {
$send_country = "TR"; }
if (substr_count($order->shipping_address->address_country[0], "United States")>=1) {
$send_country = "US"; }
if (substr_count($order->shipping_address->address_country[0], "Vietnam")>=1) {
$send_country = "VN"; }
$receiver_country = $send_country;

if ($order->weight<=0){
$order->weight = "1";
}
//$item_weight = $order->package["weight"]["value"];
$item_weight = $order->weight;
$item_type = $delyva->shipping_params->item_type;
switch($item_type)
{
case 0:
$item_type = "PARCEL";
break;
case 1:
$item_type = "DOCUMENT";
break;
case 2:
$item_type = "FOOD";
break;
case 3:
$item_type = "FLOWER";
break;
case 4:
$item_type = "CHILL";
break;
case 5:
$item_type = "FROZEN";
break;
case 6:
$item_type = "CAKE";
break;
case 7:
$item_type = "OTHER";
break;
case 8:
$item_type = "TASK";
break;
case 9:
$item_type = "BULKY";
break;
}

date_default_timezone_set("Asia/Kuala_Lumpur");
$date = new JDate('now +9 hour');
$shipment_date = date_format($date, 'Y-m-d')."T".date_format($date, 'H:i:s').".000Z";

$ch = curl_init();

//curl_setopt($ch, CURLOPT_URL, " api.matdespatch.com/v2/service/price_quote "); v1:https://api.matdespatch.com/api/services/check

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_USERPWD, $customerid.":".$apikey);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{

\"sender_address\": \"".$sender_address."\",
\"sender_postcode\": \"".$sender_postcode."\",
\"sender_country\": \"".$sender_country."\",
\"receiver_address\": \"".$receiver_address."\",
\"receiver_postcode\": \"".$receiver_postcode."\",
\"receiver_country\": \"".$receiver_country."\",
\"item_weight\": ".$item_weight.",
\"item_type\": \"".$item_type."\",
\"shipment_date\": \"".$shipment_date."\"
}");

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
// "Authorization: Basic NWNhNTUzZTkzYTQ2NTIwNjVlY2NkZGIyOjUzOWQwNjkxZDgwOWUxZjk5NThiNjU4ZWYwYTUwNGJm",
//"Authorization: 5e15e47ce1925f20dcab1f13:7160aed31123d7840ff607e60fd6ad74",
));

$response = curl_exec($ch);
$response = json_decode($response);
//print_r($response);
curl_close($ch);

return $response;
}


//do not touch here...

}

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
4 years 9 months ago #315626

Hi,

I think that the issue is that the $name variable of your plugin is incorrect. It should be delyva and not Delyva.
Because of that id doesn't find any matching shipping method for your plugin and thus returns false.

Please Log in or Create an account to join the conversation.

  • Posts: 567
  • Thank you received: 11
  • Hikashop Business
4 years 9 months ago #315639

File Attachment:

File Name: delyva_2020-02-13.zip
File Size:6 KB
Dear Nicolas

I have attached the latest plugin

I have change it to "delyva"

however, it become random behaviour

some cases it return false

some cases it return true

and when we click next on ecommerce.satumalaysiahosting.com/checkout

it seem like go into infinite loop.......we can never complete the check out and stuck at this page

Attachments:
Last edit: 4 years 9 months ago by ler@singmuiheng.com.

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
4 years 9 months ago #315654

Hi,

You should first change the line:
var $use_cache = true;
to:
var $use_cache = false;
while you're developing to make sure that the code changes you do works properly.
Then, you can change that back when the plugin is working fine.
That will help with your tests.
Otherwise, if you do some test where you don't return something to print some debug but it still caches the information, you're left with nothing in the cache afterwards and your code won't run as the result is cached. So it makes it a lot more complex to test and debug as you're not sure what is going on.
Besides this the code you sent looks correct.

It will return false when either it already has the result cached or when there is no shipping method found for your plugin (so it means your plugin doesn't have to do anything other than return.
It will return true when it wants your plugin to process his shipping methods as it doesn't have the result cached yet for the current situation.
So it can look random because of the caching and when the situation changes or not in the checkout.

Please Log in or Create an account to join the conversation.

Time to create page: 0.112 seconds
Powered by Kunena Forum