<?php

/**
 * The plugin bootstrap file
 *
 * This file is read by ione360 to generate the plugin information in the plugin
 * admin area. This file also includes all of the dependencies used by the plugin,
 * registers the activation and deactivation functions, and defines a function
 * that starts the plugin.
 *
 * @link              www.colijn-it.nl
 * @since             1.0.0
 * @package           ione360_configurator
 *
 * @ione360-plugin
 * Plugin Name:       ione360 Configurator
 * Plugin URI:        www.ione360.com
 * Description:       We help your customers make better decisions by bridging the imagination gap.
 * Version:           2.0.57
 * Author:            Colijn-IT
 * Author URI:        www.colijn-it.nl
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       ione360-configurator
 * Domain Path:       /languages
 */

// If this file is called directly, abort.
if (!defined("WPINC")) {
  die();
}

/**
 * Currently plugin version.
 * Start at version 1.0.0 and use SemVer - https://semver.org
 * Rename this for your plugin and update it as you release new versions.
 */
define("ione360_configurator_VERSION", "2.0.57");

/**
 * The code that runs during plugin activation.
 * This action is documented in includes/class-configurator-ione360-activator.php
 */
function activate_ione360_configurator()
{
	require_once plugin_dir_path(__FILE__) .
    "includes/class-configurator-ione360-activator.php";
	ione360_configurator_Activator::activate();
  
}
/**
 * The code that runs during plugin deactivation.
 * This action is documented in includes/class-configurator-ione360-deactivator.php
 */
function deactivate_ione360_configurator()
{
	require_once plugin_dir_path(__FILE__) .
	"includes/class-configurator-ione360-deactivator.php";
	ione360_configurator_Deactivator::deactivate();
}

register_activation_hook(__FILE__, "activate_ione360_configurator");
register_deactivation_hook(__FILE__, "deactivate_ione360_configurator");

/**
 * The core plugin class that is used to define internationalization,
 * admin-specific hooks, and public-facing site hooks.
 */
require plugin_dir_path(__FILE__) . "includes/class-configurator-ione360.php";

/**
 * Begins execution of the plugin.
 *
 * Since everything within the plugin is registered via hooks,
 * then kicking off the plugin from this point in the file does
 * not affect the page life cycle.
 *
 * @since    1.0.0
 */
function run_ione360_configurator()
{
	$plugin = new ione360_configurator();
	$plugin->run();
}
run_ione360_configurator();

function includeFileWithVariables($fileName, $variables) {
	extract($variables);
	include($fileName);
}



function configurator_shortcode($atts) {

	$a = shortcode_atts( array(
		'sku' => '',
		'render' => '',
		'floor' => '',
		'layout' => '',
		'external' => '',
	), $atts );

	$sku = $a['sku'];
	$render = $a['render'];
	$floor = $a['floor'];  
	$layout = $a['layout'];
	$external = $a['external'];
	ob_start();
	includeFileWithVariables('wordpress-ione360.php', array('sku'=> "$sku", 'render'=> "$render", 'floor'=>"$floor", 'layout'=> "$layout", 'external'=> "$external"));
	return ob_get_clean();
  
} 
add_shortcode( 'ione360_configurator', 'configurator_shortcode' );

//  Functions to display a list of all the shortcodes
function diwp_get_list_of_shortcodes(){
 
	// Get the array of all the shortcodes
	global $shortcode_tags;

	$shortcodes = $shortcode_tags;

	// sort the shortcodes with alphabetical order
	ksort($shortcodes);

	$shortcode_output = "<ul>";

	foreach ($shortcodes as $shortcode => $value) {
	  $shortcode_output = $shortcode_output.'<li>['.$shortcode.']</li>';
	}

	$shortcode_output = $shortcode_output. "</ul>";

	return $shortcode_output;

}
add_shortcode('get-shortcode-list', 'diwp_get_list_of_shortcodes');

/* Configurator Recaptcha */
function enqueue_recaptcha_script() {
    wp_enqueue_script( 'google-recaptcha', 'https://www.google.com/recaptcha/api.js', array(), null, true );
}
add_action( 'wp_enqueue_scripts', 'enqueue_recaptcha_script' );

/* Configurator Custom Contact Form */
add_action( 'wp_ajax_custom_form_submit', 'process_contact_form' );
add_action( 'wp_ajax_nopriv_custom_form_submit', 'process_contact_form' );


function process_contact_form(){
	GLOBAL $wpdb;
	parse_str($_POST['formData'], $params);	
	$response_body = new stdClass();
	$useRecaptcha = esc_attr(get_option("configurator_use_recaptcha_setting"));
	if ($useRecaptcha) {
		$recaptcha_response = $_POST['recaptcha_response'];
		$recaptcha_secret_key = esc_attr(get_option("configurator_recaptcha_secretkey_setting"));

		$verify_url = 'https://www.google.com/recaptcha/api/siteverify';
		$args = array(
			'body' => array(
				'secret' => $recaptcha_secret_key,
				'response' => $recaptcha_response
			)
		);
		$response = wp_remote_post( $verify_url, $args );
		$response_body = json_decode( wp_remote_retrieve_body( $response ) );		
	} else {
		$response_body->success = true;;
	}

	if ($response_body->success) {

		/*create table if not exists*/
		$table_name = $wpdb->prefix.'configurator_contact_form';
		$charset_collate = $wpdb->get_charset_collate();
		$query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) );
		if ( ! $wpdb->get_var( $query ) == $table_name ) {
			$sql = "CREATE TABLE IF NOT EXISTS $table_name (
					id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
					contact_full_name VARCHAR(255) NOT NULL,
					contact_email VARCHAR(255) NOT NULL,
					contact_gdpr VARCHAR(255) NOT NULL,
					contact_message VARCHAR (255) NOT NULL,
					contact_configuration VARCHAR (255),
					product_name VARCHAR (255),
					product_price VARCHAR (255),
					product_sku VARCHAR (255),
					product_image LONGBLOB,
					created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
			) $charset_collate;";
			if($wpdb->query($sql)){
				submitsForm($table_name,$params);
			}
		}else{
			submitsForm($table_name,$params);
		}
	} else {
		$response = array(
			'success' => false
		);	
		wp_send_json($response);
		wp_die();		
	}
}

function submitsForm($table_name, $params){
    GLOBAL $wpdb;
    $curTime = date('Y-m-d H:i:s');
    $query = "INSERT INTO {$table_name}(contact_full_name, contact_email, contact_gdpr, contact_message, contact_configuration, product_name, product_sku, product_price, product_image, created_at) 
			   VALUES('{$params['contact_full_name']}','{$params['contact_email']}','{$params['contact_gdpr']}','{$params['contact_message']}','{$params['contact_configuration']}','{$params['product_name']}','{$params['product_sku']}','{$params['product_price']}','{$params['product_image']}','{$curTime}')"; 
    if($wpdb->query($query)){
		
		$enableEmail = esc_attr(get_option("configurator_contact_enable_email_setting"));
		if($enableEmail) {
			
			$senderName = esc_attr(get_option("configurator_contact_name_setting"));
			$senderEmail = esc_attr(get_option("configurator_contact_email_setting"));
			$bccEmail = esc_attr(get_option("configurator_contact_bcc_setting"));
			$subjectEmail = esc_attr(get_option("configurator_contact_subject_setting"));
			
			$name = sanitize_text_field($params['contact_full_name']);
			$email = sanitize_email($params['contact_email']);
			
			$subject = sanitize_text_field('New Submission Configurator Contact Form');
			$message = sanitize_textarea_field($params['contact_message']);
			
			$product_name = sanitize_textarea_field($params['product_name']);
			$product_sku = sanitize_textarea_field($params['product_sku']);
			$product_price = sanitize_textarea_field($params['product_price']);
			$product_image = sanitize_textarea_field($params['product_image']);
			
			$configuration = sanitize_textarea_field($params['contact_configuration']);
			
			$site_logo = get_theme_mod('custom_logo');
			$custom_logo_url = wp_get_attachment_image_src($site_logo, 'full');
			if ($custom_logo_url) {
				$logo = esc_url($custom_logo_url[0]);
			}

			// Set up email content
			$to = $email; // Replace with the actual recipient email address
			$headers[] = 'From: ' . $senderName . ' <' . $senderEmail . '>';
			$headers[] = 'Bcc: ' . $bccEmail . "\r\n";
			$headers[] = 'Content-Type: text/html; charset=UTF-8';
			$email_message .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml"><head>
			<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
			<title>' . $subject . '</title>
			<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
			</head>
			<body>';
			$email_message .= '<h2 style="font-size: 18px;font-weight:bold">Hi ' . $name . '!</h2>';
			$email_message .= '<p style="font-size: 14px">Thank you for contacting us. We will be in touch soon.<br/>Form details below.</p>'; 	
			
			$email_message .= 'Name: ' . $name . '<br/>';
			$email_message .= 'Email: ' . $email . '<br/>';
			$email_message .= 'Message: ' . $message . '<br/><br/><br/>';
			$email_message .= '<table align="left" border="0" cellpadding="0" cellspacing="0" style="border-collapse:collapse;max-width: 800px;">
								<tr>
								<th align="left" style="padding: 5px 15px;">Image</th>
								<th align="left" style="padding: 5px 15px;">Article</th>
								<th align="left" style="padding: 5px 15px;">Configuration</th>
								<th align="left" style="padding: 5px 15px;">Price</th>
								</tr>
								<tr>
								<td align="left" style="padding: 5px 15px;"><img style="max-width:200px;" src="' .  $product_image . '"/></td>
								<td align="left" style="padding: 5px 15px;">' . $product_name . '<br/>sku: ' . $product_sku . '</td>
								<td align="left" style="padding: 5px 15px;">' . $configuration . '</td>
								<td align="left" style="padding: 5px 15px;">' . $product_price . '</td>
								</tr>
							   </table><br/><br/><br/><br/>';
			$email_message .= '<p style="display: block; width: 100%; float: left;margin: 60px 0 30px 0;">Kind regards,<br/>' . $senderName . '</p><br/>';	
			if ($custom_logo_url) {
				$email_message .= '<img src="' . $logo . '" style="margin: 30px 0; max-height:80px; "/>';
			}			
			$email_message .= '</html></body>';
			// Send the email
			$sent = wp_mail($to, $subjectEmail, $email_message, $headers);		
			if ($sent) {
				$response = array(
					'success' => true
				);
			} else {
				// Email failed to send
				$response = array(
					'success' => false
				);
			}	
		} else {
			$response = array(
				'success' => true
			);					
		}		
    } else {
		$response = array(
			'success' => false
		);
    }
	wp_send_json($response);
	wp_die();
}


include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$pluginPath = "woocommerce/woocommerce.php" ;

if(is_plugin_active($pluginPath)) {
	function woocommmerce_shortcode($atts) {
		$a = shortcode_atts( array(
			'id' => '',
			'render' => '',
			'floor' => '',
			'layout' => '',
		), $atts );
		$id = $a['id'];
		$render = $a['render'];
		$floor = $a['floor'];  
		$layout = $a['layout'];
		ob_start();
		includeFileWithVariables('templates/woocommerce/woocommerce-ione360.php', array('id'=> "$id", 'render'=> "$render", 'floor'=>"$floor", 'layout'=> "$layout"));
		return ob_get_clean();
	} 
	add_shortcode( 'ione360_woocommerce', 'woocommmerce_shortcode' );
	// Define a Hook to include the iONE360 Configurator
	function custom_configurator_content() {
		if (!is_product()) {
			return;
		}

		$product = wc_get_product(get_the_ID()); // Retrieve product correctly

		if (!$product) {
			return;
		}

		$product_setting = $product->get_meta("_configurator_demo");

		if ($product_setting === 'yes') {
			$product_hook = $product->get_meta("_configurator_hook");
			$template_directory = plugin_dir_path(__FILE__) . "templates/woocommerce/woocommerce-ione360.php";

			if (file_exists($template_directory)) {
				include($template_directory);
			} else {
				error_log("iONE360 Configurator template not found at: " . $template_directory);
			}
		}
	}

	function add_custom_configurator_hook() {
		if (!is_product()) {
			return;
		}

		$product = wc_get_product(get_the_ID());

		if (!$product) {
			return;
		}

		$product_hook = $product->get_meta("_configurator_hook");
		$selected_hook = get_option('woocommerce_product_page_hook', 'woocommerce_before_single_product');
		$hook_order = get_option('woocommerce_product_page_hook_order', 10);

		// Use product-specific hook if available, otherwise fallback to global setting
		$final_hook = !empty($product_hook) ? $product_hook : $selected_hook;

		add_action($final_hook, 'custom_configurator_content', $hook_order);
	}

	// Delay hook attachment until WooCommerce initializes
	add_action('wp', 'add_custom_configurator_hook');

	// Define a Hook to include the iONE360 Configurator Button
	function custom_configurator_scroll() {
		global $product;
		$product_setting = $product->get_meta("_configurator_demo");
		if ( is_product() &&  $product_setting === 'yes' ) {
			$template_directory = untrailingslashit(plugin_dir_path(__FILE__)) . "/templates/woocommerce/scroll-to.php";
			include($template_directory);
		}
	}
	$scroll_hook = get_option( 'woocommerce_product_page_scroll_hook' );
	$scroll_order = get_option( 'woocommerce_product_page_scroll_hook_order');
	$enable_scroll = get_option( 'woocommerce_product_page_enable_scroll');
	if ($scroll_hook && $enable_scroll && $scroll_hook !== '') {
		add_action( $scroll_hook, 'custom_configurator_scroll', $scroll_order );
	}


	add_action('woocommerce_before_single_product', 'remove_woocommerce_single_product_summary', 10);
	function remove_woocommerce_single_product_summary() {
		if (!is_product()) {
			return;
		}
		global $product;
		// Ensure the $product object is initialized
		if (!$product || !is_a($product, 'WC_Product')) {
			$product = wc_get_product(get_the_ID());
		}
		// Check the product meta and settings
		if ($product && $product->get_meta("_configurator_demo") === 'yes' && get_option('woocommerce_show_only_configurator')) {
			// Remove all actions related to single product summary
			remove_all_actions('woocommerce_single_product_summary');
			remove_action('woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20);
		}
	}

	function add_to_cart_function() {
		// Ensure WooCommerce is active
		if (!class_exists('woocommerce')) {
			die('WooCommerce is not installed or activated.');
		}

		// Ensure the product ID is provided
		if (!isset($_POST['product_id']) || !$_POST['product_id']) {
			die('Product ID is missing.');
		}

		// Ensure the quantity is provided
		if (!isset($_POST['quantity']) || !$_POST['quantity']) {
			die('Quantity is missing.');
		}

		// Get the product ID and quantity from the AJAX request
		$product_id = intval($_POST['product_id']);
		$quantity = intval($_POST['quantity']);

		// Ensure the product exists
		if (!wc_get_product($product_id)) {
			die('Invalid product.');
		}

		// Add the product to the cart
		$cart = WC()->cart;
		$cart->add_to_cart($product_id, $quantity);

		// Optionally, redirect to the cart page
		wp_redirect(wc_get_cart_url());
		exit;
	}
	/**
	 * Add configurator text to cart item.
	 *
	 * @param array $cart_item_data
	 * @param int   $product_id
	 * @param int   $variation_id
	 *
	 * @return array
	 */

	function add_configurator_text_to_cart_item(
		$cart_item_data,
		$product_id,
		$variation_id
		) {
		$configurator_text = filter_input(INPUT_POST, "configurator_text");
		if (empty($configurator_text)) {
		return $cart_item_data;
		}
		$cart_item_data["configurator_text"] = $configurator_text;
		return $cart_item_data;
	}



	add_filter(
		"woocommerce_add_cart_item_data",
		"add_configurator_text_to_cart_item",
		10,
		3
	);

	//add_filter( 'woocommerce_cart_item_thumbnail', 'custom_product_thumbnail_in_cart', 10, 3 );
	function custom_product_thumbnail_in_cart( $thumbnail, $cart_item, $cart_item_key ) {
		$configurator_image = filter_input(INPUT_POST, "configurator_image");
		$thumbnail = '<img src="' . $configurator_image . '" alt="' . esc_attr( $cart_item['data']->get_title() ) . '" width="100" height="100" />';
		return $thumbnail;
	}

	/**
	* Display configurator text in the cart.
	*
	* @param array $item_data
	* @param array $cart_item
	*
	* @return array
	*/

	function display_configurator_text_cart($item_data, $cart_item)
	{
		if (empty($cart_item["configurator_text"])) {
			return $item_data;
		}
		$item_data[] = [
			"key" => __("Configuration"),
			"value" => wc_clean($cart_item["configurator_text"]),
			"display" => "",
			"sanitize_callback" => "sanitize_text_field",
		];
		return $item_data;
	}

	add_filter(
		"woocommerce_get_item_data",
		"display_configurator_text_cart",
		10,
		2
	);

	function custom_order_item_meta($item_id, $values, $cart_item_key) {
		if (empty($values["configurator_text"])) {
			return;
		}
		wc_add_order_item_meta($item_id, 'Configuration', $values["configurator_text"]);
	}
	add_action('woocommerce_add_order_item_meta', 'custom_order_item_meta', 10, 3);

	// This shows the configurator tab on the product page in the backend
	function configurator_tab($original_tabs)
	{
		$new_tab["configurator"] = [
		"label" => __("Configurator", "woocommerce"),
		"target" => "configurator_options",
		"class" => ["show_if_configurator"],
		"sanitize_callback" => "sanitize_text_field",
		];

		// Code to reposition the tab
		$insert_at_position = 2; // This can be changed
		$tabs = array_slice($original_tabs, 0, $insert_at_position, true); // First part of original tabs
		$tabs = array_merge($tabs, $new_tab); // Add new
		$tabs = array_merge(
			$tabs,
			array_slice($original_tabs, $insert_at_position, null, true)
			); // Glue the second part of original
		return $tabs;
	}

	add_filter("woocommerce_product_data_tabs", "configurator_tab");

	// Contents of the configurator options product tab.

	function configurator_options_product_tab_content()
	{
		global $post, $product;

		// Ensure we have a product object
		if (is_object($product) && $product->get_id()) {
			$productId = $product->get_id();
		} else {
			$productId = isset($_GET['post']) ? $_GET['post'] : 0; // Fallback to post ID if product object is not available
		}
		?>
		<div id='configurator_options' class='panel woocommerce_options_panel'>
			<div class='options_group'>
				<div class='option configurator-sku'>
					<?php
						// Adding a field for inputting the product SKU
						woocommerce_wp_text_input([
							"id" => "_configurator_sku",
							"label" => __("Configurator SKU", "woocommerce"),
							"desc_tip" => "false",
							"type" => "input",
							"subtype" => "string",
							"sanitize_callback" => "sanitize_text_field",
						]);
					?>
					<span class="label">Matching the iONE article number. When empty the Woocommerce sku will be used</span>
				</div>
				<div class='option configurator-hook'>
					<?php
						// Checkbox for demo product
						woocommerce_wp_checkbox([
							"id" => "_configurator_demo",
							"label" => __("Use product page hook to display configurator", "woocommerce"),
							"desc_tip" => "false",
							"checked" => "checked",
						]);
					?>
					<span class="label">Check our iONE360 Woocommerce settings for available hooks</span>
				</div>
				<div class='option configurator-code'>
					<?php
						//   Keep Product Page intact
						woocommerce_wp_checkbox([
							'id' 		=> '_configurator_confirmation',
							'label' 	=> __( 'Use shortcode to display configurator', 'woocommerce' ),
							'desc_tip'	=> 'false',
						]);			
					?>
					<span class="label">shortcode: [ione360_woocommerce id=<?php echo $productId ?>]</span>
				</div>
				<div class='option configurator-type'>
					<?php
						//   Keep Product Page intact
						woocommerce_wp_checkbox([
							'id' 		=> '_configurator_type',
							'label' 	=> __( 'Use only 2D configurator', 'woocommerce' ),
							'desc_tip'	=> 'false',
						]);			
					?>
					<span class="label">When checked the 3D canvas for this product will be hidden</span>
				</div>		
				<div class='option configurator-hook'>
					<?php
						// Adding configurator to specific hook product page
						woocommerce_wp_text_input([
							"id" => "_configurator_hook",
							"label" => __("Hook placement configurator", "woocommerce"),
							"desc_tip" => "false",
							"type" => "input",
							"subtype" => "string",
							"sanitize_callback" => "sanitize_text_field",
						]);
					?>
					<span class="label">Adding configurator to specific hook product page. This will override the global hook setting only for this product</span>
				</div>				
			</div>
		</div>
	<?php
	}

	add_action("woocommerce_product_data_panels", "configurator_options_product_tab_content");

	// Save the custom fields.
	function save_configurator_option_fields($post_id)
	{
		$_configurator_demo = isset($_POST["_configurator_demo"]) ? "yes" : "no";
		$_configurator_only = isset($_POST["_configurator_confirmation"]) ? "yes" : "no";
		$_configurator_type = isset($_POST["_configurator_type"]) ? "yes" : "no";
		update_post_meta($post_id, "_configurator_type", $_configurator_type);
		update_post_meta($post_id, "_configurator_demo", $_configurator_demo);
		update_post_meta($post_id, "_configurator_confirmation", $_configurator_only);
		
		if (isset($_POST["_configurator_sku"])):
			update_post_meta(
				$post_id,
				"_configurator_sku",
				$_POST["_configurator_sku"]
			);
		endif;
		if (isset($_POST["_configurator_hook"])):
			update_post_meta(
				$post_id,
				"_configurator_hook",
				$_POST["_configurator_hook"]
			);
		endif;		
		$is_configurator = isset($_POST["_configurator"]) ? "yes" : "no";
		update_post_meta($post_id, "_configurator", $is_configurator);
	}

	add_action("woocommerce_process_product_meta_simple", "save_configurator_option_fields");
	add_action("woocommerce_process_product_meta_variable", "save_configurator_option_fields");

	// Use javascript to hide the tabs unless the configurator box is checked
	function wcpp_custom_style()
	{
	  ?>
		<style>
			#woocommerce-product-data ul.wc-tabs li.configurator_options a:before { font-family: WooCommerce; content: '\e010'; }
		</style>
		<script>
			jQuery( document ).ready( function( $ ) {
				$( 'input#_configurator' ).change( function() {
					var is_configurator = $( 'input#_configurator:checked' ).size();
					console.log( is_configurator );
					$( '.show_if_configurator' ).hide();
					$( '.hide_if_configurator' ).hide();

					if ( is_configurator ) {
						$( '.hide_if_configurator' ).hide();
					}
					if ( is_configurator ) {
						$( '.show_if_configurator' ).show();
					}
				});
				$( 'input#_configurator' ).trigger( 'change' );
			});
		</script><?php
	}
	add_action("admin_head", "wcpp_custom_style");

	// Add 'configurator' product option
	function add_configurator_product_option($product_type_options)
	{
		$product_type_options["configurator"] = [
			"id" => "_configurator",
			"sanitize_callback" => "sanitize_text_field",
			"wrapper_class" => "show_if_simple show_if_variable",
			"label" => __("Configurator", "woocommerce"),
			"description" => __(
				"Configurator products allow for the product to be configured in 3D.",
				"woocommerce"
			),
			"default" => "no",
		];
		return $product_type_options;
	}

	add_filter("product_type_options", "add_configurator_product_option");

	// Update the price of the product according to the configuration, IF it has been customized.
	add_filter("woocommerce_add_cart_item_data", "add_cart_price_data", 10, 3);

	function add_cart_price_data($cart_item_data, $product_id, $variation_id)
	{
		$configurator_price = filter_input(INPUT_POST, "configurator_price");

		// check if the product has been customized.
		if (!empty($_POST["configurator_text"])) {
			// Change the total price of the product here
			$cart_item_data["configurator_price"] = $configurator_price;
		}
		return $cart_item_data;
	}

	// Recalculate the totals and subtotals of the cart.
	add_action("woocommerce_before_calculate_totals", "before_calculate_totals", 10, 1);

	function before_calculate_totals($cart_obj)
	{
		if (is_admin() && !defined("DOING_AJAX")) {
			return;
		}
		// Iterate through each cart item
		foreach ($cart_obj->get_cart() as $key => $value) {
			if (isset($value["configurator_price"])) {
				$price = $value["configurator_price"];
				$value["data"]->set_price($price);
			}
		}
	}
} 
?>
