user2142189
user2142189

Reputation: 41

Prestashop api - How to get the current cart contents

I'm new to Prestashop, I cant find examples anywhere of how to get the current cart contents. I can get a list of all carts, but how do I get the current users cart?

Upvotes: 4

Views: 7835

Answers (2)

rusly
rusly

Reputation: 1522

For PS 1.4.X you can get using getProducts()

$product_array = $this->getProducts();

print_r($product_array);

Example :

public function getSubTotal() {
    $product_array = $this->getProducts();

    foreach($product_array as $product_item) {
            $sub_total += $product_item['price'] * $product_item['cart_quantity'];
    }

    return $sub_total;
}

Upvotes: 0

Altaf Hussain
Altaf Hussain

Reputation: 5202

it is easy and simple. I am considering you are using PS 1.5.x

In controllers other than cart controller

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

or in an class

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

Now the $cart is an object, and it has all the current cart data.

You can also get the cart products by calling getProducts like below

 $cartProducts = $cart->getProducts();

Hope this will help.

Please note that code is not tested and is just a sample code for your idea.

Thank you

Upvotes: 11

Related Questions