View Single Post
  #17  
Old 04-04-2005, 12:23 PM
 
MonkeyClan MonkeyClan is offline
 

Member
  
Join Date: May 2004
Posts: 12
 

Default Update to my solution...

The original mod I posted was based on a modded Version 3.4.14 installation. I recently got it to work on a Version 4.0.12 installation. I also added a feature called Notification Price that tells the customer that if they spend $XX more they'll get a free gift. Here are the differences and modifications.


STEP 1
--------
This step should be as follows:
Code:
{if $active_modules.Discount_Coupons ne ""} {$lng.lbl_coupons} {/if} Free Gifts


STEP 2
--------
The "xcart_freegifts" table now has the following structure:
Code:
REATE 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', `notification_price` decimal(12,2) default NULL, `active` tinyint(1) NOT NULL default '0', PRIMARY KEY (`id`) ) TYPE=MyISAM;


STEP 3
--------
The following functions were either modified or added:
Code:
function func_get_free_gifts($onlyActive = false) { $query = "SELECT fg.id, fg.productid, p.product, fg.minimum_price, fg.notification_price, fg.active FROM xcart_freegifts fg LEFT JOIN xcart_products p ON fg.productid = p.productid"; 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_add_free_gift($productid, $minimum_price, $notification_price, $active) { $notificationPriceStr = ($notification_price) ? $notification_price : "NULL"; $query = "INSERT INTO xcart_freegifts (productid, minimum_price, notification_price, active) VALUES(" . $productid . ", " . $minimum_price . ", " . $notificationPriceStr . ", " . $active . ")"; db_query($query); return mysql_affected_rows(); } function func_update_free_gift_notification_price($id, $notification_price) { $notificationPriceStr = ($notification_price) ? $notification_price : "NULL"; $query = "UPDATE xcart_freegifts SET notification_price = " . $notificationPriceStr . " WHERE id = " . $id; db_query($query); return mysql_affected_rows(); }


STEP 4
--------
provider/freegifts.php is also different:
Code:
<? require "./auth.php"; require $xcart_dir."/include/security.php"; include $xcart_dir."/include/categories.php"; $actionMsg = ""; if ($mode == "add") { if (isset($fg_productid) && isset($fg_minimum_price) && ($fg_productid != "") && ($fg_minimum_price != "")) { if (func_product_exists_is_avail($fg_productid)) { if (!func_free_gift_of_product_exists($fg_productid)) { $minimumPrice = floatval($fg_minimum_price); $notificationPrice = ($fg_notification_price) ? floatval($fg_notification_price) : ""; if (!func_free_gift_of_price_exists($minimumPrice)) { if ($minimumPrice > 0) { if (($notificationPrice == "") || ($notificationPrice < $minimumPrice)) { $active = (isset($fg_active) && ($fg_active == "1")) ? "1" : "0"; func_add_free_gift($fg_productid, $minimumPrice, $notificationPrice, $active); } else { $actionMsg = "The notification price must be less than the minimum price."; } } 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($fg_id) && ($fg_id != "")) { func_delete_free_gift($fg_id); } } else if ($mode == "update") { if (isset($fg_id) && ($fg_id != "")) { $minimumPriceVar = "fg_minimum_price_" . $fg_id; $oldMinimumPriceVar = "fg_old_minimum_price_" . $fg_id; $notificationPriceVar = "fg_notification_price_" . $fg_id; $activeVar = "fg_active_" . $fg_id; if ($$minimumPriceVar != $$oldMinimumPriceVar) { if (isset($$minimumPriceVar) && ($$minimumPriceVar != "")) { $minimumPrice = floatval($$minimumPriceVar); if (!func_free_gift_of_price_exists($minimumPrice)) { if ($minimumPrice > 0) { func_update_free_gift_minimum_price($fg_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($$notificationPriceVar)) { $notificationPrice = ($$notificationPriceVar) ? floatval($$notificationPriceVar) : ""; func_update_free_gift_notification_price($fg_id, $notificationPrice); } if (isset($$activeVar) && ($$activeVar == "1")) { func_activate_free_gift($fg_id); } else { func_deactivate_free_gift($fg_id); } } } $freegifts = func_get_free_gifts(); $smarty->assign("freegifts",$freegifts); $smarty->assign("freegifts_actionmsg",$actionMsg); $smarty->assign("main","freegifts"); @include $xcart_dir."/modules/gold_display.php"; func_display("provider/home.tpl",$smarty); ?>


STEP 5
--------
No changes to this step...


STEP 6
--------
skin1/provider/main/freegifts.tpl is also different:
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="fg_id" value=""> <tr class=TableHead><TD>Active</TD><td>Product ID</td><td>Minimum Price</td><td>Notification Price</td><td></td></tr> {section name=whichFreeGift loop=$freegifts} <input type=hidden name="fg_old_minimum_price_{$freegifts[whichFreeGift].id}" value="{$freegifts[whichFreeGift].minimum_price}"> <tr> <td align=center><input type=checkbox name="fg_active_{$freegifts[whichFreeGift].id}" value=1{if $freegifts[whichFreeGift].active eq 1} checked{/if}></td> <td>{$freegifts[whichFreeGift].productid} ({$freegifts[whichFreeGift].product})</td> <td><input type=text name="fg_minimum_price_{$freegifts[whichFreeGift].id}" size=10 value="{$freegifts[whichFreeGift].minimum_price}"></td> <td><input type=text name="fg_notification_price_{$freegifts[whichFreeGift].id}" size=10 value="{$freegifts[whichFreeGift].notification_price}"></td> <td> <input type="button" value="Update" onClick="this.form.mode.value='update'; this.form.fg_id.value='{$freegifts[whichFreeGift].id}'; this.form.submit();"> <input type="button" value="Delete" onClick="this.form.mode.value='delete'; this.form.fg_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="fg_active" value=1></td> <td><input type=text name=fg_productid size=10 value=""></td> <td><input type=text name=fg_minimum_price size=10 value="0.00"></td> <td><input type=text name=fg_notification_price size=10 value=""></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
--------
skin1/customer/main/cart_totals.tpl is also different:
Code:
{section name=nfg loop=$notifyFreeGifts} {if $smarty.section.nfg.index eq 0} {/if} Spend ${$notifyFreeGifts[nfg].spend_more_amount} more and get a free {$notifyFreeGifts[nfg].product}! {/section} {section name=ofg loop=$offerFreeGifts} {if $smarty.section.ofg.index eq 0} Congratulations! You qualify for {if $smarty.section.ofg.total > 1}some{else}a{/if} free promotion{if $smarty.section.ofg.total > 1}s{/if}.{if $smarty.section.ofg.total > 1} To claim your free gifts, please choose from:{/if} {/if} Your subtotal is ${$offerFreeGifts[ofg].minimum_price} or more. To receive a free {$offerFreeGifts[ofg].product}, click here {/section}


STEP 8
--------
Instead of modifying customer/cart.php, you have to modify cart.php in the root directory of your store. The top section which gets the active free gifts stays the same, but the other sections are modified as follows:

Code:
# # UPDATE CART BASED ON FREE GIFTS # $offerFreeGifts = array(); $notifyFreeGifts = array(); foreach ($activeFreeGifts as $freeGift) { $foundFreeGift = false; if ($freeGift["minimum_price"] > $cart["total_cost"]) { if (isset($cart["products"]) && is_array($cart["products"])) { 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 if ($freeGift["notification_price"] && ($freeGift["notification_price"] <= $cart["total_cost"])) { // Notify of the free gift $notifyFreeGifts[] = array("notification_price" => $freeGift["notification_price"], "spend_more_amount" => sprintf("%0.2f", ($freeGift["minimum_price"] - $cart["total_cost"])), "productid" => $freeGift["productid"], "product" => func_get_product_name($freeGift["productid"])); } } 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"])); } } }

Code:
$smarty->assign("activeFreeGifts", $activeFreeGifts); $smarty->assign("offerFreeGifts", $offerFreeGifts); $smarty->assign("notifyFreeGifts", $notifyFreeGifts);


STEP 9
--------
I also noticed you have to modify skin1/customer/main/product.tpl so that the customer cannot select more than 1 free item at a time. So in addition to the other changes, do something like the following in the section where it prints the quantity select box:
Code:
{if $product.distribution eq ""} {if $product.taxed_price eq 0} <FONT class="ProductDetailsTitle">1</FONT><INPUT type="hidden" name="amount" value="1"> {else} {if $product.min_amount le 1} {assign var="start_quantity" value=1} {else} {assign var="start_quantity" value=$product.min_amount} {/if} <SCRIPT type="text/javascript" language="JavaScript 1.2"> var min_avail = {$start_quantity|default:1}; var avail = {$mq|default:1}-1; var product_avail = {$product.avail|default:"0"}; </SCRIPT> <SELECT id="product_avail" name="amount"{if $active_modules.Product_Options ne ''} onchange="check_wholesale(this.value);"{/if}> {section name=quantity loop=$mq start=$start_quantity} <OPTION value="{%quantity.index%}" {if $smarty.get.quantity eq %quantity.index%}selected{/if}>{%quantity.index%}</OPTION> {/section} </SELECT> {/if} {else} <FONT class="ProductDetailsTitle">1</FONT><INPUT type="hidden" name="amount" value="1"> {$lng.txt_product_downloadable} {/if}


STEP 10
---------
No changes to this step...


I hope all of that makes sense...

Atul
Reply With Quote