length

Description

Returns the length of a list or strings.

 integer length( mixed input )

When passed a list, returns the number of items in that list.
When passed an object, returns the number of keys in the object.
When passed a string, returns the number of characters.
When passed anything else, converts to a string and returns the number of characters.

Example

 {length('string')} = 6 {length([1,2,3])} = 3

Dynamic Coupon Offer

Use Case: Check a user’s profile to see when they last purchased, and if it’s over six months ago, show a coupon for a week.

Zephyr:

{if profile.purchases[length(profile.purchases)-1].time > time("-187 days") && profile.purchases[length(profile.purchases)-1].time < time("-180 days")}
You haven't purchased in over six months! Here's a coupon!
{/if}

Explanation:

The script uses the length() function to find the entire number of purchases a user has ever made. Since arrays start with 0, subtract 1 from this value to isolate the last item in this array since arrays start at 0. For example, if a user has purchased ten items, the length() function would return the number 10, which we then subtract 1 from, giving us profile.purchases[9].time. “profile.purchases” references the “purchases” array on the profile object (i.e. the user’s entire purchase history), and profile.purchases[9].time specifically reference the timestamp of their 10th purchase.


It then uses the time() function to create two UNIX timestamps: One for 180 days ago, and one for 187 days ago. It then uses an if statement to make the evaluation if that timestamp is greater than (“>”) 187 days ago and (“&&”) less than (“<“) 180 days ago, i.e. the user’s last purchase was between 180 and 187 days. If you have this banner in a daily mailing, the user will then have the opportunity to see it for a full week.

Check the Number of Purchases a User Has Ever Made

Use Case: You need to check the amount of purchases a user has ever made. If it’s more than 10, display a “thank you” message at the top of the template.

Zephyr:

{if length(profile.purchases) > 10}
Thank you for being a loyal customer! Take $15 off your next purchase.
{/if}

Explanation: This scripts uses the length() function to check the number of purchases a user has ever made by referencing the “profile” object, specifically, the “purchases” array, which is an array of all user purchases. It is then wrapped in a conditional “if” statement that says to show a discount offer if the length of purchases is greater than 10, i.e. the user has purchased more than ten times.

Split a List of Values Stored as a String

Use Case: You have a value on a user’s profile, for example authors they’re following, but it’s saved as a string. It needs to be converted to an array to parse and match against a feed, so we need to use “split” for that conversion.

Zephyr:

{authorArray = split(my_authors,",")}
{if length(authorArray) > 0}
You're currently following:
{foreach authorArray as c}
{c}<br/>
{/foreach}
Click <a href="http://example.com/subscriptions">here</a> to update your preferences!
{else}
It doesn't look like you're following anyone yet! Click <a href="http://example.com/subscriptions">here</a> to update start!
{/if}

Output:

You’re currently following:

Jessie Jones
Mike Murdock
Carl Lucas

Click here to update your preferences!

Explanation: The script uses the split() function to create an array from a string value, based on a specific delimiter, in this case, a comma. Using split() converts the “my_authors” custom field so that each item because a single item in an array as opposed to one individual string. Next, using the length() function, the script checks to see if there’s more than one item in this array. If there is, a foreach loop displays each author along with a prompt to update their preferences. Otherwise (i.e. the user is not following anyone), the user is shown a prompt to start following users.

Display the Intersection of User-Interested and Trending Content

Use Case: Using collaborative filtering, you wish to find all items that are “trending” in your Content Library, but you only want to display the top three of those items to users based on user interest data from your Content Library. Used in conjunction with the personalize() function.

Zephyr:

{**Find 100 items a user is interested in and 100 items that are popular**}
{interestContent = personalize({"algorithm":"interest","size":100})}
{popularContent = personalize({"algorithm":"popular", "size":100})}

{**Desired number of items**}
{recNum = 10}

{**FInd the intersect of the popular and interested items**}
{personalizedContent = content_intersect(interestContent,popularContent)}

{**Create a backup length if there are less than the number of desired item**}
{backupLength = recNum - length(personalizedContent)}

{**If the length of backup items is greater than 0, find unique content in the interestContent object to backfill the remaining items**}

{if backupLength > 0}

{usedcontent = []}
{personalizedContent = filter(personalizedContent, lambda c: (contains(usedcontent, c.url) ? false : push("usedcontent", c.url) || true))}
{backupContent = filter(interestContent, lambda c: (contains(usedcontent, c.url) ? false : push("usedcontent", c.url) || true))}

{backupContent = personalize({
"algorithm" : "interest",
"size": backupLength,
"content" : backupContent
})}

{personalizedContent = personalizedContent + backupContent}

{/if}

{**Run personalization**}
{personalizedContent = personalize({
"algorithm" : "interest",
"size": 10,
"content" : personalizedContent
})}

In the Code:

<p>Here are hot items we think you'll love</p>
{foreach personalizedContent as c}
{c.title}<br/>
{/foreach}

Output:

Here are hot items with think you’ll love

Item 1

Item 2

Item 3

Explanation:

This script uses the personalize() function to recommend content based on two algorithms: Interest (based on on-site browsing) and what’s popular (pieces of content that have the most purchases or the most pageviews). It starts by creating two content objects based on those algorithms: One called “interestContent”, which finds 100 items a user has displayed personal interest in; and one called “popularContent”, which is the 100 globally most popular items. Ultimately, you want an overlap of ten items to show to each user (i.e., ten items that they are interested in that are also most popular), and that recommendation length is set in the “recNum” assignment. Next, in order to find the overlap between the two, the content_intersect() function is used to find how much content exists between “interestContent” and “popularContent”, which creates a new content object called “personalizedContent”. Next, there’s a check of the length of this object (i.e., exactly how many items existed in both sections). This number is subtracted from the “recNum” value, which creates the “backupLength”. This means if there are only three matching items between the interested and popular content objects, the backupLength is “7”, meaning the script will now find seven additionally items to recommend. 

To check whether or not backup content is necessary, the next part of the script uses an “if” statement to check if the backupLength is greater than 0. If it is, a dedupe script is run, which uses the contains() function to check the title of every item in a data feed against an array called “usedcontent.” If the title of the item exists in the array, the filter() function removes it from the feed (this means that the title already occurred once, and it is a duplicated item). In this example, the filter is performed twice: Once on the “personalizedContent” object, and once on the original “interestContent” object. This second filter is used to create a “backupContent” object, which is the content a user is interested in that’s not in the “popularContent” object. The personalize() function is then run on the “backupContent” object baesd on user interest, and the value of the “backupLength” number (i.e. the number of additional items needed to make it to 10) will determine how many additional items to return. “personalizedContent” is then reassigned to be the other “personalizedContent” object plus the “backupContent”. If there were 10 items or more in the original “personalizedContent” object, then no backup content gets added.

Finally, personalize() is run on “personalizedContent” to get the top ten items for each user, which is then looped through using a “foreach” loop and displayed in the template.

Set a Limit on the Number of Items Shown from a Certain Category

Use Case:

You want to ensure that users aren’t shown content based on the same “author” too many times. Instead, you want to limit it to two items max.

Zephyr:

In the Setup:

{max_per_author = 2}
{display_count = 3}

{* Sorts entire content array in interest order, ensuring that most relevant items will be kept for each user *}
{content = personalize({"algorithm": "interest", "content": content, "size": length(content)})}

{* Filter removes any items in excess of max_per_category *}
{author_counts = {}}
{content = filter_content(content, lambda c: ((contains(keys(author_counts), c.author) && author_counts[c.author] >= max_per_author) ? false : ((contains(keys(author_counts), c.author) ? (author_counts[c.author] = author_counts[c.author] + 1) : (author_counts[c.author] = 1)) || true)))}

{* Slice desired number of items for display. Content already sorted in interest order. *}
{content = slice(content, 0, display_count)}

In the Code:

Picked for You!
{foreach content as c}
<a href={c.url}>{c.title}</a> by {c.author}!
<br/>
{/foreach}

Output:

Picked for You!

Alexander Harris elected to Los Angeles House of Representatives by Willow Rosenberg
Get Your First Look at the New iPhone at the Times Square Apple Store by Willow Rosenberg
Spider-Man: Threat or Menace? by J. Jonah Jameson

Show/Hide Code Explanation

This script starts by setting two local variables: a “max_per_author” variable and a “display_count” variable, which are easily editable on a send-by-send basis. In this instance, the “max_per_author” value is set to 2, and the “display_count” is set to 3. It then uses the personalize() algorithm to find all the content the user is interested in. The length() function takes a count of every piece of content in the feed to tell the personalize() function how many items to personalize. By doing the entire length, the content feed is completely reogranized for each user.

Next, an empty object is created, called “author_counts.” In order to fill the object with data, the filter_content() function filters through the content object, using the keys() function in conjunction with the contains() function to check if any “key” (i.e. an author name) is contained within the author_counts object and if the author_count for that particular key is greater than or equal to the author count. Using the Media feed as an example, it’d check if “Willow Rosenberg” is in the object, and if so, if that value is in there more than twice. If so, the script won’t take any further action, but if not, perform another check: If the author_count object already contains that author, then set the value of that author to add “1” to the current value. For instance, if “Willow Rosenberg” were in there once already, the key/value pairing would be “Willow Rosenberg (key) = 1 (value).” Otherwise, if the author_object did not currently contain an instance of that author, the author name is set as the key, with “1” being set as the value. For instance, “Willow Rosenberg = 1″. Since this script iterates through each piece of content, once it gets to “Willow Rosenberg” again as an author, the key/value pair is updated to “Willow Rosenberg = 2″. If it finds that author a third time, it’s removed from the content object for that user.

The content array is then sliced to pull the top items for each user, using the slice() function. The slice starts at position 0 (i.e. the first item in the array) and ends at the value set in the “display_count” variable (in this instance, 3, meaning that content will be the top three items for each user. Finally, a foreach loops iterates through this content array, displaying the title, URL, and author name for each user’s top three pieces of content.

Show Feed Items that Match Terms in Custom Interest Array (With Fallback Content)

Use Case: You have an array of authors a user is following, and on a data feed, you want to match this user variable against a “sailthru.author” content variable in order to create a personalized bucket of content for users. You also want fallback content if there’s no applicable content to show the user.

Zephyr/Code:

Show/hide sample e-commerce feed used for this example
{
"feed": {
	"name": "Ecommerce Feed",
		"url": "http://feed.sailthru.com/ws/feed?id=ecomm"
},
	"content": [
		{
		"title": "To Kill a Mockingbird",
		"description":"The unforgettable novel of a childhood in a sleepy Southern town and the crisis of conscience that rocked it.",
		"date": 1489437759,
		"image": "http://example.com/fiction/tokillamockingbird-full.jpg",
		"weight": 75,
		"tags": [
			"site-store",
			"genre-mystery-thriller",
			"author-harper-lee",
			"classics",
			"fiction",
			"price-11-20"
				],
		"author": "Harper Lee",
		"price": 1500,
		"inventory": 200,
		"vars": {
			"sailthru_genre":"mysteries-and-thrillers",
			"sailthru_category":"books",
			"membership_price":"12.99"
			},
		"url": "http://example.com/fiction/tokillamockingbird/?utm_medium=site",
		"images": {
			"full": {
				"url": "http://example.com/fiction/tokillamockingbird-full.jpg"
					},
			"thumb": {
				"url": "http://example.com/fiction/tokillamockingbird-thumb.jpg"
					}
				}
	},
	{
		"title": "Salem's Lot",
		"description":"Ben Mears has returned to Jerusalem's Lot -- but things are more sinister than he remembers.",
		"date": 1474547794,
		"image": "http://example.com/books/fiction/salemslot-full.jpg",
		"weight": 30,
		"tags": [
			"site-store",
			"genre-horror",
			"author-stephen-king",
			"vampires",
			"fiction",
			"price-5-10"
				],
		"author": "Stephen King",
		"price": 999,
		"inventory": 1567,
		"vars": {
			"sailthru_genre":"horror-and-suspense",
			"sailthru_category":"books",
			"membership_price":"8.15"
			},
		"url": "http://example.com/fiction/salemslot/",
		"images": {
			"full": {
				"url": "http://example.com/books/fiction/salemslot-full.jpg"
					},
			"thumb": {
				"url": "http://example.com/books//fiction/salemslot-thumb.jpg"
					}
				}
	},
	{
		"title": "Women's Leather Jacket",
		"description":"A classic look that never goes out of style with an exposed metal closure that zips at the cuff.",
		"date": 1474547794,
		"image": "http://example.com/fashion/womens/outerwear/leatherjacket-full.jpg",
		"weight": 55,
			"tags": [
			"site-store",
			"womens-clothing",
			"outerwear",
			"material-leather",
			"color-brown",
			"price-201-300"
				],
		"price": 45000,
		"inventory": 1720,
		"vars": {
			"sailthru_color": "brown",
			"sailthru_material":"leather",
			"membership_price":"325.75"
				},
		"url": "http://example.com/fashion/womens/outerwear/leatherjacket/",
		"images": {
			"full": {
				"url": "http://example.com/fashion/womens/outerwear/leatherjacket-full.jpg"
					},
			"thumb": {
				"url": "http://example.com/fashion/womens/outerwear/leatherjacket-thumb.jpg"
					}
				}
	},
	{
		"title": "Invisible Monsters: A Novel",
		"description":"Love, betrayal, petty larceny, and high fashion fuel this deliciously comic novel from the author of Fight Club.",
		"date": 1474547794,
		"image": "http://example.com/books/fiction/invisiblemonsters-full.jpg",
		"weight": 30,
		"tags": [
			"site-store",
			"genre-mystery-thriller",
			"author-chuck-palahniuk",
			"post-modern",
			"fiction",
			"price-5-10"
				],
		"author": "Chuck Palahniuk",
		"price": 899,
		"inventory": 678,
		"vars": {
			"sailthru_genre":"mysteries-and-thrillers",
			"sailthru_category":"books"
			},
		"url": "http://example.com/fiction/invisiblemonsters/",
		"images": {
			"full": {
				"url": "http://example.com/books/fiction/invisiblemonsters-full.jpg"
					},
			"thumb": {
				"url": "http://example.com/books//fiction/invisiblemonsters-thumb.jpg"
					}
				}
	},
	{
		"title": "Men's Chelsea Boots",
		"description": "Look great with jeans or a suit with these slick, black boots.",
		"date": 1474547794,
		"image": "http://example.com/fashion/mens/shoes/chelsea-boots-full.jpg",
		"weight": 40,
			"tags": [
			"site-store",
			"mens-clothing",
			"shoes",
			"material-suede",
			"color-black",
			"price-101-150"
				],
		"price": 14000,
		"inventory": 15,
		"vars": {
			"sailthru_color": "suede",
			"sailthru_material":"leather",
			"membership_price":"115.50"
				},
		"url": "http://example.com/fashion/mens/shoes/chelsea-boots/",
		"images": {
			"full": {
				"url": "http://example.com/fashion/mens/shoes/chelsea-boots-full.jpg"
					},
			"thumb": {
				"url": "http://example.com/fashion/mens/shoes/chelsea-boots-thumb.jpg"
					}
				}
	},
	{
		"title": "From the Fashion Blog: The Most Comfortable Socks You'll Ever Own",
		"description":"They'll literally change your life. Find out where and how to get them!",
		"date": 1489092159,
		"expire_date": 1520628159,
		"image": "http://example.com/fashion/most-comfortable-socks-full.jpg",
		"weight": 25,
		"tags": [
			"site-store",
			"vertical-fashion",
			"type-footwear",
			"author-richard-wilkins",
			"socks",
			"unisex-clothing"
			],
		"author": "Richard Wilkins",
		"vars": {
			"sailthru_category": "fashion",
			"sailthru_storyid": 87422
				},
		"url": "http://example.com/fashion/most-comfortable-socks/",
		"images": {
			"full": {
				"url": "http://example.com/fashion/most-comfortable-socks-full.jpg"
					},
			"thumb": {
				"url": "http://example.com/fashion/most-comfortable-socks-thumb.jpg"
					}
				}
	}
	]
}

On the User Profile:

zephyr example following array stephen king et al

In the Setup:

{*Creating an assignment called your_authors, which is anyone who matches the value of the author var in the content*}
{yourAuthors = filter_content(content, lambda c: contains(profile.vars.following, c.author))}
{*Personalize the order in which authors appear based on interest tags*}
{if length(yourAuthors) > 0}
{yourAuthors = personalize({
 "content" : yourAuthors,
 "algorithm" : "interest",
 "size" : length(yourAuthors)
})}
{else}
{content = personalize({
 "content" : content,
 "algorithm" : "interest",
 "size" : 5
})}
{/if}

In the Code:

<!--Showing new stories if the 'your_authors' array is greater than zero-->
 {if length(yourAuthors) > 0}
 <p>Top books from your favorite authors!</p>
 {foreach yourAuthors as c}
 <p><a href={c.url}>{c.title}</a> by {c.author}</p>
 {/foreach} 
<!--Checking if the user has the following var but with at least one author-->
 {else if(contains(profile.vars,following)) && length(profile.vars.following) > 0}<p>Nothing new from who you're following
 <a href="http://example.com/preferences/">Click here to follow more!</a></p>
But here are some we think you'd like:
{content = personalize({
 "content" : content,
 "algorithm" : "interest",
 "size" : 5
})}
 {foreach content as c}
 <p><a href={c.url}>{c.title}</a> by {c.author}</p>
 {/foreach}
<!--Remaining users who aren't following anyone-->
 {else}
{content = personalize({
 "content" : content,
 "algorithm" : "interest",
 "size" : 5
})}
 <p>You're not following anyone! <a href="http://example.com/preferences/">Click here to start!</a></p>
And here are some we think you'd like to get you started:
 {foreach content as c}
 <p><a href={c.url}>{c.title}</a> by {c.author}</p>
 {/foreach}
 {/if}

Output:

Top books from your favorite authors!

Salem’s Lot by Stephen King
Invisible Monsters by Chuck Palahniuk

Show/Hide Code Explanation

This script uses the filter_content() function in conjunction with the contains() function to find the overlap of the values of a profile variable called “following” (representing authors a user is “subscribed” to), checking for overlap in the content array for any “author” value on each item. Any overlapped items gets added to the “yourAuthors” array. For instance, the example user profile is following Stephen King, Chuck Palahniuk, and Kurt Vonnegut. The first two appear as values for the “author” parameter on two items in the Media feed and are thus added to this user’s “yourAuthor” array.

Next, a check is done on the yourAuthors array to see if it’s greater than 0  (i.e., the user has authors that they’re following who are in the feed). If so, the yourAuthors array is rearranged by the personalize() function to be in order of most interested item to least interested item. Using the length() function on the yourAuthors array tells the personalize() script how many times it should run (in this instance, twice, as there are only two items in the yourAuthors array, meaning length(yourAuthors) = 2). If the user does not have any items in this array, then the personalize() script is run to find 5 items for the entire feed.

In the Code of the template, an “if” statement checks if the length of “yourAuthors” is greater than 0, If it is, loop through each author and show the appropriate content. The next statement uses an “else if” and a contains() function to see if the user has the following var on their profile (by checking all of their profile vars and seeing if following is “contained” in there). If that evaluates to true and the length of the profile user variable is greater than 0 (i.e. there’s at least one author that the user is following), then we can logically determine that the user is following an author, but that author isn’t in the feed. As a result, the content displayed returns 5 items based on user interest data while displaying a message that there isn’t anything new from authors they are following and to start following more.

The last bucket of users are ones who aren’t following any others at all. For these users, the content displayed returns 5 items based on user interest data while displaying a message encouraging them to start following authors for more personalized recommendations.

Display Content with Vars Matching Users’ Interest/Subscription Variables

Use Case: You have individual categories users can follow, each denoted by their own custom field, and on a data feed, you want to match this user field against a “sailthru.category” content variable in order to create a personalized bucket of content for users. You also want a backup section in case someone isn’t following a certain category.

Zephyr:

On the User Profile:

zephyr example tech media news number 1

In the Setup:

{*Creating an assignment called userContent, which will go through each "category" content var, find if you have that same user var with the value of 1, and add it to a "userContent" array*}{userContent = filter_content(content, lambda c: profile.vars[c.vars.sailthru_category] == 1)}{*Personalize the order in which industries appear based on interest tags, pulling the top 3*}{personalizedContent = personalize({
   "content" : userContent,
   "algorithm" : "interest",
   "size" : 3
})}

In the Code:

{if length(userContent) > 0}
<p>Based on your preferences, here are stories you'll enjoy:</p>
{foreach personalizedContent as c}
  <p>From the {c.vars.sailthru_category} category: <a href={c.url}>{c.title}</a></p>
  {/foreach}
{else}
Want personalized recommendations? Tell us what you like <a href="http://example.com/preferences">here</a> to start following a certain category!</p>
{/if}

Output:

(Based on the above user profile)

Here are stories from your selected categories:

From the news category: Spider-Man: Threat or Menace?
From the tech category: Get Your First Look at the New iPhone at the Times Square Apple Store
From the news category: Alexander Harris elected to Los Angeles House of Representatives

(If nothing matches)

Want personalized recommendations? Tell us what you like here to start following a certain category!

Explanation:

This script uses the filter_content() function to iterate through each user profile variable to see if each user has a variable on their profile that matches the value of a content variable on a data feed. For instance, in the Media feed, the values for each sailthru.category custom variable are: media, tech, news, news, and fashion. On the example user profile, the user has “tech”, “media”, and “news” are custom fields that equal “1” (i.e. they are “subscribed” to those categories). Within the filter_content() script, the value of the profile.var (aka custom field) dynamically populates as it iterates through each item in the feed. For instance, for the first item in the Media feed (“Stephen Kings’s New Book…”), the sailthru.category value is “media”. The script check each user profile to see if “profile.vars.media == 1″. If it does, that piece of content is added to a “userContent” variable, which is an array of items from the feed. If not, the item is skipped. For this particular user, since they’re following each category represented in the feed besides “fashion”, only the last article is skipped.

Next, a local variable called “personalizedContent” is created, which uses the personalize() function to pick the top three stories from the “userContent” object based on individual user interest. Finally, in the Code, an “if” statement checks if there’s more than 0 pieces of content in that userContent object (i.e. there’s an item in the feed that matches a category a user is subscribed to). If so, display the category, title, and URL. If not, show a prompt encouraging users to update their preferences and choose categories to follow.

Filter Out Content with Empty ‘Image’ URL Value

Use Case: You discover that some of your content has an image field where the value is an empty string instead of an image URL. Since Sailthru recognizes the presence of the sailthru.image tag, it won’t be filtered out at the feed level. Thankfully, you can use Zephyr to filter out any content that doesn’t have an actual value for the image.

Zephyr:

In the Setup:

{content = filter(content, lambda c: length(c.image) > 0)}

In the example Media feed, the “Spider-Man: Threat or Menace?” article would be thus excluded as its “sailthru.image” parameter is an empty string.

Explanation: This script uses a lambda, which creates an anonymous function, in conjunction with the length() function to check the “image” field of each item in a content array to see if the length is greater than 1, or in other words, if there’s a value. If there isn’t, the filter() function will remove it from the “content” object. This is beneficial if there’s a chance that you might spider an empty string for the sailthru.image parameter.

Converting Price (with a Decimal) from String to Number

Use Case: You have a field in a data feed you maintain with a price, but price is stored as a string and not a number. You need to do some mathematical operations, so it’s integral that the value is a number. The value has a decimal place. You’ll use map(), int(), strpos(), strrpos(), and substr() to successfully convert this value from a string to a number.

Zephyr:

In the Setup:

{content = map(content, lambda c: c + { "newMembershipPrice": int(substr(c.vars.membership_price,0,strpos(c.vars.membership_price,"."))) + int(substr(c.vars.price,strrpos(c.vars.membership_price,".")+1))/100 })}

In the Code:

{if profile.vars.member == true}
 As a member, here are your savings on each item!
{foreach content as c}
 {if length(c.vars.membership_price) > 0}
 ${number(c.price/100 - c.newMembershipPrice,2)} on {c.title}!
 {/if}
{/foreach}
{/if}

Output:

As a member, here are your savings on each item!

$2.01 on To Kill a Mockingbird!

$1.84 on Salem’s Lot!

$124.25 on Women’s Leather Jacket!

$24.50 on Men’s Chelsea Boots!

Explanation:

This script uses the map() function to add a new parameter to a content object, in this instead a parameter called “newMembershipPrice,” which will be the numerical value of a price point currently being stored as a string. In order to determine the value for this parameter, the substr() function then takes a substring of the “membership_price” custom variable, using the strpos() function to start at the beginning of the string and finding the numerical string position of the decimal point as the endpoint. For instance, using the first item in the eComm feed as an example, “12.99” would become “12”. In order to convert this to a number, the int() function is used on that substring.

Since int() drops everything after a decimal place, the value of the cents is isolated in a similar fashion. Using substr() to isolate the value, strrpos() looks at the the same “membership_price” custom variable, finding the numerical position of the decimal point, and then adding one, isolating the “cents.” Like the first part of this script, int() is then used to convert this value to a number. In order to re-convert this number into to cents, it is the divided by 100, and going off the same example, would result in “.99″. The two numberical substrings are added together, producing “12.99”, now as a number instead of a string.

In order to leverage this new value in the Code, an “if” statement is used to check for a custom field called “member” (this can be anything of your choosing, however). If “member” equals “true”, then display the savings each user will receive as a member. Next, a foreach statement loops through each piece of content, and an “if” statement checks to see if the item has a “membership_price” field associated. If so (i.e., there’s a discounted price), the item’s price is divided by 100 as to convert it to a dollars/cents format and then the newMembershipPrice value is subtracted. The number() function is then used to convert this value to a standardized format, such as “2.01”.

Top