BenDev Posted December 4, 2018 Share Posted December 4, 2018 Hello, I have to send a request to a script on my server in order to create and display a cart for my PrestaShop : 1.7.1.1 . An old script exist (for an older version of Prestashop) and I wanted to modify it but I've got an issue with this : <?php require_once('config/config.inc.php'); include_once('classes/Cookie.php'); require_once('vendor/autoload.php'); $context = Context::getContext(); $debug = fopen("debug.txt", "a"); $cookie = $context->cookie; $id_cart = $cookie->id_cart; fwrite($debug, "Context = "); fwrite($debug, json_encode($context)); fwrite($debug, "Cookie = "); fwrite($debug, json_encode($cookie)); $basketHeader = "Location: https://www.my-site.com/fr/panier"; $products_type_max = 5; $basket = array(); if(!empty($_GET)){ fwrite($debug, "GET = "); fwrite($debug, json_encode($_GET)); foreach($_GET as $key=>$value){ $basket[$key]=preg_replace("/[^0-9]/", "", $value); } fwrite($debug, "Basket = "); fwrite($debug, json_encode($basket)); if(count($basket) != (2 * $products_type_max) ){ header($basketHeader); exit(); } } else{ header($basketHeader); exit(); } if (isset($basket)) { if ($cookie->id_cart) { $cart = new Cart($cookie->id_cart); } if(!isset($cart) OR !$cart->id){ $cart = new Cart(); $cart->id_shop_group = 2; $cart->id_shop = 1; if(!isset($cookie->id_customer)){ $cart->id_customer = 0; $cart->id_address_delivery = 0; } else { $cart->id_customer = (int)($cookie->id_customer); $cart->id_address_delivery = (int)(Address::getFirstCustomerAddressId($cart->id_customer)); } $cart->id_guest=(int)($cookie->id_guest); $cart->id_address_invoice = $cart->id_address_delivery; $cart->id_lang = (int)($cookie->id_lang); $cart->id_currency = 1; $cart->id_carrier = 0; $cart->recyclable = 0; $cart->gift = 0; $cart->add(); $cookie->id_cart = (int)($cart->id); $cart->update(); } for($i=0;$i<$products_type_max;$i++){ if($basket['qty'.$i]>0){ $cart->updateQty($basket['qty'.$i],$basket['id'.$i]); } } if(!($cart->containsProduct(13))){ $cart->updateQty(1,13); } fwrite($debug, "Cart = "); fwrite($debug, json_encode($cart)); fclose($debug); header($basketHeader); exit(); } This script get the parameters, create a cart in prestashop but when i redirect the user... the cart page is empty. I can see the abandoned cart in the admin, but the user/visitor have nothing shown... Problem with cookie/session ? Can you guide/hep me ? PS: I'm more a JS developper, and i have put many debug log in this script to help my research ^^' sorry if it bother you Best regards, Link to comment Share on other sites More sharing options...
BenDev Posted December 13, 2018 Author Share Posted December 13, 2018 (edited) Hello, This file receive a request by an extern app, and it is located at the prestashop basepath along with the index.php... <?php require_once('config/config.inc.php'); include_once('classes/Cookie.php'); require_once('vendor/autoload.php'); ini_set("display_errors", 1); $debug = fopen("debug.txt", "a"); $context = Context::getContext(); $basketHeader = "Location: https://www.my-site.com/fr/panier"; $products_type_max = 5; $basket = array(); /** * Get the infos or redirect */ if(!empty($_GET)){ foreach($_GET as $key=>$value){ $basket[$key]=preg_replace("/[^0-9]/", "", $value); //fwrite($debug, $key. ' = '.$basket[$key]); } if(count($basket) != (2 * $products_type_max) ){ header($basketHeader); exit(); } } else{ header($basketHeader); exit(); } fwrite($debug, json_encode($context)); /** * Try to set basket */ if (isset($basket)) { if (is_null($context->cart)) { $context->cart = new Cart($context->cookie->id_cart); $context->cart->id_shop_group = 1; $context->cart->id_shop = 1; if(!isset($cookie->id_customer)){ $context->cart->id_customer = 0; $context->cart->id_address_delivery = 0; } else { $context->cart->id_customer = (int)($cookie->id_customer); $context->cart->id_address_delivery = (int)(Address::getFirstCustomerAddressId($context->cart->id_customer)); } $context->cart->id_guest=(int)($cookie->id_guest); $context->cart->id_address_invoice = $context->cart->id_address_delivery; $context->cart->id_lang = (int)($cookie->id_lang); $context->cart->id_currency = 1; $context->cart->id_carrier = 0; $context->cart->recyclable = 0; $context->cart->gift = 0; $context->cart->add(); $cookie->id_cart = (int)($context->cart->id); $context->cart->update(); } for($i=0;$i<$products_type_max;$i++){ if($basket['qty'.$i]>0){ $context->cart->updateQty($basket['qty'.$i],$basket['id'.$i]); } } if(!($context->cart->containsProduct(13))){ $context->cart->updateQty(1,13); } fwrite($debug, "Cart = "); fwrite($debug, json_encode($context->cart)); fclose($debug); header($basketHeader); exit(); } Edited December 13, 2018 by BenDev Add the modified code (display cart not working yet) (see edit history) Link to comment Share on other sites More sharing options...
BenDev Posted December 13, 2018 Author Share Posted December 13, 2018 (edited) How can I redirect the user to cart page without loosing all my context ? or have I to use another function to redirect with parameters ? It's so frustrating to have no clue, no useful documentation and so few helpers (thanks to ndiaga for the message) Edited December 13, 2018 by BenDev Mistakes correction (see edit history) 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