Free Email Customers by SKU page on AbleCommerce forums

by Joe Payne 12. May 2011 20:54

I posted a free page I wrote a while back for my site.  It lets you pull in the emails for customers who bought a particular SKU within a particular timeframe.  Then you can choose any email template to send them.  Works great when you have a recall notice or other advisory you need to send to shoppers who bought something within a certain timeframe.

 

You can find here in the AbleCommerce forums:  http://forums.ablecommerce.com/viewtopic.php?f=47&t=15301

Enjoy !

Tags: , ,

AC7 Articles | General News | New Products

How to add free item to the basket during checkout in AbleCommerce 7

by Joe Payne 26. March 2011 10:11

Introduction

 

This short tutorial describes a common need in AbleCommerce storefronts.  You are going to see line-by-line descriptions of the tasks required and how they are accomplished with my code.  At the end, the full source code is available free for you to use as you wish.  If you want to skip the entrée and go straight to the dessert, scroll to the bottom of this post.

 

The Need

 

You want to offer your shoppers a freebie when their order total exceeds a specific amount.  Currently AbleCommerce has coupons, but coupons don’t offer this particular functionality.  Plus coupons require the shopper to enter something.  In this case, we want the freebie to be completely automatic.

 

How It Works

 

These changes are heavily commented so you can walk through the experience yourself.    All of this work is done in the OnePageCheckout.ascx.cs file.   The code itself is loaded at the very beginning of the Page_Init() function.  Remember the entire code snippet is included at the bottom of this article.

 

The routine starts by making sure we’re not on a postback.  If we’re on a postback, that means all the remaining logic has already been executed in some previous page life cycle;  since the basket cannot be altered while within the checkout page itself, there’s no need to perform the free-item check again.

if (!Page.IsPostBack)
{

 

Next we set some variables we’re going to need for this modification.  The first variable _FreeProductId represents the ProductId value of the product you want to give away for free.  It’s important to remember that you should have this product marked as Hidden else shoppers will find it and try to buy it with every order. 

The second variable is our basket total threshold.  If the basket total (of products only) meets or exceeds this amount, it qualifies for the free item.

Feel free to adjust these values to suit your particular need.

// set the product ID of the free item we want to add
int _FreeProductId = 42;
 
// set the dollar amount threshold of the basket total we want
LSDecimal _MinBasketTotal = 1000;

 

Now we need to scan the basket.  In AC7, this is easily done with the Basket class object contained in the global Token instance.  We have to look through every item in the basket.  If it’s a product-type item, we add it’s dollar total to the grand total.  We also record whether the basket item is our free item i.e. is the free item already in the basket.

// build a basket total and scan the basket for existing free item
int _FreeItemBasketItemIndex = -1;
LSDecimal _BasketTotal = 0;
for (int x=0;x < Token.Instance.User.Basket.Items.Count;x++)
{
    // pull in the basket item object
    BasketItem _BasketItem = Token.Instance.User.Basket.Items[x];
 
    // if it's a product, add it to our total
    if (_BasketItem.OrderItemType == OrderItemType.Product)
        _BasketTotal += _BasketItem.ExtendedPrice;
 
    // if it's the free item, set the ID so we can check later if the free item
    // is already in the basket
    if (_BasketItem.ProductId == _FreeProductId)
        _FreeItemBasketItemIndex = x;
}

 

Now for the real work.  We have to determine if the basket even qualifies for a free item.  If it does, AND the free item doesn’t already exist in the basket, we have some work to do.  First we build a new BasketItem object called _FreeItem with a quantity 1.  Then we add the item to the current shoppers basket and save the basket.

//see if basket total exceeds threshold
if (_BasketTotal >= _MinBasketTotal)
{
    // Ok basket qualifies for the free item
    // see if the free item already exists in basket
    // if the _FreeItemBasketItemId value is still -1, a free item was never found in the
    // current basket
    if (_FreeItemBasketItemIndex == -1)
    {
        // create a basketitem object for this free product
        BasketItem _FreeItem = BasketItemDataSource.CreateForProduct(_FreeProductId, 1);
 
        // add the item to the basket and save it
        Token.Instance.User.Basket.Items.Add(_FreeItem);
        Token.Instance.User.Basket.Save();
    }
}

 

If the basket doe NOT qualify, then we still have a little work to do.  We need to remove any free item already in the basket.  Since we recorded the index of the free item earlier in the code, it becomes a quick and easy check here.

else
{
    // basket doesn't qualify, get rid of the free item if it exists
    if (_FreeItemBasketItemIndex > -1)
    {
        // remove the item and save basket
        Token.Instance.User.Basket.Items[_FreeItemBasketItemIndex].Delete();
        Token.Instance.User.Basket.Save();
    }
}
        }
ND MOD: AbleMods.com

So that’s it.  You’ve tested the basket, added the item if necessary and made sure to remove it if the basket doesn’t qualify.

 

Summary

 

This routine was written for AbleCommerce v7.0.6 but should work in practically any flavor.   To make it work for your site, simply add all the code provided to the beginning of the Page_Init() function in OnePageCheckout.ascx.cs file found in the /ConLib/ folder of your AbleCommerce 7 installation.

 

As always, back up any original files before making changes to them.  Nothing ruins a shoppers experience more than a broken checkout page.

 

Be sure to check out all my modules available at http://www.AbleMods.com/

 

Full Source Code

// BEGIN MOD: AbleMods.com
// 3/26/2011
// This modification will add a specific product (presumably priced at 0 so it's free) if
// the basket total exceeds a specified dollar amount.  Only products are counted towards the 
// basket total.
 
// total up products in the basket
if (!Page.IsPostBack)
{
    // set the product ID of the free item we want to add
    int _FreeProductId = 42;
 
    // set the dollar amount threshold of the basket total we want
    LSDecimal _MinBasketTotal = 1000;
 
    // build a basket total and scan the basket for existing free item
    int _FreeItemBasketItemIndex = -1;
    LSDecimal _BasketTotal = 0;
    for (int x=0;x < Token.Instance.User.Basket.Items.Count;x++)
    {
        // pull in the basket item object
        BasketItem _BasketItem = Token.Instance.User.Basket.Items[x];
 
        // if it's a product, add it to our total
        if (_BasketItem.OrderItemType == OrderItemType.Product)
            _BasketTotal += _BasketItem.ExtendedPrice;
 
        // if it's the free item, set the ID so we can check later if the free item
        // is already in the basket
        if (_BasketItem.ProductId == _FreeProductId)
            _FreeItemBasketItemIndex = x;
    }
 
    //see if basket total exceeds threshold
    if (_BasketTotal >= _MinBasketTotal)
    {
        // Ok basket qualifies for the free item
        // see if the free item already exists in basket
        // if the _FreeItemBasketItemId value is still -1, a free item was never found in the
        // current basket
        if (_FreeItemBasketItemIndex == -1)
        {
            // create a basketitem object for this free product
            BasketItem _FreeItem = BasketItemDataSource.CreateForProduct(_FreeProductId, 1);
 
            // add the item to the basket and save it
            Token.Instance.User.Basket.Items.Add(_FreeItem);
            Token.Instance.User.Basket.Save();
        }
    }
    else
    {
        // basket doesn't qualify, get rid of the free item if it exists
        if (_FreeItemBasketItemIndex > -1)
        {
            // remove the item and save basket
            Token.Instance.User.Basket.Items[_FreeItemBasketItemIndex].Delete();
            Token.Instance.User.Basket.Save();
        }
    }
}
// END MOD: AbleMods.com

Tags: , , ,

AC7 Articles

Month List