i need to add installments in some of my products.
I have created a product option (select) called "Number of installments" with various values like "no installments", "3 installments" etc. I want products to be added to cart only if they have the same number of installments.
If the user selects something different from "no installments" i need to check if the cart is empty and only if empty to add the product to the cart.
Im trying to read the options of the product the user is adding when clicking "Add to cart", and if the Number of Installments option exists and its value is different from "no installments",
Code: Select all
system/library/cart/cart.php
Code: Select all
public function add($product_id, $quantity = 1, $option = array(), $recurring_id = 0) {
//if the product that the user is adding has installments the cart should be empty !
foreach ($option as $key=>$val) {
if($key=="[27]" && $val<>165){
if(count($this->getProducts())<>0){
return;
}
}
}
//This should block user from adding a product with installments when there are already products in cart
foreach ($this->getProducts() as $value) {
foreach ($value['option'] as $value1) {
if($value1['option_id']==27 && $value1['option_value_id']<>165){
return;
}
}
}
$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "cart WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "' AND product_id = '" . (int)$product_id . "' AND recurring_id = '" . (int)$recurring_id . "' AND `option` = '" . $this->db->escape(json_encode($option)) . "'");
if (!$query->row['total']) {
$this->db->query("INSERT " . DB_PREFIX . "cart SET api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "', customer_id = '" . (int)$this->customer->getId() . "', session_id = '" . $this->db->escape($this->session->getId()) . "', product_id = '" . (int)$product_id . "', recurring_id = '" . (int)$recurring_id . "', `option` = '" . $this->db->escape(json_encode($option)) . "', quantity = '" . (int)$quantity . "', date_added = NOW()");
} else {
$this->db->query("UPDATE " . DB_PREFIX . "cart SET quantity = (quantity + " . (int)$quantity . ") WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "' AND product_id = '" . (int)$product_id . "' AND recurring_id = '" . (int)$recurring_id . "' AND `option` = '" . $this->db->escape(json_encode($option)) . "'");
}
}
Im still a newbie in opencart and generally in php so please be kind

Thank you