Hello everybody
I investigated how to do the quantity restriction and got it by this way:
I create a view in mysql workbech:
CREATE
ALGORITHM = UNDEFINED
DEFINER = `root`@`localhost`
SQL SECURITY DEFINER
VIEW `bitnami_abantecart`.`products_customer` AS
SELECT
`ac_op`.`product_id` AS `product_id`,
`ac_op`.`name` AS `product_name`,
SUM(`ac_op`.`quantity`) AS `quantity_hist`,
`ac_c`.`customer_id` AS `customer_id`
FROM
((`bitnami_abantecart`.`ac_order_products` `ac_op`
JOIN `bitnami_abantecart`.`ac_orders` `ac_o` ON (`ac_op`.`order_id` = `ac_o`.`order_id`))
JOIN `bitnami_abantecart`.`ac_customers` `ac_c` ON (`ac_c`.`customer_id` = `ac_o`.`customer_id`))
WHERE
`ac_o`.`order_status_id` = 5
GROUP BY `ac_op`.`product_id`, `ac_c`.`customer_id`, `ac_c`.`customer_id`
This view show product_id, product_name, customer_id and sum(quantity) group by product and customer, so i can ask for orders made in history by customer with status "5-Complete".
And modify this htdocs\core\lib\cart.php from line 166
$product_result = $this->buildProductDetails($product_id, $quantity, $options);
if (count($product_result)) {
$product_data[$key] = $product_result;
$product_data[$key]['key'] = $key;
// Query for max product per customer --- CG
$prod_query_customer = $this->db->query("SELECT * FROM products_customer WHERE customer_id =
'".(int)$this->customer->getId()."' and product_id = '".(int)$product_result['product_id']."' ");
$quantity_hist = $prod_query_customer->row['quantity_hist'];
$quantity_ask = $quantity_hist + $product_result['quantity'];
if ( $quantity_hist >= $product_result['maximum']) {
$this->cust_data['error'] = "Ya compró " .$quantity_hist. " de " .$product_result['maximum']. " unidades permitidas de: " .$prod_query_customer->row['product_name']. ". El producto se ha retirado de su carro de compras. Por favor compre otros productos disponibles.";
$this->update($key, 0 );
}
elseif ($quantity_hist > 0 and ($quantity_ask > $product_result['maximum'])) {
$new_quantity = $product_result['maximum'] - $quantity_hist;
$this->cust_data['error'] = "Ya compró " .$quantity_hist. " de " .$product_result['maximum']. " unidades permitidas de: " .$prod_query_customer->row['product_name']. ". La cantidad solicitada se ha ajustado a " .$new_quantity. ". Por favor revise su pedido.";
$this->update($key, $new_quantity);
}
//else {
//apply min and max for quantity once we have product details.
// if ($quantity < $product_result['minimum']) {
// $this->language->load('checkout/cart', 'silent');
// $this->cust_data['error'] = $this->language->get('error_quantity_minimum');
// $this->update($key, $product_result['minimum']);
//}
//if ($product_result['maximum'] > 0) {
// $this->language->load('checkout/cart', 'silent');
// if ($quantity > $product_result['maximum']) {
// $this->cust_data['error'] = $this->language->get('error_quantity_maximum');
// $this->update($key, $product_result['maximum']);
// }
// }
} else {
$this->remove($key);
}
}
I had to comment the quantity validation made by order (Abantecart default) to apply my own validation per customer.