Post by chameleon121 » Fri Sep 08, 2023 10:19 am

Hi all, Im trying to get the amount of days difference between 2 option date fields (Start Date & End Date) I would like to save the start and end dates plus the difference in days saved in an array so I can use the variables to adjust totals and also use in cart.twig and checkout.php/twig, but I am not understanding the results I am getting. Here is the code I have and the results from the echo commands give this. "start: 2023-09-08 end: 2023-09-08 start: 2023-09-11 end: 2023-09-11" I dont know why the 2 pairs.
here is my code in cart.php

Code: Select all

			$option_data = array();
			foreach ($product['option'] as $option) {
				if ($option['name'] = 'Start Date') {
					$startdate = $option['value'];
					echo " start: ".$startdate;
				}
				if ($option['name'] = 'End Date') {
					$enddate = $option['value'];
					echo " end: ".$enddate;
				}
				if ($option['type'] != 'file') {
					$value = $option['value'];
				} else {
					$upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
					if ($upload_info) {
						$value = $upload_info['name'];
					} else {
						$value = '';
					}
				}
				$datediff = abs(strtotime($enddate) - strtotime($startdate));
				$days = floor($datediff/(60*60*24));
//echo $days;
				$option_data[] = array(
					'name'  => $option['name'],
					'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value),
					'type'  => $option['type'],
					'days'  => $days, 
					'startdate' =>  $startdate,
					'enddate' =>  $enddate
				);
			}
Last edited by chameleon121 on Tue Sep 12, 2023 4:27 am, edited 2 times in total.

Newbie

Posts

Joined
Mon Jan 19, 2015 4:04 am

Post by chameleon121 » Sat Sep 09, 2023 6:21 am

I figured it out after much trial and error and works great! Just have to add some error checking to make sure the calculation only does 'date' options.

Code: Select all

			$option_data = array();

			foreach ($product['option'] as $option) {
				if ($option['type'] != 'file') {
					$value = $option['value'];
				} else {
					$upload_info = $this->model_tool_upload->getUploadByCode($option['value']);

					if ($upload_info) {
						$value = $upload_info['name'];
					} else {
						$value = '';
					}
				}

				$option_data[] = array(
					'name'  => $option['name'],
					'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value),
					'type'  => $option['type']
				);
			}

			//Calculate number days for item rental from 2 dates.
			$datediff = abs(strtotime($option_data[1]['value']) - strtotime($option_data[0]['value']));
			$days = floor($datediff/(60*60*24));
				//echo $days;
				
			// Display prices
			if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
				$unit_price = $this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax'));
				
				$price = $this->currency->format($unit_price, $this->session->data['currency']);
				$total = $this->currency->format($unit_price * $product['quantity'] * $days, $this->session->data['currency']);
			} else {
				$price = false;
				$total = false;
			}
			$data['products'][] = array(
				'cart_id'   => $product['cart_id'],
				'thumb'     => $image,
				'name'      => $product['name'],
				'model'     => $product['model'],
				'option'    => $option_data,
				'recurring' => ($product['recurring'] ? $product['recurring']['name'] : ''),
				'quantity'  => $product['quantity'],
				'price'     => $price,
				'total'     => $total,
				'href'      => $this->url->link('product/product', 'product_id=' . $product['product_id']),
				'days'		=> $days
			);
		}
Last edited by chameleon121 on Tue Sep 12, 2023 4:29 am, edited 2 times in total.

Newbie

Posts

Joined
Mon Jan 19, 2015 4:04 am

Post by chameleon121 » Tue Sep 12, 2023 12:10 am

IGNORE THE ABOVE, DOESN'T WORK.
Instead change the code in the system/library/cart/cart.php
also added some code for week/monthly discounts.

You can download the entire VQmod XML below. Good for OC 3.x or 4.x

Code: Select all

			//Calculate number days for item rental from 2 dates.
			$datediff = abs(strtotime($option_data[1]['value']) - strtotime($option_data[0]['value']));
			$days = floor($datediff/(60*60*24));
			
			if ($days >= 7 AND $days < 30) {
				$discount=20;
				$discount_text='Weekly discount 20%';
			} elseif ($days >= 30) {
				$discount=40;
				$discount_text='Monthly discount 40%';
			} else {
				$discount=0;
				$discount_text='';
			}

				$product_data[] = array(
					'cart_id'         => $cart['cart_id'],
					'product_id'      => $product_query->row['product_id'],
					'name'            => $product_query->row['name'],
					'model'           => $product_query->row['model'],
					'shipping'        => $product_query->row['shipping'],
					'image'           => $product_query->row['image'],
					'option'          => $option_data,
					'download'        => $download_data,
					'quantity'        => $cart['quantity'],
					'minimum'         => $product_query->row['minimum'],
					'subtract'        => $product_query->row['subtract'],
					'stock'           => $stock,
					'price'           => ($price + $option_price) * ((100-$discount) / 100),
					'total'           => ($price + $option_price) * $cart['quantity'] * ((100-$discount) / 100) * $days,
					'reward'          => $reward * $cart['quantity'],
					'points'          => ($product_query->row['points'] ? ($product_query->row['points'] + $option_points) * $cart['quantity'] : 0),
					'tax_class_id'    => $product_query->row['tax_class_id'],
					'weight'          => ($product_query->row['weight'] + $option_weight) * $cart['quantity'],
					'weight_class_id' => $product_query->row['weight_class_id'],
					'length'          => $product_query->row['length'],
					'width'           => $product_query->row['width'],
					'height'          => $product_query->row['height'],
					'length_class_id' => $product_query->row['length_class_id'],
					'recurring'       => $recurring,
					'days'			  => $days,
					'discount_text'	  => $discount_text
				);

Newbie

Posts

Joined
Mon Jan 19, 2015 4:04 am
Who is online

Users browsing this forum: No registered users and 0 guests