Zen Cart Logo
Forums / General Questions / How do I make it remember custom form data in cart between logins?

How do I make it remember custom form data in cart between logins?

Locked

Views: 1,813

Results 1 to 4 of 4
This thread is locked. New replies are disabled.
15 May 2009, 11:16 PM
#1
ca_zcuser avatar

ca_zcuser

New Zenner

Join Date:
Oct 2008
Posts:
5
Plugin Contributions:
0

How do I make it remember custom form data in cart between logins?

I'm trying to set up custom forms for each product in my store. I made two new database tables. One table, zen_forms, contains an HTML form for each product. (One field, "forms_fields", contains XML data that describes each form field. As you'll see in the code below, this field's contents are interpreted using SimpleXML.) The other table, zen_orders_forms, contains serialized user submitted data for each order of each product. When the customer adds a product to their cart, they're asked to fill out the associated form. We manage every form the customer fills out per session by serializing form $_POST data and storing it in an array, $forms, which in turn is serialized and stored in $_SESSION['forms']. Upon checkout, form data for each form in the order is entered into the database table zen_orders_forms.

So far, everything works, as long as the user does not log out in the middle of the checkout process. If they do and log back in, their form input data is not restored, but of course, their shopping cart, and possibly other session information, is restored. What am I doing wrong?

Below are the contents of the header_php.php file for the page where form data for the product being added to the cart is collected.

<?php
	require(DIR_WS_MODULES . zen_get_module_directory('require_languages.php'));
	
	if(!isset($_GET['products_id'])) {
		zen_redirect(zen_href_link(FILENAME_DEFAULT));
	} else if($_SESSION['cart']->count_contents() <= 0) {
		zen_redirect(zen_href_link(FILENAME_TIME_OUT));
	} else if(!$_SESSION['customer_id']) {
		$_SESSION['navigation']->set_snapshot();
		zen_redirect(zen_href_link(FILENAME_LOGIN, '', 'SSL'));
	} else if (zen_get_customer_validate_session($_SESSION['customer_id']) == false) {
		$_SESSION['navigation']->set_snapshot();
		zen_redirect(zen_href_link(FILENAME_LOGIN, '', 'SSL'));
	} else {
		$products_id = (int)$_GET['products_id'];
		
		$forms = isset($_SESSION['forms']) ? unserialize($_SESSION['forms']) : array();
		$goto_cart = false;
		if(isset($_POST) && !empty($_POST)) {
			$forms[$products_id]['post'] = $_POST;
			$goto_cart = true;
		}
		
		if(isset($forms[$products_id])) {
			$post = $forms[$products_id]['post'];
			foreach($post as $key => $value) {
				$_POST[$key] = $value;
			}
		} else {
			$sql = "
				SELECT f.products_id, f.forms_html, f.forms_fields, pd.products_name
				FROM %s f
				INNER JOIN %s pd
				ON f.products_id=pd.products_id
				WHERE f.products_id=%d";
			$sql = sprintf($sql, TABLE_FORMS, TABLE_PRODUCTS_DESCRIPTION, $products_id);
			$form_result = $db->Execute($sql);
			
			if(!empty($form_result->fields)) {
				$forms[$products_id] = array(
					'product_name' => $form_result->fields['products_name'],
					'html' => $form_result->fields['forms_html'],
					'fields' => $form_result->fields['forms_fields']);
			}
		}
		
		$_SESSION['forms'] = serialize($forms);
		
		$form_fields = @simplexml_load_string($forms[$products_id]['fields']);
		
		$form_html = $forms[$products_id]['html'];
		foreach($form_fields as $field) {
			$attrs = $field->attributes();
			$name = (string)$attrs->name;
			$default = isset($attrs->default) ? (string)$attrs->default : '';
			$value = isset($_POST[$name]) ? $_POST[$name] : '';
			
			switch($attrs->type) {
				case 'text':
					$form_html = str_replace(sprintf(':%s:', $name), $value, $form_html);
					break;
				case 'checkbox':
					$form_html = str_replace(sprintf(':%s:', $name), empty($value) ? '' : 'checked="checked"', $form_html);
					break;
				case 'radio':
					$form_html = str_replace(sprintf(':%s:', $name), ($value == $default) ? 'checked="checked"' : '', $form_html);
					break;
				case 'select':
					foreach($field->children() as $option) {
						$option_value = (string)$option;
						if($value == $option_value) {
							$form_html = str_replace(sprintf(':%s:', $name), 'selected="selected"', $form_html);
							break;
						}
					}
					break;
				case 'textarea':
					$form_html = str_replace(sprintf(':%s:', $name), htmlspecialchars($value), $form_html);
					break;
			}
		}
		
		if($goto_cart) {
			zen_redirect(zen_href_link(FILENAME_SHOPPING_CART));
		}
	}
?>
16 May 2009, 6:27 AM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: How do I make it remember custom form data in cart between logins?

Contents of the customer's shopping-cart (basket) are store in 2 database tables: customers_basket and customers_basket_attributes. That data is updated every time someone adds something to their cart, or adjusts its contents. If you've written custom code that bypasses the built-in handling of products and attributes, you'll have to expand your custom code to store that data if you want to be able to retrieve it between sessions.

16 May 2009, 7:48 PM
#3
ca_zcuser avatar

ca_zcuser

New Zenner

Join Date:
Oct 2008
Posts:
5
Plugin Contributions:
0

Re: How do I make it remember custom form data in cart between logins?

DrByte:

Contents of the customer's shopping-cart (basket) are store in 2 database tables: customers_basket and customers_basket_attributes. That data is updated every time someone adds something to their cart, or adjusts its contents. If you've written custom code that bypasses the built-in handling of products and attributes, you'll have to expand your custom code to store that data if you want to be able to retrieve it between sessions.

Doesn't all session data get automatically stored in the database table zen_sessions?

16 May 2009, 9:53 PM
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: How do I make it remember custom form data in cart between logins?

Sure, but sessions do expire, and definitely do not survive logouts.