How to add a product to the basket programmatically in prestashop

I am using prestashop 1.5.3 , and I am developing a problem with a payment gateway, so I could not find how to add the product programmatically to the cart and order it to add a fee for payment.

Please help me

+4
source share
4 answers

Below is the code to add multiple products programmatically. It can be used to add one product. Put this code in a file called test.php on your root site, and then run it like this: /test.php? Products_ids = 11,9,10, where 11, 9,10 are 3 products. Hope this helps.

 <?php require(dirname(__FILE__).'/config/config.inc.php'); $context=Context::getContext();//new Cart(); $id_cart=$context->cookie->__get('id_cart'); $products_ids=$_GET['products_ids']; // comma seprated products id example : test.php?products_ids=1,2,3 $products_ids_array=explode(",",$products_ids); if(count($products_ids_array)>0){ $cart=new Cart($id_cart); $cart->id_currency=2; $cart->id_lang=1; foreach($products_ids_array as $key=>$id_product){ $cart->updateQty(1, $id_product); } } ?> 
+3
source

If you are developing a payment module, you should first check how other payment modules are created, for example, the Ogone or Paypal module. You can find them here: https://github.com/PrestaShop/PrestaShop-modules

The method used by preashop to add / remove products from the basket is Cart-> updateQty () (in the file classes / Cart.php).

+2
source

you can put this code in a php file in your root directory and use a simple form directing to this page containing the product identifier and quantity.

Just change:

 $idProduct= 19825 to $idProduct=$_POST["txtproductid"] $qty=5 to $qty=$_POST["txtqty"]; $useSSL = true; include('/config/config.inc.php'); include('/header.php'); global $params; $errors = array(); $idProduct =19825; $qty=5; if ($cookie->isLogged()) { /* Cart already exists */ if ((int)$cookie->id_cart) { $cart = new Cart((int)$cookie->id_cart); } if (!isset($cart) OR !$cart->id) { $cart = new Cart(); $cart->id_customer = (int)($cookie->id_customer); $cart->id_address_delivery = (int) (Address::getFirstCustomerAddressId($cart->id_customer)); $cart->id_address_invoice = $cart->id_address_delivery; $cart->id_lang = (int)($cookie->id_lang); $cart->id_currency = (int)($cookie->id_currency); $cart->id_carrier = 1; $cart->recyclable = 0; $cart->gift = 0; $cart->add(); $cookie->id_cart = (int)($cart->id); } /* get product id and product attribure id */ $data = explode(",", $product); $idProduct = $data[0]; */ $idProductAttribute = $data[1]; if ($qty != '') { $producToAdd = new Product((int)($idProduct), true, (int)($cookie->id_lang)); if ((!$producToAdd->id OR !$producToAdd->active) AND !$delete) /* Product is no longer available, skip product */ continue; /* Check the quantity availability */ if ($idProductAttribute > 0 AND is_numeric($idProductAttribute)) { if (!$producToAdd->isAvailableWhenOutOfStock($producToAdd->out_of_stock) AND !Attribute::checkAttributeQty((int)$idProductAttribute, (int)$qty)) { /* There is not enough product attribute in stock - set customer qty to current stock on hand */ $qty = getAttributeQty($idProductAttribute); } } elseif (!$producToAdd->checkQty((int)$qty)) /* There is not enough product in stock - set customer qty to current stock on hand */ $qty = $producToAdd->getQuantity(idProduct); $updateQuantity = $cart->updateQty((int)($qty), (int)($idProduct), (int)($idProductAttribute), NULL, 'up'); $cart->update(); } /* redirect to cart if (!sizeof($errors)) */ Tools::redirect('order.php'); } else { Tools::redirect('/index.php'); } $smarty->assign(array( 'id_customer' => (int)($cookie->id_customer), 'errors' => $errors )); include_once('/footer.php'); 
+2
source
 <script> $(document).ready(function(){ $.ajax({ type: 'POST', headers: { "cache-control": "no-cache" }, url: 'yourshopurl', async: true, cache: false, dataType: 'json', //( (parseInt(idCombination) && idCombination != null) ? '&ipa=' + parseInt(idCombination): '') data: 'controller=cart&add=1&ajax=true&qty=1&id_product=247&token=' + static_token , success: function(jsonData) { console.log("products added"); } }); }); </script> 

Now just add the product id ... or any combinations (commented out)

0
source

Source: https://habr.com/ru/post/1489110/


All Articles