Hikashop Shipping

  • Posts: 175
  • Thank you received: 0
7 years 8 months ago #263532

-- HikaShop version -- : 2.6.4

hello

i've been trying to develop a shipping plugin that can handle multiple shipping price from different source



Context : these 4 shipping methods are same shipping method but with different parameters in backend/configuration file

the one with "free shipping" means it's not a available, how to disable it so when user click on it then it wont save/cannot pick the one with free shipping??

If i click any of the free shipping [shouldve been disabled] method and click on the one with price, it will show free shipping on other shipping method



If i click the one thats available , it will show the real number [after previously "free shipping"]



However when i click next after picking the shipping, it returned to free shipping [the shipping price is gone again]



my code is :
			//real cost = price i get from API from other site that done the calculation of shipping price

				$rate->shipping_price = $real_cost;
				
				if($real_cost!=0){
				$order->shipping[0]->shipping_price = $real_cost; 
				}
				

Any suggestion what should i do? Am i doing it wrong?

Thank You

Last edit: 7 years 8 months ago by veeco.

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
7 years 8 months ago #263534

Hi,

The shipping plugins implement the trigger onShippingDisplay:
www.hikashop.com/support/documentation/6...ml#onShippingDisplay
The variable $methods contains all the shipping methods loaded from the database.
The variable $usable_methods contains all the shipping methods that will be displayed.
By default the variable $usable_methods is an empty array, and each shipping plugin modify and copy the methods from $methods to $usable_methods so that they can be displayed.
Thus it's really easy to remove the methods from the selection area when they shouldn't be available. Just don't add them in the $usable_methods array in the onShippingDisplay method of your shipping plugin.

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

  • Posts: 175
  • Thank you received: 0
7 years 8 months ago #263593

Hello

Thank You, it works now

However there is a bug where if user create a new address, it kinda reset the shipping choices and set the shipping price 0



It shows that my calculation works fine here is my code

//rate = method
//usable_rates = usable_method
$rate->shipping_price = $real_cost;
$usable_rates[$rate->shipping_id]=$rate;


This bug only occured if there is only 1 option available, [if there is 2 option out of 4 available, it will choose the 1st one]

edit : looks like it still used any shipping method the user choose before creating a new address and because the new address doesnt have that option available, it shows free shipping as the result is 0 (???)

If i pick another address and pick the one i just created, it will fix and show the real shipping price



Any suggestion what should i do? Thank You


Edit 2 :
I also noticed that the shipping will reset to 0 when i click the checkout button from cart



After changing address, the shipping price will be fixed



any suggestion what should i do? Why its resetting to 0?

Thanks

edit 3 : is there is a system that somehow store shipping methods? i noticed that the selected shipping methods are saved , even when i clicked the checkout button it will keep the previous settings? is it possible to force it to refresh shipping method everytime they open checkout?

thanks

Last edit: 7 years 8 months ago by veeco.

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
7 years 8 months ago #263602

Hi,

Hard to say anything useful.
I don't know the full code of your shipping plugin, I don't know how you configured your checkout, etc.
Could you provide the full code of the plugin, a screenshot of the settings of your shipping method and a screenshot of the settings under the "checkout" tab of the HikaShop configuration ?

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

  • Posts: 175
  • Thank you received: 0
7 years 8 months ago #263719

nicolas wrote: Hi,

Hard to say anything useful.
I don't know the full code of your shipping plugin, I don't know how you configured your checkout, etc.
Could you provide the full code of the plugin, a screenshot of the settings of your shipping method and a screenshot of the settings under the "checkout" tab of the HikaShop configuration ?


Sure!
<?php
/**
 * @package	HikaShop for Joomla!
 * @version	2.2.1
 * @author	hikashop.com
 * @copyright	(C) 2010-2013 HIKARI SOFTWARE. All rights reserved.
 * @license	GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?><?php
class plgHikaShopshippingRajaongkir extends hikashopShippingPlugin {
	var $multiple = true;
	var $name = 'rajaongkir'; 
 
	
	function onShippingDisplay(&$order,&$dbrates,&$usable_rates,&$messages){		
		//INITIAL   
		if(!hikashop_loadUser())
			return false;
		$local_usable_rates = array();
		$local_messages = array();
		$ret = parent::onShippingDisplay($order, $dbrates, $local_usable_rates, $local_messages);
		if($ret === false) 
			return false; 
		 	  
		 
		$currentShippingZone = null;
		$currentCurrencyId = null;
 
	 	$total_order = intval($order->total->prices[0]->price_value); 
		  
		$found = true;
		//GET ZONE
		//$zoneClass=hikashop_get('class.zone');
		//$zones = $zoneClass->getOrderZones($order);
		
		//BERAT TIDAK DITEMUKAN
		$total_weight = $order->weight;
		//var_dump($total_weight);
		if($total_weight==0){
		 $total_weight = 1;	
		}
		/*
		var_dump($total_weight);
		die();
		*/
		if($total_weight<=0){
			return false;
		}
		
		if(empty($order->shipping_address)){
			return true;
		}
		
	
	
		foreach($local_usable_rates as $k => $rate){
			
						
			if($rate->shipping_type=="rajaongkir"){
				$address_city = $order->shipping_address->address_city;
							
				// $rateperKG=
				$db = JFactory::getDbo();
				$query = $db->getQuery(true);
				$query->select('*')
				->from('rajaongkir')
				->where('nama_kota = "'.$address_city.'"');
				$db->setQuery($query);
				$results = $db->loadObjectList();
				
				$city_id = $results[0]->id;

				$service_code = $rate->shipping_params->service_code;
				$service_code_text = $rate->shipping_params->service_code;
				
				$service_code = explode('|',$service_code);
				
				$service_courier = $service_code[0];
				$service_type	 = $service_code[1];
				
				$totalweight=ceil($order->weight);
				$totalweight_kg = $totalweight*1000;
				//volume				
				$origin = $rate->shipping_params->origin_city;
				
				//Search for rajaongkir_price
				$query = $db->getQuery(true);
				$query->select('*')
				->from('rajaongkir_price')
				->where('origin = "'.$origin.'"')				
				->where('destination = "'.$city_id.'"')				
				->where('weight = "'.$totalweight_kg.'"')
				->where('service_code = "'.$service_code_text.'"');
				$db->setQuery($query);
				$results_service = $db->loadObjectList();		
				
				$is_exists = 0;
				$is_exists = count($results_service);

				$expired = 0;
				if($is_exists!=0){
					$expire = $results_service[0]->expire;
					$expire = strtotime($expire);
					
					$now = date("Y-m-d H:i:s");
					$now = strtotime($now);
					
					
					if($now>$expire){
					//expired						
					$is_exists = 0;
	
					$query = $db->getQuery(true);
					$query = "DELETE FROM rajaongkir_price where origin='".$origin."' and destination='".$city_id."' and weight='".$totalweight."' and service_code='".$service_code_text."'";
					$db->setQuery($query);
					$db->execute();	
					}else{
					//not expired
					$real_cost = $results_service[0]->price;
					$real_etd = $results_service[0]->etd;
					$no_shipping=0;
					}
					
				}
				//EO Sarch for rajaongkir price
				
				if($is_exists==0){
				//Load shipping from rajaongkir
				$curl = curl_init();
				
				curl_setopt_array($curl, array(
				  CURLOPT_URL => "http://api.rajaongkir.com/starter/cost",
				  CURLOPT_RETURNTRANSFER => true,
				  CURLOPT_ENCODING => "",
				  CURLOPT_MAXREDIRS => 10,
				  CURLOPT_TIMEOUT => 30,
				  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
				  CURLOPT_CUSTOMREQUEST => "POST",
				  CURLOPT_POSTFIELDS => "origin=151&destination=".$city_id."&weight=".$totalweight_kg."&courier=".$service_courier,
				  CURLOPT_HTTPHEADER => array(
					"content-type: application/x-www-form-urlencoded",
					"key: <!-- API Code -->f"
				  ),
				));
				
				$response = curl_exec($curl);

				curl_close($curl);
				
				$response = json_decode($response);
				$service_list = $response->rajaongkir->results[0]->costs;
				
				$cost_count = count($service_list);
				$no_shipping=0;
				$real_cost = 0;
				$triggered = 0;		
				
				if($cost_count==0){
				//shipping is not available
				$no_shipping=1;
				}else{
				//shipping_available
					foreach($service_list as $sl){
						$current_service = $sl->service;
						$cost			 = $sl->cost[0]->value;
						$etd			 = $sl->cost[0]->etd;
						
						if($current_service==$service_type){
							$real_cost	= $cost;
							$real_etd	= $etd;
							$triggered = 1;
						}
	
					}
				}
				
				if($triggered==0){
					$no_shipping=1;
				}
				
				//EO Load shipping from rajaongkir
				
				
				//SAVE TO DATABASE
				if($triggered==1){
				$expire = strtotime('+2 weeks');
				$expire = strtotime('+2 weeks');
				$expire =  date('Y-m-d H:i:s',$expire);
					
				$query = $db->getQuery(true);
				$query = 'INSERT INTO rajaongkir_price values("'.$origin.'","'.$city_id.'","'.$totalweight_kg.'","'.$service_code_text.'","'.$real_cost.'","'.$real_etd.'","'.$expire.'")';
				$db->setQuery($query);
				$db->execute();		
				}
				//EO SAVE TO DATBASE
				
				//if not exists
				}
				
				$rate->shipping_price = $real_cost;

				$new_weight_volume = $totalweight." Kg";
				/*
				var_dump($new_price_weight);
				var_dump($new_price_volume);
				echo '<br />';
				var_dump($totalweight.' KG');
				var_dump($volume.' M');			
				*/
				//setting description
				

				
				
				if($no_shipping==1){
					$rate->shipping_published = 0; 
					$order->shipping[0]->shipping_price_with_tax = 0; //this one that changed the price
					$order->shipping[0]->shipping_price = 0; //this one that changed the price				
					$rate->shipping_description="Pengiriman Tidak Dapat Di lakukan";	
					//$rate->shipping_ordering=99;
				}else{
					
					$rate->shipping_description="
					".JText::_('JNE_TOTAL_BERAT')." : ".$new_weight_volume."<br />
					".JText::_('JNE_ESTIMASI_PENGIRIMAN')." : ".$real_etd." ".JText::_('JNE_HARI_KERJA')."<br />";
					
					$usable_rates[$rate->shipping_id]=$rate;//add shipping to tthe list

				if($real_cost!=0){
				//	$order->shipping[0]->shipping_price = $real_cost; //this one that changed the price
				}
					
				}



				/*
				if($total_order > 200000){
				//lebih dari 200k, gratis	
				$rate->shipping_price = 0;
				$order->shipping[0]->shipping_price = 0;
				$rate->shipping_description="
					".JText::_('JNE_TOTAL_BERAT')." : ".$new_weight_volume."<br />
					".JText::_('JNE_METHOD')." : ".$method."<br />
					".JText::_('JNE_ESTIMASI_PENGIRIMAN')." : ".$etd." ".JText::_('JNE_HARI_KERJA')."<br />";
				}
				*/

			}
			
		}
		
		
	}/*
	

	function onShippingConfiguration(&$element){ //FOR BACKEND

	} 
	
	function onShippingConfigurationSave(&$element){ //FOR BACKEND
	}
	
	
	
	function shippingMethods(&$method){
		die('123');
	}	*/
	
	function onShippingSave(&$cart,&$methods,&$shipping_id){
		
		/*
		echo '<pre>';
		var_dump($cart);
		echo '</pre>';

		echo '<pre>';
		var_dump($methods);
		echo '</pre>';

		echo '<pre>';
		var_dump($shipping_id);
		echo '</pre>';
		
		*/

		foreach($methods as $m){
			$all_shipping = $m->shipping_id;
			
			
			if($all_shipping==$shipping_id){
			$shipping_published = $m->shipping_published;
				if($shipping_published==0){					
					return false;	
				}
			}
		}
		
				
		
		$usable_mehtods = array();
		$errors = array();
		$this->onShippingDisplay($cart,$methods,$usable_mehtods,$errors);
		foreach($usable_mehtods as $k => $usable_method){
			if($usable_method->shipping_id==$shipping_id){
				return $usable_method;
			}
		}
		return false;
		/*var_dump("asd");
			die();*/
	}
	
	
}


Shipping Setting :



checkout settings



EDIT :

If user change their address's city [that is the parameter that is used by my shipping script] the shipping method of first method will be turned into free shipping.I have var_dump the variable and everything is working fine just like the other 2



if i select another address and then select the one that i just created/changed, then it works normally. Any suggestion?

edit 2 :
If i select other shipping method, that method turned into free shipping ??


note : these 3 methods are the same method but with different parameters in their setting/configuration

Last edit: 7 years 8 months ago by veeco.

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
7 years 8 months ago #263733

Hi,

The function onShippingDisplay looks correct. The onShippingSave function should not be necessary.
The shipping methods are indeed cached to avoid doing too much unecessary cURL.
I would recommend to add that attribute to your shipping plugin class:
var $use_cache = false;
That will deactivate the cache for your plugin. It will probably help when you do your tests. That's probably why you didn't see the shipping methods working properly even after fixing the onShippingDisplay function with the instructions we gave.

The following user(s) said Thank You: veeco

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

Time to create page: 0.069 seconds
Powered by Kunena Forum