Jump to content

Help in adding a Promo code to shopping cart


saf_fire
 Share

Recommended Posts

I have build an online shopping store, but would want to give away a promo code or Gift coupon to some of my customers so that they can avail discounts while they check out. Please tell me what coding should I do or what addition should I do for this feature.

  • Like 1
Link to comment
Share on other sites

I've never done this, but the first idea that comes to my mind is to create all the promotions as pages with a field for the code and another for the discount:

-promotions

--promo1(title, code, discount)

--promo2(title, code, discount)

--promo3(title, code, discount)

When someone submits a code, you can look for the discount with:

$discount = $pages->get("parent=/promotions/, code=$code")->discount;

You can generate your dscount code with something like this http://stackoverflow.com/questions/3521621/php-code-for-generating-decent-looking-coupon-codes-mix-of-alphabets-and-number (first result on my google search), and make sure that the code is unique by turning that code into a function, and matching the result  against your other discount codes:

// written on the browser and not tested. might be completely wrong 

function generateCode(){
    $chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $res = "";
    for ($i = 0; $i < 10; $i++) {
        $res .= $chars[mt_rand(0, strlen($chars)-1)];
    }
    foreach (wire('pages')->get("parent=/promotions")->children as $c) {
        if ($c->code == $res) {
            $res = 0;
            break;
        }
    }
    if ($res == 0) generateCode();
    return $res;
}
 
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...