Jump to content

Get product shipping cost without cart


Nico Ratti

Recommended Posts

Hi,

I developed a module that allow users to ask for a quote of a single product. Now I need to calculate the shipping cost for the product but I can't get the shipping cost of the cart because the user ask for a quote from the product page without add it to the cart.

Obviously the customer will put the shipping address before the calculation.

Where I can take the shipping cost?

Edited by Nico Ratti (see edit history)
Link to comment
Share on other sites

First you will need to know the shopping carrier, and then use the carrier object to get the shipping fee estimation.

 

 

$carrier->getDeliveryPriceByPrice($order_total, $id_zone, $id_currency)

 
Or
 
$carrier->checkDeliveryPriceByPrice($id_carrier, $order_total, $id_zone, $id_currency)
 
  • Like 1
Link to comment
Share on other sites

Thank you for the answer. Unfortunately my shop have a few custom carriers module installed. So I write this little script to take the cheapest shipping fees:

function getShippingCost($id_product, $total_cost, $quantity, $id_country, $id_state = null, $zipcode = null, $id_lang = 1) {

	$product = new Product($id_product, true);
	$volume = $product->width * $product->height * $product->depth * $quantity;


	$country_iso = Country::getIsoById($id_country);
	$state_iso = null;
	if($id_state) {
		$sql = 'SELECT iso_code FROM '._DB_PREFIX_.'state WHERE id_state = '.$id_state;
		if ($row = Db::getInstance()->getRow($sql)){
			$state_iso = $row['iso_code'];
		}
	}

	//Get all carriersModule
	$carriers = Carrier::getCarriers($id_lang, true, false, false, null, Carrier::CARRIERS_MODULE);

	$shipping_cost = false;
	$available_carriers = array();

	foreach ($carriers as $corriere) {

	    $carrier = new Carrier($corriere['id_carrier'], false);

	    //get external shipping cost from module
	    if ($carrier->shipping_external) {

	        $module_name = $carrier->external_module_name;
	        $module = Module::getInstanceByName($module_name);

	        if (Validate::isLoadedObject($module)) {
	            if (array_key_exists('id_carrier', $module)) {
	                $module->id_carrier = $carrier->id;
	            }
	            $shipping_cost = $module->getOrderShippingCost(null, 0, true, $country_iso, $state_iso, $zipcode, $volume, $total_cost);

	            // check if this carrier is available
	            if ($shipping_cost === false) {
	                $shipping_cost = false;
	            }
	        } 
	        else {
	            $shipping_cost = false;
	        }
	    }

	    if ($shipping_cost !== false) {
	    	$available_carriers[] = array('carrier_name' => '', 'shipping_cost' => Product::convertAndFormatPrice($shipping_cost));
	    }
	}

	// if no carriers available return false
	if(!sizeof($available_carriers)) 
		return false;

	$cheapest = false;
	foreach($available_carriers as $carrier) {
		if($cheapest === false || $cheapest['shipping_cost'] > $carrier['shipping_cost'])
			$cheapest = $carrier;
	}

	return ($cheapest);
}
Link to comment
Share on other sites

  • 6 months later...

Hi. I'm new in developing Prestashop area. I try to implement this code... But I don't know how I should add this function, where and how to use it in product.tpl file.

Should I add this code to /classes/Product.php?

 

I'm using Prestashop 1.6

 

Could you please give me some tips or give some url to any manual?

Link to comment
Share on other sites

  • 4 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...