<?php

return;

function wc_remove_checkout_field( $fields ) {
	if ( ! wc_check_product_is_in_the_cart() ) {
		unset( $fields['billing']['cover_start_rate'] );
	}else{
		unset( $fields['billing']['policy_start_date'] );
	}

	return $fields;
}
function wc_check_product_is_in_the_cart() {
	// Add your special product IDs here
	$ids = array( '3715' );;

	// Products currently in the cart
	$cart_ids = array();

	// Find each product in the cart and add it to the $cart_ids array
	foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
		$cart_product = $values['data'];
		$cart_ids[]   = $cart_product->id;
	}

	// If one of the special products are in the cart, return true.
	if ( ! empty( array_intersect( $ids, $cart_ids ) ) ) {
		return true;
	} else {
		return false;
	}
}
add_filter( 'woocommerce_checkout_fields' , 'wc_remove_checkout_field' );
function bk_title_order_received($title, $id) {
	if (is_order_received_page () && get_the_ID () === $id) {
		$title = "Confirmation";
	}
	return $title;
}

add_filter ( 'the_title', 'bk_title_order_received', 10, 2 );
// check for empty-cart get param to clear the cart
add_action ( 'init', 'woocommerce_clear_cart_url' );

add_action ( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta($order_id) {
	$billing_confirm_items = WC ()->session->get ( 'billing_confirm_items' );
	$terms_and_conditions = $_POST ['terms'];
	
	$cardNumber = $_POST ['psense_ccnum'];
	$expiryDate = $_POST ['psense_expmonth'] . "/" . $_POST ['psense_expyear'];
	if ($cardNumber != null && $expiryDate != null) {
		
		$data = substr ( $cardNumber, - 4 );
		$data = "XXXXXXXXXXXX" . $data;
		update_post_meta ( $order_id, 'card_number', esc_attr ( $data ) );
		update_post_meta ( $order_id, 'expiry_date', esc_attr ( $expiryDate ) );
	}
	if ($billing_confirm_items) {
		update_post_meta ( $order_id, 'billing_confirm_items', esc_attr ( $billing_confirm_items ) );
		update_post_meta ( $order_id, 'payment_start_date', esc_attr ( date ( "d-m-Y" ) ) );
	}
	if ($terms_and_conditions) {
		update_post_meta ( $order_id, 'terms_and_conditions', esc_attr ( $terms_and_conditions ) );
	}
}

add_action ( 'woocommerce_checkout_process', 'my_custom_checkout_field_process' );
function my_custom_checkout_field_process() {
	WC ()->session->set ( 'billing_confirm_items', $_POST ['billing_confirm_items'] );
	
	WC ()->session->set ( 'billing_first_name', $_POST ['billing_first_name'] );
	
	WC ()->session->set ( 'billing_last_name', $_POST ['billing_last_name'] );
	
	WC ()->session->set ( 'billing_address_1', $_POST ['billing_address_1'] );
	
	WC ()->session->set ( 'billing_city', $_POST ['billing_city'] );
	
	WC ()->session->set ( 'billing_postcode', $_POST ['billing_postcode'] );
	
	WC ()->session->set ( 'billing_dob', $_POST ['billing_dob'] );
	
	WC ()->session->set ( 'billing_phone', $_POST ['billing_phone'] );
	
	WC ()->session->set ( 'billing_email', $_POST ['billing_email'] );
	
	WC ()->session->set ( 'billing_password', $_POST ['billing_password'] );
	
	WC ()->session->set ( 'shipping_first_name', $_POST ['shipping_first_name'] );
	
	WC ()->session->set ( 'shipping_last_name', $_POST ['shipping_last_name'] );
	
	WC ()->session->set ( 'shipping_company', $_POST ['shipping_company'] );
	
	WC ()->session->set ( 'shipping_address_1', $_POST ['shipping_address_1'] );
	
	WC ()->session->set ( 'shipping_address_2', $_POST ['shipping_address_2'] );
	
	WC ()->session->set ( 'shipping_city', $_POST ['shipping_city'] );
	
	WC ()->session->set ( 'shipping_state', $_POST ['shipping_state'] );
	
	/*
	 * if ( ! $_POST['billing_email_policy'] && ! $_POST['billing_confirm_items'] )
	 * wc_add_notice( __( 'You must agree with all terms and conditions.' ), 'error' );
	 */
}
function woocommerce_clear_cart_url() {
	global $woocommerce;
	
	if (isset ( $_GET ['empty-cart'] )) {
		$woocommerce->cart->empty_cart ();
	}
}
function wnd_checkout_message() {
	echo '<div class="wnd-checkout-message"><h3>Extra &pound39.00 added!</h3>
</div>';
}
function jquery_min() {
	wp_enqueue_script ( 'jquery-min', 'https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js', array (
			'jquery' 
	) );
}
function my_scripts_method() {
	wp_enqueue_script ( 'custom-script', get_stylesheet_directory_uri () . '/js/minus-child.js', array (
			'jquery' 
	) );
}
add_action ( 'woocommerce_after_checkout_form', 'jquery_min' );
add_action ( 'woocommerce_after_checkout_form', 'my_scripts_method' );
function wc_quiits_custom_variable_price($cart_object) {
	$discountProducts = array (
			"TV",
			"Satellite System",
			"Fridge & Freezer",
			"Washing Machine",
			"Washer Dryer",
			"Tumble Dryer",
			"Cooker",
			"Dishwasher",
			"Microwave",
			"Oven" 
	);
	$cart = WC ()->cart->get_cart ();
	$totalPrice = $cart_object->cart_contents_total;
	$productCount = 0;
	$televisionCount = 0;
	$satliteCount = 0;
	$item_6_to_10 = 0;
	$homeemegerncyCout = 0;
	$onlyHomeEmergency = 0;
	$motorCout = 0;
	$motorPrice = 0;
	foreach ( $cart as $cart_item ) {
		
		$product = wc_get_product ( $cart_item ['product_id'] );
		
		/* extra amount in 6-10 year item */
		/*
		 * if ($cart_item ['variation'] ['attribute_age-of-item'] && $cart_item ['variation'] ['attribute_age-of-item'] == "6-10 years")
		 * $item_6_to_10+=$cart_item ['quantity'];
		 */
		if ($product->get_name () == "TV")
			$televisionCount += $cart_item ['quantity'];
		if ($product->get_name () == "Satellite System")
			$satliteCount += $cart_item ['quantity'];
		if (in_array ( $product->get_name (), $discountProducts ))
			$productCount += $cart_item ['quantity'];
		if ($product->get_name () == "Home Emergency + Boiler Service")
			$homeemegerncyCout += $cart_item ['quantity'];
		if ($product->get_name () == "Boiler Service")
			$boilerServiceCout += $cart_item ['quantity'];
		if ($product->get_name () == "Home Emergency")
			$onlyHomeEmergency += $cart_item ['quantity'];
		if ($product->get_name () == "Motor Break Down Cover") {
			$motorCout += $cart_item ['quantity'];
			$motorPrice = $cart_item ['line_total'];
		}
	}
	// this calculation for 94.99 for Home Emergency + Boiler Service & Boiler Service (monthly will be 8.99)
	
	/*
	 * if($homeemegerncyCout==1){
	 * $totalPrice = ($homeemegerncyCout * 7.915833333333333);
	 *
	 * }
	 * if($homeemegerncyCout==1){
	 *
	 * $price=7.9158333333333;
	 * if($totalPrice==$price){
	 *
	 * $totalPrice=8.99;
	 * }
	 * }
	 */
	if ($productCount == 1 || $productCount == 0) {
		if ($_POST ['billing_cycle'] == "Yearly") {
			if ($homeemegerncyCout) {
				$totalPrice = $totalPrice - ($homeemegerncyCout * 8.99);
				$totalPrice += ($homeemegerncyCout * 7.915833333333333);
			}
			if ($boilerServiceCout) {
				$totalPrice = $totalPrice - ($boilerServiceCout * 8.99);
				$totalPrice += ($boilerServiceCout * 7.915833333333333);
			}
			if ($motorCout) {
				
				$totalPrice = $totalPrice - (($motorCout * $motorPrice));
				$totalPrice += ($motorCout * $motorPrice) * 10 / 12;
			}
		}
	} else if ($productCount > 1) {
		if ($productCount == $televisionCount && $productCount < 5) {
			switch ($productCount) {
				case 2 :
					$totalPrice = 9.99;
					break;
				case 3 :
					$totalPrice = 12.99;
					break;
				case 4 :
					$totalPrice = 15.99;
					break;
			}
		} else if ($productCount == $satliteCount && $productCount < 5) {
			switch ($productCount) {
				case 2 :
					$totalPrice = 9.99;
					break;
				case 3 :
					$totalPrice = 10.99;
					break;
				case 4 :
					$totalPrice = 11.99;
					break;
			}
		} else if ($productCount < 10) {
			switch ($productCount) {
				case 2 :
					$totalPrice = 9.99;
					break;
				case 3 :
					$totalPrice = 14.99;
					break;
				case 4 :
					$totalPrice = 19.99;
					break;
				case 5 :
					$totalPrice = 19.99;
					break;
				case 6 :
					$totalPrice = 24.99;
					break;
				case 7 :
					$totalPrice = 29.99;
					break;
				case 8 :
					$totalPrice = 34.99;
					break;
				case 9 :
					$totalPrice = 39.99;
					break;
			}
		}
		if ($onlyHomeEmergency) {
			$totalPrice += ($onlyHomeEmergency * 1.99);
		}
		if ($_POST ['billing_cycle'] == "Yearly") {
			if ($homeemegerncyCout) {
				$totalPrice += ($homeemegerncyCout * 7.915833333333333);
			}
			if ($boilerServiceCout) {
				$totalPrice += ($boilerServiceCout * 7.915833333333333);
			}
			if ($motorCout) {
				$totalPrice += ($motorCout * $motorPrice) * 10 / 12;
			}
		} else {
			if ($homeemegerncyCout) {
				$totalPrice += ($homeemegerncyCout * 8.99);
			}
			if ($boilerServiceCout) {
				$totalPrice += ($boilerServiceCout * 8.99);
			}
			if ($motorCout) {
				$totalPrice += ($motorCout * $motorPrice);
			}
		}
	}
	/* extra amount in 6-10 year item */
	/*
	 * if($item_6_to_10>0){
	 * $totalPrice+=($item_6_to_10*39);
	 *
	 * add_action( 'woocommerce_before_checkout_form', 'wnd_checkout_message', 10 );
	 * }
	 */
	// var_dump("totalPrice",$totalPrice);
	$cart_object->cart_contents_total = $totalPrice;
	$cart_object->subtotal = $totalPrice;
	if ($cart_object->fees) {
		if ($cart_object->fees [0]->amount > 10) {
			$cart_object->fees [0]->amount = $totalPrice * 11;
			$cart_object->fee_total = $totalPrice * 11;
		}
	}
}

add_action ( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price($cart_object) {
	$cart = WC ()->cart->get_cart ();
	
	if ($cart) {
		$homeEmergency = $boilerService = false;
		
		foreach ( $cart as $key => $cart_item ) {
			
			$product = wc_get_product ( $cart_item ['product_id'] );
			
			if ($product->get_name () == "Home Emergency") {
				$homeEmergency = true;
				$key_home_emergency = $key;
			}
			
			if ($product->get_name () == "Boiler Service") {
				$homeEmergencyBoiler = true;
				$key_Boiler_Service = $key;
			}
			
			if ($product->get_name () == "Home Emergency + Boiler Service") {
				$homeEmergencyboilerService = true;
				$key_homeEmergencyboilerService = $key;
			}
		}
		if ($homeEmergencyboilerService == true) {
			// WC()->cart->remove_cart_item($key_homeEmergencyboilerService);
			WC ()->cart->remove_cart_item ( $key_home_emergency );
			WC ()->cart->remove_cart_item ( $key_Boiler_Service );
			// addHomeEmergencyBoilerCombinationToCart();
			// wc_add_notice( __( 'Already in cart' ), 'success' );
		}
		if ($homeEmergency == true && $homeEmergencyBoiler == true) {
			
			WC ()->cart->remove_cart_item ( $key_home_emergency );
			WC ()->cart->remove_cart_item ( $key_Boiler_Service );
			addHomeEmergencyBoilerCombinationToCart ();
		}
	}
}
function addHomeEmergencyBoilerCombinationToCart() {
	WC ()->cart->add_to_cart ( 1429 );
}

add_filter ( 'woocommerce_calculate_totals', 'wc_quiits_custom_variable_price', 10, 1 );
add_filter ( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' ); // 2.1 +
function woo_archive_custom_cart_button_text() {
	return __ ( 'Get an Instant Quote', 'woocommerce' );
}

// terms & condition
add_action ( 'woocommerce_review_order_before_submit', 'downloadLinkBasedOnProduct', 9 );
function downloadLinkBasedOnProduct() {
	// $amount2 = floatval( preg_replace( '#[^\d.]#', '', WC()->cart->get_cart_total() ) );
	$otherProductFound = FALSE;
	$homeemergencyFound = FALSE;
	$homeemergencyBoilerFound = FALSE;
	$gadgetFound = FALSE;
	$tvFound = FALSE;
	$boilerFound = FALSE;
	$satliteFound = FALSE;
	$tvSatliteFound = FALSE;
	// Show Terms 1
	$otherProducts = array (
			"Fridge & Freezer",
			"Washing Machine",
			"Washer Dryer",
			"Tumble Dryer",
			"Cooker",
			"Dishwasher",
			"Microwave",
			"Oven" 
	);
	$cart = WC ()->cart->get_cart ();
	$key_home_emergency = $homeemergencyBoilerFound = $key_Boiler_Service = null;
	foreach ( $cart as $key => $cart_item ) {
		
		$product = wc_get_product ( $cart_item ['product_id'] );
		if (in_array ( $product->get_name (), $otherProducts ) && $otherProductFound == FALSE)
			$otherProductFound = TRUE;
		
		if ($product->get_name () == "Home Emergency + Boiler Service" && $homeemergencyFound == FALSE) {
			$homeemergencyFound = TRUE;
			$boilerFound = TRUE;
		}
		if ($product->get_name () == "Home Emergency") {
			$homeemergencyFound = TRUE;
			$key_home_emergency = $key;
		}
		if ($product->get_name () == "TV" && $tvFound == FALSE) {
			$tvFound = TRUE;
		}
		if ($product->get_name () == "Satellite System" && $satliteFound == FALSE) {
			$satliteFound = TRUE;
		}
		if ($product->get_name () == "Boiler Service" && $boilerFound == FALSE) {
			$boilerFound = TRUE;
			$key_Boiler_Service = $key;
		}
	}
	
	if ($satliteFound == TRUE && $tvFound == TRUE) {
		$tvSatliteFound = TRUE;
		$tvFound = FALSE;
		$satliteFound = FALSE;
	}
	$link = "";
	
	if ($homeemergencyFound == TRUE)
		$link = '<a href="' . get_site_url () . '/wp-content/uploads/2017/11/Home-Emergency-Pol_SS_Doc_Mandatory-250-Final-210717-1.pdf" download><u>Download the Terms & Conditions for Home Emergency</u></a>';
	if ($otherProductFound == TRUE)
		$link .= '<a href="' . get_site_url () . '/wp-content/uploads/2017/11/Multi-Pol_SS_Doc_SSMA4.2-Alpha-.pdf" download><u> Download the Terms & Conditions for Kitchen appliances</u></a>';
	if ($tvFound == TRUE)
		$link .= '<a href="' . get_site_url () . '/wp-content/uploads/2017/11/Multi-Pol_SS_Doc_SSMA4.2-Alpha-.pdf" download><u> Download the Terms & Conditions for  TV appliances</u></a>';
	if ($satliteFound == TRUE)
		$link .= '<a href="' . get_site_url () . '/wp-content/uploads/2017/11/Multi-Pol_SS_Doc_SSMA4.2-Alpha-.pdf" download><u> Download the Terms & Conditions for Satellite</u></a>';
	if ($tvSatliteFound == TRUE)
		$link .= '<a href="' . get_site_url () . '/wp-content/uploads/2017/11/Multi-Pol_SS_Doc_SSMA4.2-Alpha-.pdf" download><u> Download the Terms & Conditions for  Satellite & TV appliances</u></a>';
	if ($boilerFound == TRUE)
		$link .= '<a href="' . get_site_url () . '/wp-content/uploads/2017/11/Boiler-Service_SS_DocSSBS1.5-Final-09122016.pdf" download><u> Download the Terms & Conditions for  Boiler Service</u></a>';
	
	?>

<p class="form-row terms wc-terms-and-conditions">
	<span class="t_c_download_link"><?php echo $link ?></span>
</p>

<?php
}

?>
