Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new Prestashop cart in custom php

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!

like image 739
Stratos Nikolaidis Avatar asked Feb 14 '14 00:02

Stratos Nikolaidis


3 Answers

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.

like image 151
cyrez Avatar answered Sep 30 '22 11:09

cyrez


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 :) )

like image 37
user2831723 Avatar answered Sep 30 '22 10:09

user2831723


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

like image 38
Jonathan Parent Lévesque Avatar answered Sep 30 '22 11:09

Jonathan Parent Lévesque