This is the documentation for concrete5 version 5.6 and earlier. View Current Documentation

If you'd like to display the total number of products in your cart and provide a link to your cart, eCommerce includes a handy Cart Links block that you can easily add to any area on your page.

However, if you'd like to access the cart data directly and implement it into a custom component for your site, the following code might help you get started.

Note: this how-to assumes your site has the eCommerce add-on installed. There are no checks in place to ensure that it is. Blindly inserting this code into a theme that doesn't have eCommerce installed will result in errors.

Let's first load the resources we need. This is best kept out of the view layer, perhaps placed in a block controller or single page controller and passed back to the view as values.

// load the cart model
Loader::model('cart', 'core_commerce');

// create a variable named cart and call the get method, which returns the current order object from our session
$cart = CoreCommerceCurrentOrder::get();

Then we'll call a couple different methods on the cart object:

// get the total number of products in the cart
$products = $cart->getTotalProducts();

// get the order total 
$total = $cart->getBaseOrderDisplayTotal();

Once we've done that, we'll take the data returned to our view layer and display it:

// display the total number of products in the cart if greater than 0
if ($products > 0 ) {
    echo "Items in cart: " . $products;
}else{
    echo "Your cart is empty.";
}

Note that this also renders our currency smybol and formatting:

// display the order total 
echo "Order total: " . $total;

Then, let's make a convenient link to the cart itself:

<a href="<?php echo $this->url('cart');?>">View cart ยป</a>

Now let's see how it turned out. (I've omitted some php tags and br elements from the code above, for the sake of simplicity.) When our cart is empty:

empty cart

Let's add a product to the cart:

full cart

It's that easy.

Loading Conversation