Updating the Cart in OpenCart

In a previous article we gave you an in depth look at how to add to your cart and how that reflects on both ends, the back end and the front end. This article will tell you how the cart updates if you should want to edit any existing cart contents. A page that you can perform this edit would be the main shopping cart page located at index.php?route=checkout/cart

The Cart Array

In order to update any item(s) in the cart there must already be items present.  If you attempt to travel to the shopping cart page with an empty cart it will give you a warning along with a way to get back to the home page. The cart array is always present, but must not be empty. If you are interested in how the cart array is established you can go over this in the previous article.

Performing Update/Edit

Open the file /catalog/view/theme/your_theme/template/checkout/cart.tpl and look for this line of code:

<form action=”<?php echo $action; ?>” method=”post” enctype=”multipart/form-data”>

When user clicks the update button, the form action is called: <?php echo $action; ?>  If you open /catalog/controller/checkout/cart.php you can view the action if you search for this line of code:

$this->data['action'] = $this->url->link(‘checkout/cart’);

This is a command to reload the current page while sending the HTTP_POST (along with all post variables).

Let’s locate those post fields being sent with the form in catalog/view/theme/your_theme/template/checkout/cart.tpl here:

<input type=”text” name=”quantity[<?php echo $product['key']; ?>]” value=”<?php echo $product['quantity']; ?>” size=”1″ />
&nbsp;
<input type=”image” src=”catalog/view/theme/default/image/update.png” alt=”<?php echo $button_update; ?>” title=”<?php echo $button_update; ?>” />

As you can see, the quantity input field is set up to be passed as an array where the key is the product_key and the value is the quantity. This in fact is how array data is passed in HTTP POST. Now that we have our POST VARIABLES established, let’s see php that deals with them. Again, open /catalog/controller/checkout/cart.php and find this code:

// Update
if (!empty($this->request->post['quantity'])) {
foreach ($this->request->post['quantity'] as $key => $value) {
$this->cart->update($key, $value);
}unset($this->session->data['shipping_method']);
unset($this->session->data['shipping_methods']);
unset($this->session->data['payment_method']);
unset($this->session->data['payment_methods']);
unset($this->session->data['reward']);$this->redirect($this->url->link(‘checkout/cart’));
}

This procedure checks to see if the quantity post field is NOT EMPTY, and if not empty, update the cart. As we explained before, the post variable quantity field is an ARRAY so we must iterate through the array to get the product_key and the quantity. We do that here:

foreach ($this->request->post['quantity'] as $key => $value) {
$this->cart->update($key, $value);
}

We now will focus on the system function that is contacted in order to update the cart array.  This function is called update() that is found in /system/library.cart.php

Search for the following function:

public function update($key, $qty) {
if ((int)$qty && ((int)$qty > 0)) {
$this->session->data['cart'][$key] = (int)$qty;
} else {
$this->remove($key);
}$this->data = array();
}

Remember in the control we call the function like this: $this->cart->update($key, $value); (placing two arguments $key and $value)

The very first line of the function you can see the two arguments public function update($key, $qty) {

All this function is doing is finding a key position in the cart array and updating the quantity with the following line: $this->session->data['cart'][$key] = (int)$qty;

OR

it’s removing the key all together if the $quantity is not an INTEGER or is Zero.

} else {
$this->remove($key);
}

That’s it. Since the page is being reloaded…the cart will reflect the new changes.

Note: the quantity field should only be set up for numbers however OpenCart has not yet changed this so if you place any character other than a number it counts as a zero and removes the item completely.

Looking for quality OpenCart Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , | Posted under OpenCart | RSS 2.0

Author Spotlight

Joe Stenhouse

Joe Stenhouse

I am a web application developer that specializes in PHP, JAVASCRIPT, MYSQL, HTML, and CSS. We manifest exciting potentials within the world wide web through means of innovation.

Leave a Reply

Your email address will not be published. Required fields are marked *


× 2 = 16

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>