Hello,
I wrote a module(for prestashop 1.6) to restrict the order quantity of specific products.
The module should prevent customers to buy some specific products(with ID 220 & 221 in this case) more than once and not more than 1 piece.
Unfortunately the code isn't working. Maybe someone can help me
public function hookActionValidateOrder($params)
{
// Loop through cart products
foreach ($this->context->cart->getProducts() as $product) {
$product_id = (int)$product['id_product'];
// Check if the product is either ID 220 or 221
if ($product_id !== 220 && $product_id !== 221) {
continue; // Skip to the next product
}
// Check if the customer has already purchased the product
$customer_id = (int)$this->context->customer->id;
$db = Db::getInstance();
$result = $db->getValue('SELECT COUNT(*) FROM `'._DB_PREFIX_.'orders` o
LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON (od.`id_order` = o.`id_order`)
WHERE o.`id_customer` = '.(int)$customer_id.' AND od.`product_id` = '.(int)$product_id);
if ($result > 0) {
$this->context->controller->errors[] = 'You have already purchased this product.';
return false;
}
// Check if the customer is trying to purchase more than 1 piece
$quantity = (int)$product['cart_quantity'];
if ($quantity > 1) {
$this->context->controller->errors[] = 'You can only buy one piece of this product.';
return false;
}
}
// If everything is OK, allow the purchase
return true;
}