Jump to content

Shipping cost calculations


Recommended Posts

Hi!

 

I have created this php script for shipping cost calculation.

The problem is i don't know how to implement in prestashop 1.5.2

 

I think an options is to use override/classes/Cart.php but i dont know how to implement and adapt the script.

 

I hope someone ken help me.

 

This is the script for shipping calculations price

 

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass= '12345';
$dbname = "prestadatabase";

$check_con = mysql_connect($dbhost,$dbuser,$dbpass);
$check_db = mysql_select_db($dbname,$check_con);

$id_shop = 1;
$id_cart = 5; //user shopping cart   id
//extract id products from shoping cart
$products_cart_id = array();
$cart_products_query = mysql_query("SELECT id_product FROM ps_cart_product WHERE id_cart=".$id_cart);
while($cart_products_array = mysql_fetch_array($cart_products_query)){
$cart_products = $cart_products_array["id_product"];
$products_cart_id[] = $cart_products;
}

$total_products = count($products_cart_id);

// split products with special price and products with normal price in 2 arrays
$specials_prod_array = array();
$normal_prod_array = array();
foreach($products_cart_id as $product_id) {
$chk_specialprice_query_count = mysql_query("SELECT * FROM ps_specific_price WHERE id_product=".$product_id." AND id_shop=1");
while($chk_specialprice = mysql_fetch_array($chk_specialprice_query_count)) {
$value_specialprice = $chk_specialprice["reduction"];
$value_normalprice = $chk_specialprice["price"];
$date_from = $chk_specialprice["from"];
$date_to = $chk_specialprice["to"];
// check if special price is valid as time
if(  ($date_from == '0000-00-00 00:00:00') and ($date_to == '0000-00-00 00:00:00')  ) { $valability_special_price = 1; }
if(  ($date_from <> '0000-00-00 00:00:00') and ($date_to <> '0000-00-00 00:00:00')  ) { $valability_special_price = $date_to - $date_from; }
}
// split products id
if(  ($value_specialprice <> "0.000000") and ($valability_special_price > 0)	) {$specials_prod_array[] = $product_id; } else  { $normal_prod_array[] = $product_id; }

									   }


// procesing specials products
$array_s_total_cart = array();
foreach($specials_prod_array as $special_prod_id){
$query_specials = mysql_query("SELECT ps_cart_product.quantity, ps_specific_price.reduction, ps_specific_price.price FROM `ps_cart_product` LEFT JOIN `ps_specific_price` ON ps_cart_product.id_product = ps_specific_price.id_product WHERE ps_cart_product.id_product=".$special_prod_id." AND ps_specific_price.id_shop=".$id_shop);
$mysql_fetch_array_specials = mysql_fetch_array($query_specials);
$qt_specialproduct = $mysql_fetch_array_specials["quantity"];
$price_specialproduct = $mysql_fetch_array_specials["price"] - $mysql_fetch_array_specials["reduction"] ;
$price_specialproduct = $price_specialproduct * $qt_specialproduct;
$array_s_total_cart[] = $price_specialproduct;
}

$total_nr_special_cart = count($specials_prod_array);
$total_special_cart =  array_sum($array_s_total_cart);


// procesing normal price products
$array_n_total_cart = array();
foreach($normal_prod_array as $normal_prod_id) {
$query_normal_price = mysql_query("SELECT ps_cart_product.quantity, ps_product.price FROM `ps_cart_product` LEFT JOIN `ps_product` ON ps_cart_product.id_product = ps_product.id_product WHERE ps_cart_product.id_product=".$normal_prod_id);
$mysql_fetch_array_normal_price = mysql_fetch_array($query_normal_price);
$qt_normalproduct = $mysql_fetch_array_normal_price["quantity"];
$price_normalproduct = $mysql_fetch_array_normal_price["price"] * $qt_normalproduct;
$array_n_total_cart[] = $price_normalproduct;
}


$total_nr_normal_price_cart = count($normal_prod_array);
$total_normal_price_product_cart =  array_sum($array_n_total_cart);


// conditions of shipping price
$value_limit = 179;
if( ($total_nr_special_cart == 0) and ($total_normal_price_product_cart > $value_limit) ) { $shipping_price = '0'; }
if( ($total_nr_special_cart == 0) and ($total_normal_price_product_cart < $value_limit) ) { $shipping_price = '10'; }
if( ($total_nr_special_cart <> 0) and ($total_nr_normal_price_cart == 0) ) { $shipping_price = '20'; }
if( ($total_nr_normal_price_cart > 1) and ($total_normal_price_product_cart > $value_limit) and ($total_nr_special_cart == 1) ) { $shipping_price = '10'; }
if( ($total_nr_normal_price_cart == 1) and ($total_normal_price_product_cart < $value_limit) and ($total_nr_special_cart == 1) ) { $shipping_price = '20'; }

echo '<br>Shipping value: '.$shipping_price."<br>";


?>

 

Thank you !

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

×
×
  • Create New...