Alexandre MAS Posted April 24, 2024 Share Posted April 24, 2024 (edited) Hello, I want to create a php script to calculate the shipping cost using my prestashop rules. I understand the function to use is getPackageShippingCost from classes/Cart.php. I must mis understand how to call it/the parameters. Here below my code. I would rather to call with a list of product but if not possible the cart ID would be ok I get a fatale error. (using php8.0) How would you correct this code ? I thank you a lot in advance for your help ! Alexandre <?php // Inclure le fichier d'initialisation de PrestaShop require_once('/home/otokipd/www/config/config.inc.php'); // Inclure le fichier Cart.php où se trouve la fonction require_once('/home/otokipd/www/classes/Cart.php'); // Charger le contexte actuel de PrestaShop $context = Context::getContext(); // Récupérer l'ID du panier $id_cart = 13239; // Récupérer l'ID du transporteur (carrier) et la zone (zone) appropriés à votre situation $id_carrier = 219; // Modifier avec l'ID du transporteur approprié $id_zone = 113; // Modifier avec l'ID de la zone appropriée // Créer une instance de la classe Cart avec l'ID du panier $cart = new Cart($id_cart); // Créer un tableau avec les informations du produit $product_list = array( array( 'id_product' => 1026, // ID du produit 'id_product_attribute' => 0, // ID de l'attribut du produit (0 pour le produit sans attribut) 'quantity' => 1, // Quantité du produit 'is_virtual' => 0 // Indique si le produit est virtuel (1 pour virtuel, 0 pour physique) ) ); // Récupérer le coût d'expédition du colis en utilisant la méthode getPackageShippingCost() $shipping_cost = $cart->getPackageShippingCost( $id_carrier, // ID du transporteur true, // Utiliser la taxe $default_country, // Pays par défaut (utilisé si le paramètre $default_country est null) $product_list, // Liste de produits concernés par l'expédition (null pour tous les produits du panier) $id_zone, // ID de la zone false // Garder les prix de commande (keepOrderPrices) ); // Afficher le coût d'expédition if ($shipping_cost !== false) { echo "Coût d'expédition : $shipping_cost"; } else { echo "Impossible de calculer le coût d'expédition pour le transporteur $id_carrier et la zone $id_zone."; } ?> Edited April 24, 2024 by Alexandre MAS (see edit history) Link to comment Share on other sites More sharing options...
WebDesk Solution Posted May 17, 2024 Share Posted May 17, 2024 Hello @Alexandre MAS, We have checked the issue and resolved it. Please use the updated code below in your PHP script: <?php // Load PrestaShop configuration require(dirname(__FILE__).'/config/config.inc.php'); require(dirname(__FILE__).'/init.php'); // Get cart ID // $cartId = Context::getContext()->cart->id; $cartId = 32; $carrierId = 2; // Replace with your carrier ID $zoneId = 2; // Replace with your zone ID // Ensure the cart ID is valid if (!$cartId) { die('No cart found.'); } // Load the cart $cart = new Cart($cartId); // Calculate the total shipping cost // $totalShippingCost = $cart->getOrderTotal(true, Cart::ONLY_SHIPPING); $totalShippingCost = $cart->getPackageShippingCost($carrierId, true, null, $zoneId); echo 'Total Shipping Cost: ' . Tools::displayPrice($totalShippingCost); Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now