6

In Prestashop, I've created a custom form where I show a list with all products and the user can fill the corresponding quantities. By submitting the form, I clear the cart and fill it with the new values and finally redirect to the checkout page.

Everything's working fine, but only when the cart already exists. In a case of an empty cart (cart_id==null), I cannot add the products. I tried with several ways to create a $cart but I didn't manage to do so.

I don't get any exceptions, the code is executed without errors, it's just that at the end, in the checkout page, the cart remains empty; I repeat, only when the cart was already empty. In the case of a cart with products in it, then the process is just perfect!

I would appreciate some help on this.

Here is my small controller that gets the products and quantities from the form and adds them in the cart:

include('../../config/config.inc.php');
include('../../header.php');

// Filling the array of products
$products = array();
foreach ($_POST as $key => $value)
    if (substr($key, 0, 9) === 'quantity_')
        $products[substr($key, 9)] = $value;

// First of all, we remove all products
$prods = $cart->getProducts();
foreach ($prods as $prod)
    $cart->deleteProduct($prod['id_product']);

// Adding the new products
foreach ($products as $product_id => $quantity)
    if ($quantity > 0)
        $cart->updateQty($quantity, $product_id);

// Redirecting to the checkout page.
header("Location: " . $_POST['redirect']);
exit();`

Thank you in advance!

3 Answers3

11

I had the same problem with Prestashop not correctly creating a new cart upon calling the CartCore in many different ways - neither context or direct calls did work out.

Found this gem from someone else over here:

// get cart id if exists
if ($this->context->cookie->id_cart)
{
    $cart = new Cart($this->context->cookie->id_cart);
}

// create new cart if needed
if (!isset($cart) OR !$cart->id)
{
    $cart = new Cart();
    $cart->id_customer = (int)($this->context->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)($this->context->cookie->id_lang);
    $cart->id_currency = (int)($this->context->cookie->id_currency);
    $cart->id_carrier = 1;
    $cart->recyclable = 0;
    $cart->gift = 0;
    $cart->add();
    $this->context->cookie->id_cart = (int)($cart->id);    
    $cart->update();
}

This is working for me now. As you see it goes a little more in depth than just asking the context to retrieve or create a new cart. hth.

Community
  • 1
  • 1
cyrez
  • 111
  • 1
  • 3
  • 1
    i tried this but as the `$cart->id` is null , so it doesn't work – Master Yoda Aug 24 '15 at 10:09
  • Did not work for me but this did: https://stackoverflow.com/questions/24610116/prestashop-add-customized-product-to-cart – Michael Käfer Dec 04 '17 at 17:50
  • This WORKS. It create a new cart in the current context and cookie with the last id+1, however it doesn't create the cart in the database UNTIL you add a product in your cart, with the same created cart_id. – moghwan Jun 14 '22 at 11:45
4

You probably need to include this line when you are outside of a class

$context = Context::getContext();

And then add a cart variable that defines cart attributes ( in ur case assigns a id )

$cart = $context->cart; // gets the cart id or creates a new one 

Should work fine now

BR's ( dont forget to accept the answer if it helps you :) )

user2831723
  • 832
  • 12
  • 25
  • I've already tried it. Still it doesn't work. As I wrote, the problem seems to exist when the cart is empty (id_cart==NULL); if the cart has already products, then it works just fine. The problem seems to be at the correct initialization of an empty cart. – Stratos Nikolaidis Feb 15 '14 at 10:08
  • As I said this works just fine. Im using it myself. ( inserting products from a custom module in to a empty cart ) There must be something else wrong then. And your code can't works icne you are not defining your $cart variable at first place. Just look at examples in product.php. – user2831723 Feb 15 '14 at 11:24
1

As a complement to previous posts.

If you set that configuration : Configuration::updateValue('PS_CART_FOLLOWING', 1) (display the last cart of the customer instead of creating a new one each time).

You will always get the cart id $this->context->cart->id at NULL when trying to create a new cart at the customer's first log in (after creating a new account) using the following code:

if (is_null($this->context->cart)) {

    $this->context->cart = 
            new Cart($this->context->cookie->id_cart);
}

I manage to solve the problem by adding some code to after the invalid cart creation.

/**
* Manage the possible errors when trying to create 
*    a new cart for a customer.
*
* The new cart is saved in the current context ($this->context->cart).
*/
private function createCart()
{
    if (is_null($this->context->cart)) {

        $this->context->cart = 
            new Cart($this->context->cookie->id_cart);
    }

    if (is_null($this->context->cart->id_lang)) {
         $this->context->cart->id_lang = $this->context->cookie->id_lang;
    }

    if (is_null($this->context->cart->id_currency)) {
         $this->context->cart->id_currency = $this->context->cookie->id_currency;
    }

    if (is_null($this->context->cart->id_customer)) {
         $this->context->cart->id_customer = $this->context->cookie->id_customer;
    }

    if (is_null($this->context->cart->id_guest)) {

        if (empty($this->context->cookie->id_guest)){
            $this->context->cookie->__set(
                'id_guest', 
                Guest::getFromCustomer($this->context->cookie->id_customer)
            );
        }
        $this->context->cart->id_guest = $this->context->cookie->id_guest;
    }

    if (is_null($this->context->cart->id)) {

         $this->context->cart->add();

         $this->context->cookie->__set('id_cart', $this->context->cart->id);
    }
}

After initializing the cart that way, I'm now able to add products to the cart with no problem.

Good success everyone