AMPscript 401
This scenario includes information on these aspects of using AMPscript:
- Using for loops as part of an email send
- Using AMPscript to change the appearance of an email send
- Filtering message context
- Creating content from blocks of code
This AMPscript provides the content for the entire email message:
The first part of the code appears the same as the code found in AMPscript 301, but the second part inserts information about a subscriber's past purchases and total amount spent into the email. First, the code creates a new set of variables to use in inserting product and price information into the email:
Next, the @totalPurchases and @indicator variables receive values. The code uses @totalPurchases to store the subscriber's total purchases in dollar value, and it uses @indicator to alternate the background color for each individual purchase:
Once the variables receive their initial values, the code consults the Purchases data extension to find any previous purchases from a subscriber based on a member ID.
As long as a row matches the for loop's condition, the code performs these actions. First, the code assigns values to some of the variables declared earlier. The Field() function sets values for the fields in that specific location, and the Add() function adds the price of the individual item to the @totalPurchase running total.
The code also sets the background color to white or gray, depending on what the previous entry's background color. Odd number entries get a background color of white, while even entries get a gray background color.
Finally, the code takes the information and formats it in the same manner as the content block referred to in the folder location in quotation marks. The code then proceeds to the next row. After using all rows, the code moves on to calculating the total purchases.
The final part of the code makes sure a zero appears after a single digit decimal point value in @totalPurchase or two zeroes after a whole number value in @totalPurchase. The code also prints the @totalPurchase value in the appropriate field.