View Single Post
  #16  
Old 02-10-2005, 12:48 PM
 
MonkeyClan MonkeyClan is offline
 

Member
  
Join Date: May 2004
Posts: 12
 

Default One Solution

I have managed to get this to work for our store... It requires a lot of modifications and we've already done so many to other parts of our site that it's going to be difficult to explain exactly where the modifications should be made but anyone with a little experience modding X-Cart should be able to figure it out... We haven't fully beta-tested it yet, but so far it seems to be working fine...

Keep in mind that the Free Gifts you add should have a price of 0.00, be available for sale, and be in a category that can't be accessed via normal browsing of the site. This was easy to accomplish with our site design but it may or may not be so simple with yours.


STEP 1
--------
Add a "Free Gifts" menu item (or whatever you want to call it) to the admin menu under "Inventory". We put it just under "Coupons".
In /skin1/provider/menu.tpl, add:
Code:
{if $active_modules.Discount_Coupons ne ""} Coupons {/if} Free Gifts


STEP 2
--------
Create the Free Gifts table in the database. We called it "xcart_freegifts" and it has the following structure:
Code:
CREATE TABLE `xcart_freegifts` ( `id` int(11) NOT NULL auto_increment, `productid` int(11) NOT NULL default '0', `minimum_price` decimal(12,2) NOT NULL default '0.00', `active` tinyint(1) NOT NULL default '0', PRIMARY KEY (`id`) ) TYPE=MyISAM;


STEP 3
--------
Create a bunch of functions in func.php for handling the Free Gifts. Some of the function might be reduntant but I just made what I needed and didn't think too much about it.

Code:
function func_get_free_gifts($onlyActive = false) { $query = "SELECT id, productid, minimum_price, active FROM xcart_freegifts"; if ($onlyActive) { $query .= " WHERE active = 1"; } $query .= " ORDER BY minimum_price"; $freeGifts = func_query($query); if (!is_array($freeGifts)) { $freeGifts = array(); } return $freeGifts; } function func_free_gift_of_price_exists($minimum_price) { $query = "SELECT id FROM xcart_freegifts WHERE minimum_price = " . $minimum_price; $freegift = func_query_first($query); return count($freegift); } function func_free_gift_of_product_exists($productid) { $query = "SELECT id FROM xcart_freegifts WHERE productid = " . $productid; $freegift = func_query_first($query); return count($freegift); } function func_add_free_gift($productid, $minimum_price, $active) { $query = "INSERT INTO xcart_freegifts (productid, minimum_price, active) VALUES(" . $productid . ", " . $minimum_price . ", " . $active . ")"; db_query($query); return mysql_affected_rows(); } function func_update_free_gift_price($id, $minimum_price) { $query = "UPDATE xcart_freegifts SET minimum_price = " . $minimum_price . " WHERE id = " . $id; db_query($query); return mysql_affected_rows(); } function func_activate_free_gift($id) { $query = "UPDATE xcart_freegifts SET active = 1 WHERE id = " . $id; db_query($query); return mysql_affected_rows(); } function func_deactivate_free_gift($id) { $query = "UPDATE xcart_freegifts SET active = 0 WHERE id = " . $id; db_query($query); return mysql_affected_rows(); } function func_delete_free_gift($id) { $query = "DELETE FROM xcart_freegifts WHERE id = " . $id; db_query($query); return mysql_affected_rows(); } function func_product_exists_is_avail($productid) { $query = "SELECT * FROM xcart_products WHERE productid = " . $productid . " AND forsale = 'Y'"; return count(func_query_first($query)); } function func_get_product_name($productid) { $query = "SELECT product FROM xcart_products WHERE productid = " . $productid; $product = func_query_first($query); return $product["product"]; }


STEP 4
--------
Create provider/freegifts.php for handling all the admin modifications to the Free Gifts. Here's the code for that page:
Code:
<? require "../smarty.php"; require "../config.php"; require "./auth.php"; require "../include/security.php"; $actionMsg = ""; if ($mode == "add") { if (isset($_REQUEST["productid"]) && isset($_REQUEST["minimum_price"]) && ($_REQUEST["productid"] != "") && ($_REQUEST["minimum_price"] != "")) { if (func_product_exists_is_avail($_REQUEST["productid"])) { if (!func_free_gift_of_product_exists($_REQUEST["productid"])) { $minimumPrice = floatval($_REQUEST["minimum_price"]); if (!func_free_gift_of_price_exists($minimumPrice)) { if ($minimumPrice > 0) { $active = (isset($_REQUEST["active"]) && ($_REQUEST["active"] == "1")) ? "1" : "0"; func_add_free_gift($_REQUEST["productid"], $minimumPrice, $active); } else { $actionMsg = "The minimum price is invalid."; } } else { $actionMsg = "That minimum price has already been used."; } } else { $actionMsg = "That product has already been used."; } } else { $actionMsg = "Product does not exist or is not available for sale."; } } } else if ($mode == "delete") { if (isset($_REQUEST["id"]) && ($_REQUEST["id"] != "")) { func_delete_free_gift($_REQUEST["id"]); } } else if ($mode == "update") { if (isset($_REQUEST["id"]) && ($_REQUEST["id"] != "")) { if ($_REQUEST["minimum_price_" . $_REQUEST["id"]] != $_REQUEST["old_minimum_price_" . $_REQUEST["id"]]) { if (isset($_REQUEST["minimum_price_" . $_REQUEST["id"]]) && ($_REQUEST["minimum_price_" . $_REQUEST["id"]] != "")) { $minimumPrice = floatval($_REQUEST["minimum_price_" . $_REQUEST["id"]]); if (!func_free_gift_of_price_exists($minimumPrice)) { if ($minimumPrice > 0) { func_update_free_gift_price($_REQUEST["id"], $minimumPrice); } else { $actionMsg = "The minimum price is invalid."; } } else { $actionMsg = "That minimum price has already been used."; } } else { $actionMsg = "The minimum price is invalid."; } } if (isset($_REQUEST["active_" . $_REQUEST["id"]]) && ($_REQUEST["active_" . $_REQUEST["id"]] == "1")) { func_activate_free_gift($_REQUEST["id"]); } else { func_deactivate_free_gift($_REQUEST["id"]); } } } $freegifts = func_get_free_gifts(); $smarty->assign("freegifts",$freegifts); $smarty->assign("freegifts_actionmsg",$actionMsg); $smarty->assign("main","freegifts"); @include "../modules/gold_display.php"; $smarty->display("provider/home.tpl"); ?>


STEP 5
--------
Add the redirect call in skin1/single/home.tpl just below Coupons for consistency.
Code:
{elseif $main eq "coupons"} {include file="modules/Discount_Coupons/coupons.tpl"} {elseif $main eq "freegifts"} {include file="provider/main/freegifts.tpl"}


STEP 6
--------
Create skin1/provider/main/freegifts.tpl. The code is as follows:
Code:
{include file="location.tpl" last_location="Free Gifts"} This page allows you to define free gifts that will be offered to customers if they spend a minimum amount that you specify. The free gifts should have their price set to 0 and be available for sale but should not be accessible via normal browsing of the site. When a customer spends equal to or more than the minimum amount they will be notified of the promotion and will need to click on the link provided to add the item to their cart. If they remove items from their cart such that the cart total drops below the minimum price the item will be removed from their cart. {capture name=dialog} <table border="0"> {if $freegifts_actionmsg ne ""} <tr><td colspan=5 class=AdminTitle>{$freegifts_actionmsg}</td></tr> <tr><td colspan=5></td></tr> {/if} <form action="freegifts.php" method="POST"> <input type=hidden name="mode" value="add"> <input type=hidden name="id" value=""> <tr class=TableHead><TD>Active</TD><td>Product ID</td><td>Minimum Price</td><td></td></tr> {section name=whichFreeGift loop=$freegifts} <input type=hidden name="old_minimum_price_{$freegifts[whichFreeGift].id}" value="{$freegifts[whichFreeGift].minimum_price}"> <tr> <td align=center><input type=checkbox name="active_{$freegifts[whichFreeGift].id}" value=1{if $freegifts[whichFreeGift].active eq 1} checked{/if}></td> <td>{$freegifts[whichFreeGift].productid}</td> <td><input type=text name="minimum_price_{$freegifts[whichFreeGift].id}" size=10 value="{$freegifts[whichFreeGift].minimum_price}"></td> <td> <input type="button" value="Update" onClick="this.form.mode.value='update'; this.form.id.value='{$freegifts[whichFreeGift].id}'; this.form.submit();"> <input type="button" value="Delete" onClick="this.form.mode.value='delete'; this.form.id.value='{$freegifts[whichFreeGift].id}'; this.form.submit();"> </td> </tr> {/section} {if $smarty.section.whichFreeGift.total} <tr><td colspan=5></td></tr> {/if} <tr><td colspan=5 class=AdminTitle>Add a new Free Gift</td></tr> <tr> <td align=center><input type=checkbox name="active" value=1></td> <td><input type=text name=productid size=10 value=""></td> <td><input type=text name=minimum_price size=10 value="0.00"></td> <td><input type="submit" value="Add Free Gift"></td> </tr> </form> </table> {/capture} {include file="dialog.tpl" title="EDIT FREE GIFTS" content=$smarty.capture.dialog extra="width=100%"}


STEP 7
--------
Add the promotion text somewhere at the bottom of skin1/customer/main/cart_totals.tpl. The code below is just a generic version. Modify it to fit your site design.
Code:
{section name=ofg loop=$offerFreeGifts} {if $smarty.section.ofg.index eq 0} Congratulations! You qualify for a free promotion. To claim your free gift, please choose from: {/if} Your subtotal is ${$offerFreeGifts[ofg].minimum_price} or more. To receive a free {$offerFreeGifts[ofg].product}, click here {/section}


STEP 8
--------
Add the following code to customer/cart.php. Somewhere at the top, maybe after "$intershipper_recalc = 'Y'", put:
Code:
# # GET ACTIVE FREE GIFTS # $activeFreeGifts = func_get_free_gifts(true);

After the code that checks if the cart is empty and includes the Discount Coupons module if it's active, below "$smarty->assign('cart', $cart);", and above the Redirect code put the code below. Keep in mind that it's very important that this code be put in the right place otherwise your additions and removals from your cart won't work properly.
Code:
# # UPDATE CART BASED ON FREE GIFTS # $offerFreeGifts = array(); foreach ($activeFreeGifts as $freeGift) { $foundFreeGift = false; if ($freeGift["minimum_price"] > $cart["total_cost"]) { foreach ($cart["products"] as $checkFreeGiftProduct) { if ($checkFreeGiftProduct["productid"] == $freeGift["productid"]) { $foundFreeGift = true; } } if ($foundFreeGift) { // Remove free gift $tempProducts = array(); foreach ($cart["products"] as $checkProductForRemove) { if ($checkProductForRemove["productid"] != $freeGift["productid"]) { $tempProducts[] = $checkProductForRemove; } } $cart["products"] = $tempProducts; func_header_location("cart.php"); } } else { foreach ($cart["products"] as $checkFreeGiftProduct) { if ($checkFreeGiftProduct["productid"] == $freeGift["productid"]) { $foundFreeGift = true; } } if (!$foundFreeGift) { // Offer the free gift $offerFreeGifts[] = array( "minimum_price" => $freeGift["minimum_price"], "productid" => $freeGift["productid"], "product" => func_get_product_name($freeGift["productid"])); } } }

Lastly, send the two arrays to the smarty templating engine:
Code:
$smarty->assign("activeFreeGifts", $activeFreeGifts); $smarty->assign("offerFreeGifts", $offerFreeGifts);


STEP 9
--------
In skin1/customer/main/product.tpl you'll have to change the code that prints the price of the item. Our product.tpl is heavily modified so all I can say is that instead of showing a text input field when the price is 0.00 you should put text that says "FREE!" or something like that. For us, it looks something like this:
Code:
<td>{if $product.price ne 0}<font class=ProductDetailsTitle>{include file="currency.tpl" value=$product.price}</font>{else}<font class=ProductDetailsTitle>FREE!</font>{/if}</td>


STEP 10
----------
In skin1/customer/main/cart.tpl or skin1/customer/main/cart_contents.tpl (depending on your setup) you have to do something along these lines so that the quantity input field doesn't show up if it's a free gift:
Code:
{assign var="isFreeGift" value="0"} {section name=afg loop=$activeFreeGifts} {if $activeFreeGifts[afg].productid eq $products[product].productid} {assign var="isFreeGift" value="1"} {/if} {/section} {if $isFreeGift eq "1"} 1 {else} {if $products[product].distribution}1<input type=hidden{else}<input type=text size=3{/if} name="productindexes[{$smarty.section.product.index}]" value="{$products[product].amount}"> {/if}


That's all of it... If anyone needs any help let me know!

Atul
Reply With Quote