Post by glolar » Sat Jun 19, 2021 5:07 am

Hello all, question for OpenCart 3.0.3.2

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!';
Next, I have added the following to the add() function in /store/catalog/controller/checkout/cart.php, right before the line that checks the $json variable:

Code: Select all

$quantity_in_stock = $product_info['quantity'];
if ($quantity > $quantity_in_stock) {
		$json['error'] = $this->language->get('error_insufficient_stock');
}
Right after my added code, there is existing code that checks to see if there is anything in $json (e.g., if there is an error). If there is NOT, the items are added to the cart. If there IS something in $json, the error message should be displayed. I am including the entire code block here for clarity, but the important code is in the final else block:

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!

Increase Your Child's I.Q.
iPad Wallpapers
Turtle & Tortoise Screen Savers


User avatar
Active Member

Posts

Joined
Thu Jul 29, 2010 12:35 pm
Location - San Diego, CA

Post by khnaz35 » Sat Jun 19, 2021 3:44 pm

I didn't see any code you are adding to product.twig
Am I missing something?

Urgent Questions shoot here: khnaz35@gmail.com
Enjoy nature ;) :) :-*


User avatar
Active Member

Posts

Joined
Mon Aug 27, 2018 11:30 pm
Location - Malaysia

Post by glolar » Sat Jun 19, 2021 7:23 pm

khnaz35 wrote:
Sat Jun 19, 2021 3:44 pm
I didn't see any code you are adding to product.twig
Am I missing something?
I am probably the one missing something. I assumed (maybe wrongly) that since the twig code MUST be there for the success message, it would cover any error message also, since the error message would appear exactly where the success message does. Which twig file should I be looking at?

Thank you!

Increase Your Child's I.Q.
iPad Wallpapers
Turtle & Tortoise Screen Savers


User avatar
Active Member

Posts

Joined
Thu Jul 29, 2010 12:35 pm
Location - San Diego, CA

Post by glolar » Sun Jun 20, 2021 8:21 am

Yes, that is EXACTLY what I need.

I looked at the XML file to see what changes the extension makes, and they are the same ones I made manually. I reverted my manual changes, installed the extension, and refreshed all cache, and everything now works as needed. I think maybe my manual .twig file changes didn't work (caching issue), but since the extension works, that's great.

Thank you VERY MUCH for your help!

Increase Your Child's I.Q.
iPad Wallpapers
Turtle & Tortoise Screen Savers


User avatar
Active Member

Posts

Joined
Thu Jul 29, 2010 12:35 pm
Location - San Diego, CA

Post by paulfeakins » Mon Jun 21, 2021 7:51 pm

glolar wrote:
Sun Jun 20, 2021 8:21 am
Yes, that is EXACTLY what I need.
Great, please add [SOLVED] to the start of the post title.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom
Who is online

Users browsing this forum: No registered users and 82 guests