Very Simple Coupon mod but based on product not total

Completed modifications to the NOP Shopping Cart. Additions, modifications, and more.

Moderators: Koibito, Stefko, Randy, Rosey

Very Simple Coupon mod but based on product not total

Postby g-wiz » Thu Aug 09, 2012 3:40 pm

viewtopic.php?f=5&t=3420#p17688
Very Simple Coupon Codes" mod by Jason Butler v2

Code: Select all
    //--------------------------------------------------------------------||
    //  "Very Simple Coupon Codes" mod by Jason Butler v2                  ||
    //     based on original mod by Gene Eilebrecht                       ||
    //--------------------------------------------------------------------||
    // Function:  addCoupon                                               ||
    // PARAMETERS: Form Object, Text Box, SubTotal, NumberOrdered         ||
    // RETURNS: Cookie to user's browser, with prompt                     ||
    // PURPOSE: Tests for valid coupon code.  If valid, adds to cart.     ||
    //--------------------------------------------------------------------||
    function addCoupon(coupon,fTotal,iNumberOrdered){

    //----------Begin Listing of Coupons----------

    //-----5.00 Off Coupon-----
    if (coupon=="Test1"){
    validcoupon = true;
    strPRICE =-5.00;
    strNAME ="€5.00 Off Coupon";

    }

    //-----15% Off Coupon-----
    if (coupon=="justnatural"){
    validcoupon = true;
    strPRICE = -(fTotal * .15);
    strNAME ="15% Off Coupon";
    }

    //----------End of Coupon Listing----------


    //----------Add Coupon to cart----------
    if (validcoupon){
    iNumberOrdered++;
    strID_NUM = "Discount";
    strQUANTITY = "1";
    strSHIPPING = "0";
    strADDTLINFO = "";

    dbUpdatedOrder = strID_NUM + "|" +
    strQUANTITY + "|" +
    strPRICE + "|" +
    strNAME + "|" +
    strSHIPPING + "|" +
    strADDTLINFO;

    strNewOrder = "Order." + iNumberOrdered;
    SetCookie(strNewOrder, dbUpdatedOrder, null, "/");
    SetCookie("NumberOrdered", iNumberOrdered, null, "/");
    alert("Discount added: \n-------------------------------------\n" + strNAME)
    location.href=location.href;
    } else {
    alert("I'm sorry, that coupon code is invalid.  Please try another code.");
    }

    }

    //---------------------------------------------------------------------||
    // FUNCTION: CheckDiscountAdded ||
    // PARAMETERS: None  ||
    // RETURNS: Null ||
    // PURPOSE: Checks if discount added and removes if so ||
    //---------------------------------------------------------------------||

    function CheckDiscountAdded(){
    // alert("CheckDiscountAdded");
    iNumberOrdered = GetCookie("NumberOrdered");
    if ( iNumberOrdered == null )
    iNumberOrdered = 0;

    for ( i = 1; i <= iNumberOrdered; i++ ) {
             even = Math.round(i/2);
          NewOrder = "Order." + i;

    database = "";
    database = GetCookie(NewOrder);

    Token0 = database.indexOf("|", 0);
    Token1 = database.indexOf("|", Token0+1);
    Token2 = database.indexOf("|", Token1+1);
    Token3 = database.indexOf("|", Token2+1);
    Token4 = database.indexOf("|", Token3+1);

    fields = new Array;
    fields[0] = database.substring( 0, Token0 );
    fields[1] = database.substring( Token0+1, Token1 );
    fields[2] = database.substring( Token1+1, Token2 );
    fields[3] = database.substring( Token2+1, Token3 );
    fields[4] = database.substring( Token3+1, Token4 );
    fields[5] = database.substring( Token4+1, database.length );

    if( fields[0] == 'Discount') {
    //alert("Discount added: TRUE " + i)
    RemoveFromCart(i)
    }

    }
    }


Once you have that installed and working I have found a way to apply the discount to certain products only and not the entire cart...
so for example a store sells different wines such as chenin, shiraz, cabernet etc etc

firstly here is an example of your product html page with ID_NUM value in this case Chen Blanc Case is CBCA
Code: Select all
<input type="hidden" name="ID_NUM" value="CBCA">


You will want to modify your nopcart discount mod to the following

under
Code: Select all
function addCoupon(coupon,fTotal,iNumberOrdered){


place in the following so that it loops through your cookie
Code: Select all
var iNumberOrdered = 0;
iNumberOrdered = GetCookie("NumberOrdered");
if ( iNumberOrdered == null )
iNumberOrdered = 0;


for ( i = 1; i <= iNumberOrdered; i++ ) {
even = Math.round(i/2);
NewOrder = "Order." + i;
database = "";
database = GetCookie(NewOrder);

Token0 = database.indexOf("|", 0);
Token1 = database.indexOf("|", Token0+1);
Token2 = database.indexOf("|", Token1+1);
Token3 = database.indexOf("|", Token2+1);
Token4 = database.indexOf("|", Token3+1);

fields = new Array;
fields[0] = database.substring( 0, Token0 ); 
fields[1] = database.substring( Token0+1, Token1 ); 
fields[2] = database.substring( Token1+1, Token2 );
fields[3] = database.substring( Token2+1, Token3 );
fields[4] = database.substring( Token3+1, Token4 ); 
fields[5] = database.substring( Token4+1, database.length );


now add in your if statement based on the product ID that you set on your html page

Code: Select all
//if coupon code = CHENINCASE and product id = CBCA (Chenin Blanc Case) then take off $20
   if (coupon=="CHENINCASE" && fields[0]== "CBCA"){
    validcoupon = true;
   myInt = -20;
    fTotal -= myInt;
   strPRICE = myInt;
    strNAME ="$20 Off Coupon Chenin Blanc Case Only";   
    }   


then leave this all the same
Code: Select all
if (validcoupon){
    iNumberOrdered++;
    strID_NUM = "Discount";
    strQUANTITY = "1";
   strSHIPPING = "0";
    strADDTLINFO = "0";

    dbUpdatedOrder = strID_NUM + "|" +
    strQUANTITY + "|" +
   strPRICE + "|" +
   strNAME + "|" +
   strSHIPPING + "|" +
   strADDTLINFO;

    strNewOrder = "Order." + iNumberOrdered;
    SetCookie(strNewOrder, dbUpdatedOrder, null, "/");
    SetCookie("NumberOrdered", iNumberOrdered, null, "/");
    alert("Discount added: \n-------------------------------------\n" + strNAME)
    location.href=location.href;
    } else {
    alert("I'm sorry, that coupon code is invalid.  Please try another code.");
    }

    }


So it should look like as a whole
Code: Select all
function addCoupon(coupon,fTotal,iNumberOrdered){

var iNumberOrdered = 0;

iNumberOrdered = GetCookie("NumberOrdered");
if ( iNumberOrdered == null )
iNumberOrdered = 0;


for ( i = 1; i <= iNumberOrdered; i++ ) {
even = Math.round(i/2);
NewOrder = "Order." + i;
database = "";
database = GetCookie(NewOrder);

Token0 = database.indexOf("|", 0);
Token1 = database.indexOf("|", Token0+1);
Token2 = database.indexOf("|", Token1+1);
Token3 = database.indexOf("|", Token2+1);
Token4 = database.indexOf("|", Token3+1);

fields = new Array;
fields[0] = database.substring( 0, Token0 ); 
fields[1] = database.substring( Token0+1, Token1 ); 
fields[2] = database.substring( Token1+1, Token2 );
fields[3] = database.substring( Token2+1, Token3 );
fields[4] = database.substring( Token3+1, Token4 ); 
fields[5] = database.substring( Token4+1, database.length );
   
//if coupon code = CHENINCASE and product id = CBCA (Chenin Blanc Case) then take off $20
   if (coupon=="CHENINCASE" && fields[0]== "CBCA"){
    validcoupon = true;
   myInt = -20;
    fTotal -= myInt;
   strPRICE = myInt;
    strNAME ="$20 Off Coupon Chenin Blanc Case Only";   
    }   
   
}    
    if (validcoupon){
    iNumberOrdered++;
    strID_NUM = "Discount";
    strQUANTITY = "1";
   strSHIPPING = "0";
    strADDTLINFO = "0";

    dbUpdatedOrder = strID_NUM + "|" +
    strQUANTITY + "|" +
   strPRICE + "|" +
   strNAME + "|" +
   strSHIPPING + "|" +
   strADDTLINFO;

    strNewOrder = "Order." + iNumberOrdered;
    SetCookie(strNewOrder, dbUpdatedOrder, null, "/");
    SetCookie("NumberOrdered", iNumberOrdered, null, "/");
    alert("Discount added: \n-------------------------------------\n" + strNAME)
    location.href=location.href;
    } else {
    alert("I'm sorry, that coupon code is invalid.  Please try another code.");
    }

    }


now you can customize your coupons so that they can only apply to certain items :)

example:
Code: Select all
//if coupon code = special and product id = RESERVE and quantity ordered is 6 or more then give a $50 discount
   if (coupon=="special" && fields[0]== "RESERVE" && fields[1]>= "6"){
    validcoupon = true;
   myInt = -50;
    fTotal -= myInt;
   strPRICE = myInt;
    strNAME ="$50 Off Your 6 or more bottles Reserve Order";   
    }
g-wiz
Guru
 
Posts: 32
Joined: Mon Nov 22, 2004 3:30 am

Return to Modifications

Who is online

Users browsing this forum: No registered users and 1 guest

cron