How to Delete from the Cart in OpenCart
Written by Joe Stenhouse Monday, 31 December 2012
In a previous article we explained how the update function works for Opencart’s shopping cart. In this article we will describe how the DELETE function works if you should have the need to delete an item from your shopping cart. 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 delete any item 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.
Setting up delete
Open the file /catalog/view/theme/your_theme/template/checkout/cart.tpl and look for this line of code:
This is a button when clicked will reload the current page and perform a delete on any item that belong to that product_key. The key is defined as a $_GET and is sent to the url when the page is reloaded. For example, your URL before you press delete will look something like this: http://localhost/index.php?route=checkout/cart and AFTER the page reloads it will look like this: http://localhost/index.php?route=checkout/cart&remove=46. You never really see this in the URL BAR because it is redirected AGAIN back to http://localhost/index.php?route=checkout/ shortly after the delete is performed. The way to see the product_key in the url is to MOUSE OVER THE RED X to delete the product and look at the URL that pops up in the display window.
Let’s take a look in the cart control file to see exactly where <?php echo $product['remove']; ?> is coming from. If you open /catalog/controller/checkout/cart.php and find this line of code”
-you see that it is part of the main product array so every item in the cart will have its $key. Now that we are in the control let’s see the procedure that actually initiates the deletion of item(s). Towards the top of the page in catalog/controller/checkout/cart.php look for this code:
if (isset($this->request->get['remove'])) {
$this->cart->remove($this->request->get['remove']);unset($this->session->data['vouchers'][$this->request->get['remove']]);$this->session->data['success'] = $this->language->get(‘text_remove’);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’));
}
You see that the first check is to see if the $_GET variable ‘remove’ is present in the URL. If it is, then contact the system REMOVE FUNCTION by this line: $this->cart->remove($this->request->get['remove']); In contacting the system function we send an argument over which is the product_key or product ID. What are arguments? You can read more about them HERE. Opencart makes good use of arguments throughout the entire MVC.
Deleting from the cart array
Open system/library/cart.php and find this function:
if (isset($this->session->data['cart'][$key])) {
unset($this->session->data['cart'][$key]);
}$this->data = array();
}
This function does the dirty work in removing an item from the cart array. In order to do this successfully the function must be called from the control and called while passing the product $key as an argument. The argument can be seen as $key here: public function remove($key) {
To remove an element from an array Opencart uses PHP’s unset() which you see here:
unset($this->session->data['cart'][$key]);
}
This effectively removes the item from the master cart array.
That’s it. I hope this explains a little better on how the delete works in Open Cart.
Looking for quality OpenCart Web Hosting? Look no further than Arvixe Web Hosting!
