Post by kkasfikis » Wed Feb 19, 2020 1:26 am

Hello,
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
I changed the add() function as below :

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)) . "'");
		}
	}
In the above code the option_id = 27 is the option "Number of installments" and the option_value_id = 165 is the option value "No installments". What im trying to do is read all the $option array and if there is option_id = 27 with value different from 165 return without adding to cart.
Im still a newbie in opencart and generally in php so please be kind :)
Thank you

Newbie

Posts

Joined
Thu Jan 02, 2020 8:10 pm

Post by kkasfikis » Wed Feb 19, 2020 4:08 pm

To make my question more clear, based on this thread
viewtopic.php?t=58177
$option array has the following format :
select / checkbox

Code: Select all

    [option] => Array
        (
            [252] => 78
        )
 
Checkbox:

Code: Select all

    [option] => Array
        (
            [253] => Array
                (
                    [0] => 93
                    [1] => 94
                    [2] => 95
                )

        )
 
Text, Date, TextArea:

Code: Select all

     [option] => Array
        (
            [254] => "my text value"
        )
 
How do i read the whole $option array and isolate the option value im interested in ?
Im using opencart 3.0.3.2 with journal 3
thank you

Newbie

Posts

Joined
Thu Jan 02, 2020 8:10 pm

Post by kkasfikis » Wed Feb 19, 2020 6:30 pm

solved it on my own
the array contains product_option_id and product_option_value_id and not option_id and option_value_id respectively

Newbie

Posts

Joined
Thu Jan 02, 2020 8:10 pm
Who is online

Users browsing this forum: No registered users and 4 guests