On the product page, if the customer tries to add 2 items to the cart, and there is only 1 item in stock, I want to NOT add any items to the cart, and I also want to display (in red) the message "Insufficient quantity in stock: You have selected a quantity greater than the quantity available in stock!".
I have successfully prevented the items from being added to the cart, but the red error message is not being displayed. Below are the coding changes I have made. Hopefully, someone can tell me what I am doing wrong that is preventing the red error message from being displayed:
First, I added the following to the /store/catalog/language/en-gb/checkout/cart.php file:
Code: Select all
$_['error_insufficient_stock'] = 'Insufficient quantity in stock: You have selected a quantity greater than the quantity available in stock!';
Code: Select all
$quantity_in_stock = $product_info['quantity'];
if ($quantity > $quantity_in_stock) {
$json['error'] = $this->language->get('error_insufficient_stock');
}
Code: Select all
if (!$json) {
$this->cart->add($this->request->post['product_id'], $quantity, $option, $recurring_id);
$json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('checkout/cart'));
// Unset all shipping and payment methods
unset($this->session->data['shipping_method']);
unset($this->session->data['shipping_methods']);
unset($this->session->data['payment_method']);
unset($this->session->data['payment_methods']);
// Totals
$this->load->model('setting/extension');
$totals = array();
$taxes = $this->cart->getTaxes();
$total = 0;
// Because __call can not keep var references so we put them into an array.
$total_data = array(
'totals' => &$totals,
'taxes' => &$taxes,
'total' => &$total
);
// Display prices
if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
$sort_order = array();
$results = $this->model_setting_extension->getExtensions('total');
foreach ($results as $key => $value) {
$sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order');
}
array_multisort($sort_order, SORT_ASC, $results);
foreach ($results as $result) {
if ($this->config->get('total_' . $result['code'] . '_status')) {
$this->load->model('extension/total/' . $result['code']);
// We have to put the totals in an array so that they pass by reference.
$this->{'model_extension_total_' . $result['code']}->getTotal($total_data);
}
}
$sort_order = array();
foreach ($totals as $key => $value) {
$sort_order[$key] = $value['sort_order'];
}
array_multisort($sort_order, SORT_ASC, $totals);
}
$json['total'] = sprintf($this->language->get('text_items'), $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0), $this->currency->format($total, $this->session->data['currency']));
// The logic in the following else statement is being executed, but the error message is NOT being displayed
} else {
$json['redirect'] = str_replace('&', '&', $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']));
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
I have verified that the correct language file is being used, and that my new code is being executed, but there must be something wrong with my syntax that is preventing the red error message from being displayed at the top of the product page.
Any help is greatly appreciated!