Have a look at the file \catalog\controller\checkout_payment.php for an example of code that includes form input validation and navigation for valid/invalid inputs.
Also look in the cart template. It already has support for an error message to be displayed.
Also look in the cart template. It already has support for an error message to be displayed.
Hi,
Don't be too hard on yourself, this as a server side solution turned out to be more tricky than I thought. The following is not a complete solution but does contain enough working code to get you started. You could also explore doing the validation using javascript on the client.
best of luck.
The tricky part was finding that I had to make the following change to the update function in \store\library\cart\cart.php
Try it first without this bit if you want to see why.
backup your copy of \catalog\controller\cart.php and replace it with the following
Don't be too hard on yourself, this as a server side solution turned out to be more tricky than I thought. The following is not a complete solution but does contain enough working code to get you started. You could also explore doing the validation using javascript on the client.
best of luck.
The tricky part was finding that I had to make the following change to the update function in \store\library\cart\cart.php
Try it first without this bit if you want to see why.

Code: Select all
function update($key, $qty) {
if ($qty) {
$this->data[$key] = $qty;
// new bit here to keep products quantity in sync
$this->products[$key]['quantity'] = $qty;
} else {
$this->remove($key);
}
$this->session->set('cart', $this->data);
}
Code: Select all
<?php
class ControllerCart extends Controller
{
var $error = null;
function index()
{
$cart =& $this->locator->get('cart');
$config =& $this->locator->get('config');
$currency =& $this->locator->get('currency');
$customer =& $this->locator->get('customer');
$database =& $this->locator->get('database');
$language =& $this->locator->get('language');
$image =& $this->locator->get('image');
$module =& $this->locator->get('module');
$request =& $this->locator->get('request');
$response =& $this->locator->get('response');
$shipping =& $this->locator->get('shipping');
$tax =& $this->locator->get('tax');
$template =& $this->locator->get('template');
$url =& $this->locator->get('url');
if ($request->isPost())
{
if ($request->get('product_id', 'post')) {
$cart->add($request->get('product_id', 'post'), '1', $request->get('option', 'post'));
}
if ($request->get('quantity', 'post')) {
foreach ($request->get('quantity', 'post') as $key => $value) {
$cart->update($key, $value);
}
}
if ($request->get('remove', 'post')) {
foreach (array_keys($request->get('remove', 'post')) as $key) {
$cart->remove($key);
}
}
// extra bit here, the call to $this->validate()
// if $this->validate() returns false, we do not redirect,
// but instead accept the post and remain on the current
// form so the user can see the error message, the invalid data
// and fix it.
//
if ($this->validate($cart))
$response->redirect($url->ssl('cart'));
}
$language->load('controller/cart.php');
$template->set('title', $language->get('heading_title'));
if ($cart->hasProducts()) {
$view = $this->locator->create('template');
$view->set('heading_title', $language->get('heading_title'));
$view->set('text_subtotal', $language->get('text_subtotal'));
$view->set('column_remove', $language->get('column_remove'));
$view->set('column_image', $language->get('column_image'));
$view->set('column_name', $language->get('column_name'));
$view->set('column_model', $language->get('column_model'));
$view->set('column_quantity', $language->get('column_quantity'));
$view->set('column_price', $language->get('column_price'));
$view->set('column_total', $language->get('column_total'));
$view->set('button_update', $language->get('button_update'));
$view->set('button_shopping', $language->get('button_shopping'));
$view->set('button_checkout', $language->get('button_checkout'));
// new bit here
if ($this->error)
$view->set('error', $this->error);
else
$view->set('error', ((!$cart->hasStock()) && ($config->get('config_stock_check')) ? $language->get('error_stock') : NULL));
$view->set('action', $url->href('cart'));
$product_data = array();
foreach ($cart->getProducts() as $result) {
$option_data = array();
foreach ($result['option'] as $option) {
$option_data[] = array(
'name' => $option['name'],
'value' => $option['value'],
);
}
$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']))
);
}
$view->set('products', $product_data);
$view->set('subtotal', $currency->format($cart->getSubtotal()));
$view->set('continue', $url->href('home'));
$view->set('checkout', $url->href('checkout_shipping'));
$template->set('content', $view->fetch('content/cart.tpl'));
$template->set($module->fetch());
$response->set($template->fetch('layout.tpl'));
} else {
$view = $this->locator->create('template');
$view->set('heading_title', $language->get('heading_title'));
$view->set('text_error', $language->get('text_error'));
$view->set('button_continue', $language->get('button_continue'));
$view->set('continue', $url->href('home'));
$template->set('content', $view->fetch('content/error.tpl'));
$template->set($module->fetch());
$response->set($template->fetch('layout.tpl'));
}
}
//
// new bit here, a function that returns false if there is an error
// in the entered data and puts a value in the $this->error
// variable to display to the user.
//
function validate($cart)
{
$products = $cart->getProducts();
foreach ($products as $product)
{
if (!(ereg("^[0-9]*$", $product['quantity'])))
{
$this->error = 'invalid characters in quantity ' . $product['quantity'];
return false;
}
}
$this->error = '';
return true;
}
}
?>
Who is online
Users browsing this forum: No registered users and 2 guests