Using coupon code as a promo code to add free item to the cart

Hello,
I have perused the forum and dug through the docs but am not finding a great solution to my problem. I am trying to create a promotion where people can enter a promo code(coupon code), where they will receive a free gift with a purchase over $x. What I have set up is that the cart will notify customer of the status of the coupon with a message below the coupon using code from this snippet to let them know if they have met the criteria. When the go to checkout there is a modal (snippet link) that displays a link to add the free gift only if free item is not in cart and the coupon is applied. However I keep running into catch 22's. Here's what I've tried:

Scenario 1:
Coupon method: Discount by an amount based on the price in this category (49-5.95)
Categories: all
Free Gift Price: 5.95
Result: Customer is given corresponding messages below code. When they continue to checkout they are met with a modal with a link to add gift to cart. When product is added it has all functionally worked.
Issues: 1.)coupon applies discount even when the free gift is not in the cart 2.)Even if the product is added to the cart manually or automatically, customer can remove product and still receive discount

Scenario 2:
Coupon method: Discount by an amount based on the price in this category (49-5.95)
Categories: Free Gift (where it is the only product with that category)
Free Gift Price: 5.95
Result: Coupon message does not work correctly as it is checking for value of items in cart in the specific category and therefore coupon criteria is never met.

Scenario 3:
Coupon method: Discount by an amount based on the price in this category (49-0)
Categories: all
Free gift price: 0.00
Result: Doesn't work as coupon and product must have values greater than 0.

Is there another means to either not allow the coupon to apply if the free gift is not in the cart, or add a gift at 0 cost?

Thanks!
Comments
  • fc_marijafc_marija FoxyCart Team
    Hi @mrlarson -

    Good questions! The coupon functionality is set up to apply to the cart overall, as you noticed. If you restrict the code to certain products as you did with the Category restriction (you'd also get the same effect using product code restrictions), the discount will only apply once that particular product has reached the minimum amount (which in this case is $49) and the other item wouldn't be required.

    I think you can achieve what you're looking for using scenario 1 and this snippet in your template configuration footer:

    {% if context == "cart" or context == "checkout" %}
    <script>
    var free_prod_coupon_code = "freeproduct";

    FC.client.on("ready.done", checkFreeProdCoupon);
    FC.client.on("cart-item-remove.done", checkFreeProdCoupon);
    FC.client.on("cart-item-quantity-update.done", checkFreeProdCoupon);

    FC.client.on("cart-coupon-add", function(params) {
    var has_correct_product = false;
    for (var i = 0; i < FC.json.items.length; i++) {
    if (FC.json.items[i].code == "freeproduct") {
    has_correct_product = true;
    }
    }

    if (params.coupon_code == free_prod_coupon_code && !has_correct_product) {
    alert("Sorry, we can't add that coupon without the required product.");
    return false;
    }
    });

    function checkFreeProdCoupon(next) {
    var coupon_id = -1;
    var has_correct_product = false;

    for (var i = 0; i < FC.json.items.length; i++) {
    if (FC.json.items[i].code == "freeproduct") {
    has_correct_product = true;
    }
    }

    for (var code in FC.json.coupons) {
    if (code == free_prod_coupon_code) {
    coupon_id = FC.json.coupons[code].id;
    }
    }

    if (coupon_id > -1 && !has_correct_product) {
    // Has coupon, but shouldn't
    FC.cart.request({
    'cart': 'remove_coupon',
    'coupon_code_id': coupon_id
    }).done(function () {
    FC.cart.render();
    FC.util.removeError('coupon');
    });
    }
    }
    </script>
    {% endif %}


    Not sure if you'll want to do that alert on coupon add - you can remove it if not. Hope that helps!
Sign In or Register to comment.