Create a new Prestashop cart in custom php

In Prestashop, I created a custom form in which I display a list with all the products, and the user can fill in the appropriate quantities. By submitting the form, I empty the basket and fill it with new values ​​and finally redirect to the verification page.

Everything works fine, but only when the basket already exists. In the case of an empty basket (cart_id == null) I can not add products. I tried several ways to create a $ basket, but I failed.

I do not get any exceptions, the code runs without errors, it is just that in the end, on the check page, the basket remains empty; I repeat, only when the cart was already empty. In the case of a cart with products in it, the process is just perfect!

I would appreciate help on this.

Here is my little controller that receives products and quantities from the form and adds them to 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!

+4
source share
3 answers

I had the same problem that Prestashop did not correctly create a new cart when calling CartCore in different ways - neither contextual nor direct calls worked.

Found this stone from someone else 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();
}

Now it works for me. As you see, this goes a little deeper than just asking for context to retrieve or create a new cart. NTN.

+7
source

You probably need to include this line if you are outside the class

$context = Context::getContext();

, ( )

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

BR ( , :))

+4

.

: Configuration::updateValue('PS_CART_FOLLOWING', 1) ( , ).

$this->context->cart->id NULL ( ), :

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

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

, .

/**
* 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);
    }
}

.

+1

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


All Articles