DESCRIPTION :
This HOW TO contains a complete guide to adding the facility to implement MINIMUM ORDER QUANTITY per Product on your OpenCart Site.
Since my version of OpenCart has now moved considerably away from the standard, I have included all the changes to be made as text within this file. It means that you will need to do some manual editing rather than just copying in replacement files, but it will be easier for those of us with heavily customised setups.
Differences have been identified using WinMERGE, and the appropriate parts of the code have been cut from the working version and then pasted into this document. This hopefully means that the scope for mistakes has been much reduced (?)
If you have not yet discovered WinSCP - this may well be a good opportunity !
BACKUP:
Ensure that you make back-up copies of the files you are about to change, it is very easy to make mistakes while editing !
The files to backup are :
\admin\controller\product.php
\admin\language\english\controller\product.php
\admin\template\default\content\product.tpl
\catalog\controller\cart.php
\catalog\controller\product.php
\catalog\language\english\controller\cart.php
\catalog\language\english\controller\product.php
\catalog\template\default\content\cart.tpl
\catalog\template\default\content\product.tpl
\library\cart\cart.php
INSTALL:
1) Create a new column in your product table by running this SQL script
Code: Select all
ALTER TABLE `product` ADD `min_order` int(4) NOT NULL default '1' AFTER `weight_class_id`;
In function insert() : change
Code: Select all
if ( ( $request->isPost()) && ( $this->validateForm())) {
$sql = "insert into product set quantity = '?', date_available = '?', model = '?', manufacturer_id = '?', image_id = '?', shipping = '?', price = '?', sort_order = '?', weight = '?', weight_class_id = '?', status = '?', tax_class_id = '?', date_added = now()";
$database->query($database->parse($sql, $request->get('quantity', 'post'), date('Y-m-d', strtotime($request->get('date_available_year', 'post') . '/' . $request->get('date_available_month', 'post') . '/' . $request->get('date_available_day', 'post'))), $request->get('model', 'post'), $request->get('manufacturer_id', 'post'), $request->get('image_id', 'post'), $request->get('shipping', 'post'), $request->get('price', 'post'), $request->get('sort_order', 'post'), $request->get('weight', 'post'), $request->get('weight_class_id', 'post'), $request->get('status', 'post'), $request->get('tax_class_id', 'post')));
$insert_id = $database->getLastId();
Code: Select all
if ( ( $request->isPost()) && ( $this->validateForm())) {
$sql = "insert into product set quantity = '?', date_available = '?', model = '?', manufacturer_id = '?', image_id = '?', shipping = '?', price = '?', sort_order = '?', weight = '?', weight_class_id = '?', status = '?', tax_class_id = '?', min_order = '?', date_added = now()";
$database->query($database->parse($sql, $request->get('quantity', 'post'), date('Y-m-d', strtotime($request->get('date_available_year', 'post') . '/' . $request->get('date_available_month', 'post') . '/' . $request->get('date_available_day', 'post'))), $request->get('model', 'post'), $request->get('manufacturer_id', 'post'), $request->get('image_id', 'post'), $request->get('shipping', 'post'), $request->get('price', 'post'), $request->get('sort_order', 'post'), $request->get('weight', 'post'), $request->get('weight_class_id', 'post'), $request->get('status', 'post'), $request->get('tax_class_id', 'post'), $request->get('min_order', 'post')));
$insert_id = $database->getLastId();
Code: Select all
if ( ( $request->isPost()) && ( $this->validateForm())) {
$sql = "update product set quantity = '?', date_available = '?', model = '?', manufacturer_id = '?', image_id = '?', shipping = '?', price = '?', sort_order = '?', weight = '?', weight_class_id = '?', status = '?', tax_class_id = '?', date_modified = now() where product_id = '?'";
$database->query($database->parse($sql, $request->get('quantity', 'post'), date('Y-m-d', strtotime($request->get('date_available_year', 'post') . '/' . $request->get('date_available_month', 'post') . '/' . $request->get('date_available_day', 'post'))), $request->get('model', 'post'), $request->get('manufacturer_id', 'post'), $request->get('image_id', 'post'), $request->get('shipping', 'post'), $request->get('price', 'post'), $request->get('sort_order', 'post'), $request->get('weight', 'post'), $request->get('weight_class_id', 'post'), $request->get('status', 'post'), $request->get('tax_class_id', 'post'), $request->get('product_id')));
$database->query("delete from product_description where product_id = '" . ( int ) $request->get('product_id') . "'");
Code: Select all
if ( ( $request->isPost()) && ( $this->validateForm())) {
$sql = "update product set quantity = '?', date_available = '?', model = '?', manufacturer_id = '?', image_id = '?', shipping = '?', price = '?', sort_order = '?', weight = '?', weight_class_id = '?', status = '?', tax_class_id = '?', min_order = '?', date_modified = now() where product_id = '?'";
$database->query($database->parse($sql, $request->get('quantity', 'post'), date('Y-m-d', strtotime($request->get('date_available_year', 'post') . '/' . $request->get('date_available_month', 'post') . '/' . $request->get('date_available_day', 'post'))), $request->get('model', 'post'), $request->get('manufacturer_id', 'post'), $request->get('image_id', 'post'), $request->get('shipping', 'post'), $request->get('price', 'post'), $request->get('sort_order', 'post'), $request->get('weight', 'post'), $request->get('weight_class_id', 'post'), $request->get('status', 'post'), $request->get('tax_class_id', 'post'), $request->get('min_order', 'post'), $request->get('product_id')));
$database->query("delete from product_description where product_id = '" . ( int ) $request->get('product_id') . "'");
Code: Select all
$view->set('entry_min_order', $language->get('entry_min_order'));
Code: Select all
$view->set('entry_discount', $language->get('entry_discount'));
Code: Select all
if ( $request->has('min_order', 'post')) {
$view->set('min_order', $request->get('min_order', 'post'));
}
else {
$view->set('min_order', @ $product_info['min_order']);
}
Code: Select all
if ( $request->has('sort_order', 'post')) {
$view->set('sort_order', $request->get('sort_order', 'post'));
}
else {
$view->set('sort_order', @ $product_info['sort_order']);
}
INSERT
Code: Select all
$_['entry_min_order'] = 'Minimum Order Quantity:';
Code: Select all
$_['entry_weight'] = 'Weight:';
INSERT
Code: Select all
<tr>
<td><?php echo $entry_min_order; ?></td>
<td><input name="min_order" value="<?php echo $min_order; ?>" size="2" /></td>
</tr>
Code: Select all
<tr>
<td><?php echo $entry_quantity; ?></td>
<td><input name="quantity" value="<?php echo $quantity; ?>" size="2" /></td>
</tr>
5) Edit \catalog\controller\cart.php
In function index() : INSERT
Code: Select all
$view->set('column_min_order', $language->get('column_min_order'));
Code: Select all
$view->set('column_min_order', $language->get('column_min_order'));
Code: Select all
$min_error = '0';
Code: Select all
$product_data = array( );
Code: Select all
$product_data[] = array( 'key' => $result['key'], 'name' => $result['name'], 'model' => $result['model'], 'thumb' => $image->resize($result['image'], 50, 50), 'option' => $option_data, 'quantity' => $result['quantity'], 'stock' => $result['stock'], 'price' => $currency->format($tax->calculate($result['price'], $result['tax_class_id'], $config->get('config_tax'))), 'discount' => ( $result['discount'] ? $currency->format($tax->calculate($result['price'] - $result['discount'], $result['tax_class_id'], $config->get('config_tax'))) : null ), 'total' => $currency->format($tax->calculate($result['total'], $result['tax_class_id'], $config->get('config_tax'))), 'href' => $url->href('product', false, array( 'product_id' => $result['product_id'] )));
Code: Select all
$line_min_error = '0';
if ($result['quantity'] < $result['min_order']) {
$result['quantity'] = $result['min_order'];
$min_error = '1';
$line_min_error = '1';
}
$product_data[] = array( 'key' => $result['key'], 'name' => $result['name'], 'model' => $result['model'], 'thumb' => $image->resize($result['image'], 50, 50), 'option' => $option_data, 'quantity' => $result['quantity'], 'min_order' => $result['min_order'], 'min_error' => $line_min_error, 'stock' => $result['stock'], 'price' => $currency->format($tax->calculate($result['price'], $result['tax_class_id'], $config->get('config_tax'))), 'discount' => ( $result['discount'] ? $currency->format($tax->calculate($result['price'] - $result['discount'], $result['tax_class_id'], $config->get('config_tax'))) : null ), 'total' => $currency->format($tax->calculate($result['total'], $result['tax_class_id'], $config->get('config_tax'))), 'href' => $url->href('product', false, array( 'product_id' => $result['product_id'] )));
if ($min_error == '1') {
$view->set('error', $language->get('error_min_order'));
}
In function index() : change
Code: Select all
$cart->add($request->get('product_id', 'post'), '1', $request->get('option', 'post'));
Code: Select all
$cart->add($request->get('product_id', 'post'), $request->get('quantity', 'post'), $request->get('option', 'post'));
Code: Select all
$view->set('text_min_order', $language->get('text_min_order'));
$view->set('min_order', $product_info['min_order']);
Code: Select all
$view->set('price', $currency->format($tax->calculate($product_info['price'], $product_info['tax_class_id'], $config->get('config_tax'))));
INSERT
Code: Select all
$_['column_min_order'] = 'Min.';
Code: Select all
$_['column_quantity'] = 'Quantity';
Code: Select all
$_['error_min_order'] = 'Products marked with ### have had their Quantity reset to the Minimum Order Quantity!';
Code: Select all
$_['error_stock'] = 'Products marked with *** are not available in the desired quantity or not in stock!';
INSERT
Code: Select all
$_['text_min_order'] = 'Minimum Order Quantity for this product is: ';
Code: Select all
$_['text_error'] = 'Product not found!';
INSERT
Code: Select all
<th class="f"><?php echo $column_min_order; ?></th>
Code: Select all
<th class="f"><?php echo $column_quantity; ?></th>
Code: Select all
<?php if ($product['min_error'] == '1') { ?>
<span>###</span>
<?php } ?>
Code: Select all
<td class="j"><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a>
Code: Select all
<td class="l"><?php echo $product['min_order']; ?></td>
Code: Select all
<td class="l"><input type="text" name="quantity[<?php echo $product['key']; ?>]" value="<?php echo $product['quantity']; ?>" size="3" /></td>
INSERT
Code: Select all
<?php if ($min_order > '1') { ?>
<p><b><?php echo $text_min_order; ?><?php echo $min_order; ?></b></p>
<?php } ?>
Code: Select all
<?php echo $description; ?>
Code: Select all
<input type="hidden" name="quantity" value="<?php echo $min_order; ?>" />
Code: Select all
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>" />
In function __construct
change
Code: Select all
$this->products[$key] = array( 'key' => $key, 'product_id' => $product['product_id'], 'name' => $product['name'], 'model' => $product['model'], 'shipping' => $product['shipping'], 'image' => $product['filename'], 'option' => $option_data, 'download' => $download_data, 'quantity' => $quantity, 'stock' => ( $quantity <= $product['quantity'] ), 'price' => ( $product['price'] + $option_price ), 'discount' => $discount, 'total' => ( ( $product['price'] + $option_price ) - $discount ) * $quantity, 'tax_class_id' => $product['tax_class_id'], 'weight' => $product['weight'], 'weight_class_id' => $product['weight_class_id'] );
Code: Select all
$this->products[$key] = array( 'key' => $key, 'product_id' => $product['product_id'], 'name' => $product['name'], 'model' => $product['model'], 'shipping' => $product['shipping'], 'image' => $product['filename'], 'option' => $option_data, 'download' => $download_data, 'quantity' => $quantity, 'min_order' => $product['min_order'], 'stock' => ( $quantity <= $product['quantity'] ), 'price' => ( $product['price'] + $option_price ), 'discount' => $discount, 'total' => ( ( $product['price'] + $option_price ) - $discount ) * $quantity, 'tax_class_id' => $product['tax_class_id'], 'weight' => $product['weight'], 'weight_class_id' => $product['weight_class_id'] );
Code: Select all
function add($product_id, $qty = '1', $options = array( )) {
Code: Select all
function add($product_id, $qty, $options = array( )) {
Code: Select all
$this->data[$key] += $qty;
Code: Select all
$this->data[$key] += 1;
In Catalog -> Product
> Select the Product you wish to update
> Set the Minimum Order Quantity to the value you require
repeat as necessary.
and thats all there is to it !
and finally ...........
GOOD LUCK !