Hi all, I've got this mod done and thought I'd share it - allows you to save multiple ship-to addresses on the profile page.
It creates a nice drop-down menu to choose your profile from, and allows you to switch, add, and delete profiles.
Once you've got it going - realize that you have to save your profile the regular x-cart way, and once it's saved you can then hit "create saved profile" to save a copy of it later. Then, modify your profile, click the regular submit to save it, and then click on the "create saved profile" - this will keep adding to the profiles available to "switch to".
First, you need to create a php file called createAlt.php and put it in your root directory:
Code:
<?
//this script is to backup / restore user profiles
require "top.inc.php";
require "config.php";
db_connect($sql_host, $sql_user, $sql_password);
if($createAltFunc == "create"){
Header("Content-type: text/html");
#$my_alt = array_pop(func_query_first("select max(profile) from alt_profiles where login='$uname'"));
#if($my_alt == null){
# $my_alt = 0;
#}
// first write the existing data to alt_profiles in the appropriate place
$data = func_query_first("select * from xcart_customers where login='$uname'");
$profile = func_query_first("select max(profile) as max from xcart_shipping_addresses where login='$uname'");
#$profile0 = 0 + $profile[max];
$profile0 = 0 + $data[profile];
$profile1 = $profile0 + 1 + $profile[max];
foreach($data as $me => $crap){
#print "ME $me $crap
";
}
db_query("update xcart_customers set profile=$profile1 where login='$uname'");
//db_query("delete from xcart_shipping_addresses where login='$uname' and profile=$profile0");
//db_query("insert into xcart_shipping_addresses (login, profile, firstname, lastname, s_address, s_city, s_state, s_country, s_zipcode ) values ('$uname', $profile0, '$data[firstname]','$data[lastname]', '$data[s_address]','$data[s_city]','$data[s_state]','$data[s_country]','$data[s_zipcode]')");
db_query("insert into xcart_shipping_addresses (login, profile, s_firstname, s_title, s_lastname, s_address, s_city, s_state, s_country, s_zipcode ) values ('$uname', $profile1, '$s_firstname', '$s_title' , '$s_lastname', '$s_address\n$s_address_2', '$s_city','$s_state','$s_country','$s_zipcode')");
//db_query();
//Header("Location: register.php?mode=update&action=cart");
//switch to this profile
#Header("Content-type: text/html");
$sdata = db_query("select * from xcart_sessions_data");
while ($row = db_fetch_array($sdata)) {
$mdata = unserialize($row[data]);
#print "$mdata[login] $row[sessid]
";
if($mdata[login] == $uname){
$mdata[s_firstname] = $data[s_firstname];
$mdata[s_lastname] = $data[s_lastname];
$mdata[s_title] = $data[s_title];
$sessid = $row[sessid];
$new;
foreach($mdata as $key => $value){
#print "MDATA $key $mdata[$key]
";
if($key != "extended_userinfo"){
$new[$key] = $value;
}
else{
$edata = unserialize($mdata[$key]);
$edata[s_firstname] = $data[s_firstname];
$edata[s_lastname] = $data[s_lastname];
$edata[s_title] = $data[s_title];
$new[$key] = serialize($edata);
}
}
db_query("update xcart_sessions_data set data = '".serialize($new)."' where sessid='$sessid' ");
#break;
}
}
#//Header ("Location: createAlt.php?createAltFunc=switch&switchProfile=$profile0&uname=$uname");
Header("Location: register.php?mode=update&action=cart");
}
elseif($createAltFunc == "switch"){
// save data to alt_profile on switch
// write current profile back to alt_profiles
// get the data
$data = func_query_first("select * from xcart_customers where login='$uname'");
// save the data
// but first check to make sure the record exists
$exists = array_pop(func_query_first("select count(*) from xcart_shipping_addresses where login='$uname' and profile=$data[profile]"));
if($exists == 0){
db_query("insert into xcart_shipping_addresses (login, profile) values ('$uname', $data[profile])");
}
foreach($data as $key => $value){
db_query("update xcart_shipping_addresses set $key='$value' where login='$uname' and profile=$data[profile]");
}
// then copy the desired profile's data over the old
$data = func_query_first("select * from xcart_shipping_addresses where login='$uname' and profile=$switchProfile");
foreach($data as $key => $value){
if($key == "profile"){
db_query("update xcart_customers set profile=$value where login='$uname'");
}
elseif($key == "s_firstname" || $key == "s_lastname" || $key == "s_title"){
//dont need to run these into main db
}
else{
db_query("update xcart_customers set $key='$value' where login='$uname'");
}
}
// now update the session
#Header("Content-type: text/html");
$sdata = db_query("select * from xcart_sessions_data");
while ($row = db_fetch_array($sdata)) {
$mdata = unserialize($row[data]);
#print "$mdata[login] $row[sessid]
";
if($mdata[login] == $uname){
$new;
$sessid = $row[sessid];
foreach($mdata as $key => $value){
#print "MDATA $key $mdata[$key]
";
if($key != "extended_userinfo"){
$new[$key] = $value;
}
else{
$edata = unserialize($mdata[$key]);
foreach($mdata as $ekey => $eval){
if($key == "extended_userinfo"){
foreach($eval as $euikey => $euival){
#print "EUIkey $euikey value $euival
";
if($euikey == "s_firstname"){
#print "VAL FOUND $eval[$euikey]";
$eval[$euikey] = $data[s_firstname];
}
if($euikey == "s_lastname"){
#print "VAL FOUND $eval[$euikey]";
$eval[$euikey] = $data[s_lastname];
}
if($euikey == "s_title"){
#print "VAL FOUND $eval[$euikey]";
$eval[$euikey] = $data[s_title];
}
}
}
$edata[$ekey] = $eval;
#print "key $ekey value $eval
";
}
#$edata[s_firstname] = $data[s_firstname];
#$edata[s_lastname] = $data[s_lastname];
#$edata[s_title] = $data[s_title];
$new[$key] = serialize($edata);
}
}
db_query("update xcart_sessions_data set data = '".serialize($new)."' where sessid='$sessid' ");
#break;
}
}
#Header("Content-type: text/html");
Header("Location: register.php?mode=update&action=cart");
}
elseif($createAltFunc == "delete"){
db_query("delete from xcart_shipping_addresses where login='$uname' and profile=$deleteProfile");
Header("Location: register.php?mode=update&action=cart");
}
?>
Second, you will need to create a new table in the mysql database - you can use this dump file I created:
Code:
--
-- Table structure for table `xcart_shipping_addresses`
--
CREATE TABLE `xcart_shipping_addresses` (
`addr_id` int(8) NOT NULL auto_increment,
`login` varchar(32) NOT NULL default '',
`company` varchar(255) NOT NULL default '',
`s_address` varchar(255) NOT NULL default '',
`s_city` varchar(255) NOT NULL default '',
`s_county` varchar(32) NOT NULL default '',
`s_state` varchar(32) NOT NULL default '',
`s_country` char(2) NOT NULL default '',
`profile` int(11) default NULL,
`s_zipcode` varchar(32) default NULL,
`s_firstname` varchar(32) default NULL,
`s_lastname` varchar(32) default NULL,
`s_title` varchar(32) default NULL,
UNIQUE KEY `addr_id` (`addr_id`)
) TYPE=MyISAM AUTO_INCREMENT=396 ;
Also, alter the customers table like so (thanks techhead):
Code:
ALTER TABLE `xcart_customers` ADD `profile` INT( 11 ) DEFAULT '0' NOT NULL ;
Third, you will need to modify and create some .tpl files:
Create a new .tpl file and put it in the skin1/main folder and call it register_switch_profile.tpl - here is the code:
Code:
{* $Id: register_switch_profile.tpl*}
<tr valign=middle>
<td height=20 colspan=3>Switch Shipping Profile<hr size=1 noshade></td>
</tr>
<tr valign=middle>
<td align=right>Choose Profile</td>
<td></td>
<td nowrap>
<select name=switchProfile>
{section name=switchProfile loop=$alt_profiles}
{*<option {if $userinfo.title eq $alt_profiles[title]}selected{/if}>{$alt_profiles[title]}</option>*}
<option value="{$alt_profiles[switchProfile]}">{$alt_profile_names[switchProfile]}
{sectionelse}
<option>No Alternates Available
{/section}
</select><input onClick="javascript: submitAlternateProfile(document.registerform, 'switch');" type="button" name="switch" value="Switch Now" {if $smarty.section.switchProfile.total == 0}disabled{/if}>
</td>
</tr>
Now create another new .tpl file called register_delete_altprofile.tpl and put it in this same spot.
Code:
{* $Id: register_delete_altprofile.tpl*}
<tr valign=middle>
<td height=20 colspan=3>Remove Shipping Profile<hr size=1 noshade></td>
</tr>
<tr valign=middle>
<td align=right>Delete Profile</td>
<td></td>
<td nowrap>
<select name=deleteProfile>
{section name=switchProfile loop=$alt_profiles}
{*<option {if $userinfo.title eq $alt_profiles[title]}selected{/if}>{$alt_profiles[title]}</option>*}
<option value="{$alt_profiles[switchProfile]}">{$alt_profile_names[switchProfile]}
{sectionelse}
<option>No Alternates Available
{/section}
</select><input onClick="javascript: if(confirm('Are you sure you want to delete?'))submitAlternateProfile(document.registerform, 'delete')" type="button" name="switch" value="Delete" {if $smarty.section.switchProfile.total == 0}disabled{/if}>
</td>
</tr>
Now modify skin1/customer/main/register.tpl - adding this into your chosen location depending on where you want it to showup in the form:
Code:
{* Borealis Media - "profile switch" -grep- the other half of this is in register_contact_info.tpl*}
{if $smarty.get.mode eq "update"}
{include file="main/register_switch_profile.tpl" userinfo=$userinfo}
{include file="main/register_delete_altprofile.tpl" userinfo=$userinfo}
{/if}
{*/Borealis Media - this is membership profile switch (change) code*}
Then, in this same file, somewhere in the "hidden inputs" or below the regular submit button, add another like this that will show the user the new create saved profile button:
Code:
<input type="hidden" name="createAltFunc">
After hitting the "submit" button above to save your profile, you can then click on "create saved profile" to save this profile for a later use in the dropdown menu above.
<input type="button" name="create" value="Create Saved Profile" onClick="submitAlternateProfile(document.registerform, 'create')" >
Ok, now last step - modify one of the "register" .tpl's in the skin1/main folder to add this - I've put mine in the register_contact_info.tpl but it doesn't matter which you put it in.
Code:
{* Borealis Media - the other half of this is in register.tpl*}
{if $smarty.get.mode eq "update" }
<tr><Td colspan="3" align="center">
{literal}
<script language="JavaScript1.2">
function submitAlternateProfile(obj, func){
obj.createAltFunc.value = func;
obj.action = 'createAlt.php';
obj.submit();
}
</script>
{/literal}
</td></tr>
{/if}
{*/Borealis Media*}
Cheers, hope you all find some use of this mod, took me awhile ;o) BTW - I've got it working on both the 3.5.x branch and the 4.0.x branch.
Instinctual
Colorado
X-Cart Versions 3.5.x - 4.0.x