Today we will discuss the process during add to cart in Opencart. The Opencart Cart processing is highly based on the Ajax Based.
In default Opencart, we can add the product from the product search page, product page, product manufacturer page, product special page, product compare page and product category pages.
Everywhere in Opencart except the product page for the particular product, it uses the add function which is in the catalog/view/javascript/common.js file which is already loaded during the header load initially.
<script src="catalog/view/javascript/common.js" type="text/javascript"></script>
Processes through Javascript:
When click to the Add to cart button first it calls the add function of common.js by passing the product id and the minimum quantity to sell for that product.
//it has onclick evnet which calls the add function of the common.js script file by passing the product id and it's minimum quantity <button type="button" onclick="cart.add('<?php echo $product['product_id']; ?>', '<?php echo $product['minimum']; ?>');"><i class="fa fa-shopping-cart"></i> <span class="hidden-xs hidden-sm hidden-md"><?php echo $button_cart; ?></span></button>
It sends the arguments the controller through the Post request
//Sends the Ajax request by post method and pass the product id and quantity as post data var cart = { 'add': function(product_id, quantity) { $.ajax({ url: 'index.php?route=checkout/cart/add', type: 'post', data: 'product_id=' + product_id + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1), dataType: 'json',
Processes at the Controller:
First, it validates the product id and quantity arguments by getting the product information of the product.
//add function of controller checkout/cart public function add() { $this->load->language('checkout/cart'); $json = array(); //Get the product id or set is as 0 if (isset($this->request->post['product_id'])) { $product_id = (int)$this->request->post['product_id']; } else { $product_id = 0; } $this->load->model('catalog/product'); //Get the product inforamtion to check the product minimum quantity with post quantity $product_info = $this->model_catalog_product->getProduct($product_id); if ($product_info) { if (isset($this->request->post['quantity']) && ((int)$this->request->post['quantity'] >= $product_info['minimum'])) { $quantity = (int)$this->request->post['quantity']; } else { $quantity = $product_info['minimum'] ? $product_info['minimum'] : 1; }
After that, it gets the options of that product and validate to the required options and recurring of the product if these are exist on the particular product.
//Getting the product option and validate for required options $product_options = $this->model_catalog_product->getProductOptions($this->request->post['product_id']); foreach ($product_options as $product_option) { if ($product_option['required'] && empty($option[$product_option['product_option_id']])) { $json['error']['option'][$product_option['product_option_id']] = sprintf($this->language->get('error_required'), $product_option['name']); } } if (isset($this->request->post['recurring_id'])) { $recurring_id = $this->request->post['recurring_id']; } else { $recurring_id = 0; } // Get the recurring profile and check for the recurring id if recurring exist on the product $recurrings = $this->model_catalog_product->getProfiles($product_info['product_id']); if ($recurrings) { $recurring_ids = array(); foreach ($recurrings as $recurring) { $recurring_ids[] = $recurring['recurring_id']; } if (!in_array($recurring_id, $recurring_ids)) { $json['error']['recurring'] = $this->language->get('error_recurring_required'); } }
If there are no error in $json array related to option and recurring it calls the add method of cart library which insert the product details and customer data to the Opencart cart table.
if (!$json) { $this->cart->add($this->request->post['product_id'], $quantity, $option, $recurring_id);
The controller returns the JSON response by encoding to the $json array:
$this->response->addHeader('Content-Type: application/json'); $this->response->setOutput(json_encode($json));
The json array can contain one of the following combination:
$json[‘error’] & $json[‘redirect’] : If gets error in required options or recurring id error
$json[‘success’] & $json[‘total’] : If add to the cart successfully
Processes after the Ajax request completed:
If it contains the redirect parameter which redirects to the product page of that product or else it will show the success message and load the html data by sending the request to the common/cart info method and jquery parse it with ul li.
success: function(json) { $('.alert, .text-danger').remove(); //If redirect parameter is in the json response then redirect to that location if (json['redirect']) { location = json['redirect']; } if (json['success']) { $('#content').parent().before('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">×</button></div>'); // Need to set timeout otherwise it wont update the total setTimeout(function () { $('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>'); }, 100); $('html, body').animate({ scrollTop: 0 }, 'slow'); // Load the cart inforamtion from common/cart in html form and parse it with ul li $('#cart > ul').load('index.php?route=common/cart/info ul li'); } }
The info method of controller common/cart return the all the products with the product details such as cart_id, name, model, options, price total etc.
4 comments
Thanks for sharing valuable comments.
Kindly stay connected for more such content. For further more assistance kindly share the details on [email protected].
Thanks and Regards.
Thanks for the suggestion and we will try to implement this for future articles.
For further queries, you can drop us a mail at [email protected] and we will for sure assist you.Thanks