[ic] Solution for Discounts Using Discount Codes Stored in Database
(code included)
Duane Hinkley
duane at DownHomeWebDesign.com
Sun Jun 6 12:12:46 EDT 2004
Interchange Users,
My client Ultimate Engineering (unlimitedengineering.com) likes to have
any custom code I write for him to be donated to the icdevgroup. He
hopes the code to be part of the standard Interchange distribution to be
used by all and be improved on.
I just finished writing a short piece of code to allow him to give
discounts based on special discount codes he sends to customers. This
code is writing to run as a usertag inside the catalog.cfg file. So
those of you who don't own their own servers will like this one. The
code can easily modified to be a system wide usertag.
Here's the code and installation instructions.
catalog.cfg:
=======================================================
1. Add the following line. I added it to the "Database Setup" section:
Database discountcodes discountcodes.txt TAB
2. Add the following code to the bottom of the file:
UserTag discountcodes Order code
UserTag discountcodes Routine <<EOR
sub {
my ($code) = @_;
my $q;
my $db;
my $ary;
my $discount_value;
my $fail = 0;
my $result;
my $err_msg;
$code =~ s/\s+$//g; # Trims trailing blanks
$code =~ s/^\s+//g; # Trims leading blanks
$code =~ tr/[a-z]/[A-Z]/; # Makes all letters upper case
if ( $code ) {
if ( $Vend::Database{"discountcodes"} ) {
$db = ::database_exists_ref("discountcodes");
$q = qq{SELECT discount_code, discount_amt,
discount_percentage, discount_description FROM discountcodes WHERE
discount_code = $code };
$ary = $db->query($q);
if( $ary and $ary->[0] and $code eq $ary->[0][0] ) {
if ( $ary->[0][1] ) { # Discount specific amount
$discount_value = $ary->[0][1];
$Tag->discount( { code => 'ALL_ITEMS'}, "\$s -
$discount_value " );
$result = 1;
}
if ( $ary->[0][2] ) { # Discount percentage
$discount_value = $ary->[0][2];
$Tag->discount( { code => 'ALL_ITEMS'}, "\$s - \$s *
$discount_value" );
$result = 1;
}
}
else {
$result = 0;
}
}
}
else {
$result = 1;
}
return $result;
}
EOR
etc/profiles.order:
==========================================================================
1. Add the following section: (I use multiple page checkouts)
__NAME__ discount_code
&set=discount_result [discountcodes code="[cgi discount_code]" ]
&fatal = yes
__END__
pages/ord/checkout3.html:
===========================================================================
1. This could be any of the multiple checkout pages. Or, I'm sure you
could include it in a single checkout page provoided you made the
appropriate changes to the profiles.order. Add the following to the
top of the appropriate checkout page to report errors:
[if value discount_result != 1 ]
<P>
<B>[L]There were errors in your last submission[/L]:<br>
<blockquote>
<FONT color="__CONTRAST__">
Error: Invalid Discount Code
</font>
</blockquote>
</p>
[value name=discount_code set="[cgi
discount_code]" hide=1]
[/if]
2. Use the following for the user to enter the discount code:
<b>[error name=discount_result std_label="[L]Discount Code:[/L]"
] <INPUT TYPE=text NAME=discount_code value="[cgi
discount_code]"SIZE=6></b>
3. Set the profile to the page to use to our discount profile:
<INPUT TYPE=hidden NAME=mv_form_profile
VALUE=discount_code>
4. Here's all the code of my checkout3.html
----------------------------------------------------------------------------------------------------
===================================================================
-----------------------------------------------------------------------------------------------------
[comment]
ui_page: ord/checkout3.html
ui_type: page
ui_name: ord/checkou3t.html
ui_page_template: noleft
ui_version: 4.9.9
ui_source: pages/ord/checkout3.html
ui_template: Yes
ui_template_name: noleft
[/comment]
[tmp page_title]__COMPANY__ -- [L]Check Out[/L][/tmp]
[tmpn members_only]0[/tmpn]
[control reset=1]
[control-set]
[component][/component]
[/control-set]
[control reset=1]
@_NOLEFT_TOP_@
<!-- BEGIN CONTENT -->
[if session logged_in]
[else]
[bounce href="[area href=ord/checkout secure=1]"]
[/else]
[/if]
[if items]
[else]
[bounce href="[area href=ord/basket secure=1]"]
[/else]
[/if]
[comment] if we came from checkout 2, save the shipping information
and get the billing information. [/comment]
<table width="95%">
<tr>
<td align="left" width="100%">
<P>
<div align=center><h1>Mailing Preferences & Discount</h1></center>
[comment] Remove any previous discounts [/comment]
[discount code="ALL_ITEMS"][/discount]
[if cgi checkout5_pg]
[userdb function=load hide=1 ]
[seti email_copy][data table="userdb" column="email_copy"
key="[value mv_username]" ][/seti]
[value name="email_copy" set="[scratch email_copy]" hide=1 ]
[value name="discount_result" set="" hide=1 ]
[seti clear_errors][error all=1 hide=1 comment="Clear errors"][/seti]
[/if]
[if cgi checkout2_pg ]
[if value new_account]
[seti nill]
[email-raw][include etc/new_account_notice][/email-raw]
[value name="new_account" set="" hide=1 ]
[value name="discount_result" set="" hide=1 ]
[/seti]
[/if]
[if cgi mv_same_billing]
[loop list="fname lname company address1 address2 city state zip
country phone"]
[value name="b_[loop-code]" set="[value [loop-code]]" hide=1]
[/loop]
[/if]
[comment] Make sure proper case for Authorizenet [/comment]
[loop list="fname lname company address1 address2 city state b_fname
b_lname b_company b_address1 b_address2 b_city b_state"]
[perl]
my $field = "[loop-code]";
$Values->{$field} =~ s/(?<!\')\b(\w+)/\u$1\E/g;
return "";
[/perl]
[/loop]
[userdb function=save hide=1]
[userdb function=set_shipping nickname=default hide=1]
[userdb function=set_billing nickname=default hide=1]
[seti email_copy][data table="userdb" column="email_copy"
key="[value mv_username]" ][/seti]
<p align=center><font class=green_message>Shipping & Billing Address
Saved</font></p>
[else]
[if type=explicit compare="[error all=1 show_var=1 keep=1]"]
<P>
<B>[L]There were errors in your last submission[/L]:<br>
<blockquote>
<FONT color="__CONTRAST__">
[error all=1 keep=1 show_error=1 show_label=1 joiner="<br>"]
</font>
</blockquote>
</p>
<p>
[msg arg.0="<FONT color='__CONTRAST__'>" arg.1="</FONT>"]The
areas you need to correct are shown in %sthis color%s below.[/msg]</B>
</p>
[/if]
[if value discount_result != 1 ]
<P>
<B>[L]There were errors in your last submission[/L]:<br>
<blockquote>
<FONT color="__CONTRAST__">
Error: Invalid Discount Code
</font>
</blockquote>
</p>
[value name=discount_code set="[cgi
discount_code]" hide=1]
[/if]
[/else]
[/if]
</td>
</tr>
<tr>
<td>
<table align="center">
<tr>
<td align="center">
[L]Select your mailing preferences and enter any discount
code. Click the <b>'Next'</b> button to proceed to the next step.[/L]
</td>
</tr>
<tr>
<td>
<br>
<form action="[area href='ord/checkout2' secure=1]"
method="POST" name=bill_address_back>
<input type=hidden NAME=mv_session_id
value="[data session id]">
<INPUT TYPE=hidden NAME=mv_action VALUE=back >
<INPUT TYPE=hidden NAME=mv_fail_href
VALUE=ord/checkout3>
<INPUT TYPE=hidden NAME=mv_success_href
VALUE=ord/checkout2>
</form>
<!-- Start Billing Form -->
<form action="[process secure=1]" method="POST"
name=bill_address>
<input type=hidden NAME=mv_session_id value="[data
session id]">
<INPUT TYPE=hidden NAME=mv_form_profile
VALUE=discount_code>
<INPUT TYPE=hidden NAME=mv_action VALUE=return >
<INPUT TYPE=hidden NAME=mv_fail_href
VALUE=ord/checkout3>
<INPUT TYPE=hidden NAME=mv_success_href
VALUE=ord/checkout5>
<INPUT TYPE=hidden NAME=checkout3_pg VALUE=true >
<table cellspacing=0 cellpadding=0 width="100%">
<tr>
<td>
</td>
</tr>
<tr>
<td>
<br>
<!-- START Discount Form -->
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr class="contentbar2">
<td colspan=4 class="contentbar2"><b>[L]Discount
Code[/L]</b></td>
</tr>
<tr>
<td colspan=4 class="contentbar1"> </td>
</tr>
<tr>
<td class="contentbar1"> </td>
<td class="contentbar1">
<b>[error name=discount_result std_label="[L]Discount Code:[/L]"
] <INPUT TYPE=text NAME=discount_code value="[cgi
discount_code]"SIZE=6></b>
</td>
<td colspan="2" class="contentbar1">
([L]If you where provided a discount code
enter it here to receive your discount.[/L])
</td>
</tr>
<tr>
<td class="contentbar1" colspan="4"> </td>
</tr>
</table>
</td>
</tr>
</table>
<!-- END Discount Code Form -->
</td>
</tr>
<tr>
<td>
<br>
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<td>
<table border="0" align="center" cellspacing="0" width="100%">
<tr class="contentbar2">
<td class="contentbar2" colspan=2><b>[L]Mailing
Status[/L]</b></td>
</tr>
<tr>
<td align="left" class="contentbar1" >
<input type=checkbox name=email_copy VALUE="1" [checked
name="email_copy" value="1" ] > <B>[L]Send an email copy of my
receipt[/L]</B><br>
</td>
</tr>
[if variable CHECKOUT_MAIL_LIST]
<tr>
<td align="left" valign="middle" class="contentbar1" >
<br>
<B>[L]Mail lists (if any)[/L]</B>
[seti mlist][perl tables="__UI_META_TABLE__ mv_metadata"]
return;
[/perl]
[input-filter name=mail_list op=null_to_space][/input-filter]
[display table=userdb type=checkbox column=mail_list
key="[data session username]"][/seti]
[perl]
my $mailing_list = $Scratch->{mlist};
$mailing_list =~ s/</<br></g;
return $mailing_list;
[/perl]
</td>
</tr>
[/if]
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<br>
<br>
<br>
<br>
</td>
</tr>
</table>
<input type=button
onclick="bill_address_back.submit();" value="<-- [L]Back[/L]"
class=button3>
<input type=submit value="Next -->" class=button1
style="font-weight: bold">
[seti clear_errors][error all=1 hide=1 comment="Clear
errors"][/seti]
</form>
<!-- End Billing Form -->
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
<!-- END CONTENT -->
@_NOLEFT_BOTTOM_@
----------------------------------------------------------------------------------------------------
===================================================================
-----------------------------------------------------------------------------------------------------
products/discountcodes.txt
====================================================================
1. Create the discountcodes.txt database file as follows:
discount_code<tab>discount_amt<tab>discount_percentage<tab>discount_description
DISC2<tab>2<tab>Give $2.00 discount
DISC1<tab>0<tab>.25<tab>This is a test discount
Sincerely,
Duane Hinkley
Down Home Web Design, Inc.
9494 W. Fairview Ave., Suite C
Boise, Idaho 83704
(208) 424-0572
duane at dhwd.com
www.downhomewebdesign.com
==========================================
Interchange Consulting & Perl Development
==========================================
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3210 bytes
Desc: S/MIME Cryptographic Signature
Url : http://www.icdevgroup.org/pipermail/interchange-users/attachments/20040606/a5800761/smime.bin
More information about the interchange-users
mailing list