Post by glolar » Sun May 13, 2018 9:07 am

Hello all,

I have an old site on Version 1.4.9.3 of OpenCart, and I need a bit of custom code to do this:

1. If a customer adds product with model number = 'DER-ST2-BLK' to the cart, and they try to order more than a quantity of 10, I want to display an error message in the cart: "Products marked with *** have a maximum order quantity of 10!"
2. Prevent the customer from going to checkout until they reduce the quantity to 10 or less.

So far, I have been able to figure out how to display the error message (See the 2nd attachment below). However, I cannot figure out how to prevent the customer from navigating to Checkout (by clicking the Checkout button). I need the same behavior as when a customer tries to order a quantity of product greater than the quantity in stock. I added the following code to /store/catalog/language/english/checkout/cart.php to handle the error message:

$_['error_max_quantity'] = 'Products marked with *** have a maximum order quantity of 10!';

I also updated the code in /store/catalog/controller/checkout/cart.php. The added parts of code are commented, so you can easily see what I added:

// Added the following line of code to check if order of DER-ST2-BLK exceeds maximum allowable. Larry Sanford [05/12/2018]
$exceeded_max_quantity = false;
//
foreach ($this->cart->getProducts() as $result) {
$option_data = array();

foreach ($result['option'] as $option) {
$option_data[] = array(
'name' => $option['name'],
'value' => $option['value']
);
}

if ($result['image']) {
$image = $result['image'];
} else {
$image = 'no_image.jpg';
}

$this->data['products'][] = array(
'key' => $result['key'],
'name' => $result['name'],
'model' => $result['model'],
'thumb' => $this->model_tool_image->resize($image, $this->config->get('config_image_cart_width'), $this->config->get('config_image_cart_height')),
'option' => $option_data,
'quantity' => $result['quantity'],
'stock' => $result['stock'],
'price' => $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'))),
'total' => $this->currency->format($this->tax->calculate($result['total'], $result['tax_class_id'], $this->config->get('config_tax'))),
'href' => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $result['product_id'])
);

// Added new code (following 5 lines) to check if order of DER-ST2-BLK exceeds maximum allowable. Larry Sanford [05/12/2018]
if ($this->data['error_warning'] == '') {
if (($result['model'] == 'DER-ST2-BLK') && ($result['quantity'] > 10)) {
$exceeded_max_quantity = true;
}
}
// End of newly added code to check if order of DER-ST2-BLK exceeds maximum allowable.
}

// Added new code (following 3 lines) to check if order of DER-ST2-BLK exceeds maximum allowable. Larry Sanford [05/12/2018]
if($exceeded_max_quantity){
$this->data['error_warning'] = $this->language->get('error_max_quantity');
}
// End of newly added code to check if order of DER-ST2-BLK exceeds maximum allowable.

So, as you can see above, I initialize a variable ($exceeded_max_quantity) to false, then while looping thru the products in the cart, if I find one with the desired model number (DER-ST2-BLK), and the quantity is greater than 10, I set the $exceeded_max_quantity = true. After the loop, if the variable $exceeded_max_quantity = true, I set the error message, and it gets displayed. See the first attachment for a screen capture showing the error message "Products marked with *** have a maximum order quantity of 10!". The problem is that the product in the cart does not get the three asterisks displayed next to it, and the customer is allowed to click the Checkout button and advance to the Checkout screens.

The first attached screen capture shows the error message displayed when a customer tries to order more than the number of products available in stock. In this case, even if the customer clicks the Checkout button, they do NOT get taken to the Checkout screen until they rectify the error. That's how I want it to work if the customer orders more than 10 of product DER-ST2-BLK.

Thanks in advance for any help!

Attachments

opencart_2.jpg

Error shown when customer tries to order more than quantity in stock. - opencart_2.jpg (239.5 KiB) Viewed 188 times

opencart_1.jpg

Desired error if customer orders more than 10. - opencart_1.jpg (244.88 KiB) Viewed 188 times


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 synapseindia » Wed May 16, 2018 4:53 pm

You can disable the checkout button on cart.tpl file .
For example - if($exceeded_max_quantity=='true' ) {
Button attribute should be disabled="disabled"
}

User avatar
Active Member

Posts

Joined
Thu Apr 05, 2018 2:27 pm
Who is online

Users browsing this forum: Bing [Bot] and 15 guests