I intended on adding this post to the end of the original thread located here.
But doing so may lead to confusion with the older v3.4 and v4.0 code of that thread.
First of all, giving credit where credit is due. Kudos to MonkeyClan, the original poster of this great Mod and to all those that contributed.
This module is basically the same as MonkeyClan's post but was modified for x-cart v4.1.8
As stated in the original post which was for x-cart v3.4.14 and v4.0.12, this module basically allows the store administrator to define "Free Gifts" that are offered to the customer if they spend a certain amount of money. This module also has the added feature called 'Notification Price' that tells the customer that if they spend $XX more, they'll get a free gift.
STEP 1
In the administrator side, we place a menu item called 'Free Gifts' in the Inventory menu under the 'Coupons' menu item.
Find the following line of code in skin1/provider/menu.tpl:
Code:
<a href="{$catalogs.provider}/coupons.php" class="VertMenuItems">{$lng.lbl_coupons}</a><br />
{/if}
and after that code, add:
Code:
<a href="{$catalogs.provider}/freegifts.php" class="VertMenuItems">{$lng.lbl_free_gifts}</a><br />
STEP 2
WARNING: always make a backup of your database before making any changes to it!
Now, using phpMyAdmin or any other way; create the 'x-cart_freegifts' table to your x-cart database.
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',
`notification_price` decimal(12,2) default NULL,
`active` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
STEP 3
We now create a file by copying the following code, and saving it as xcart/include/func.php
Code:
<?php
#
# $Id: func.php,v 1.0 2007/10/06 svowl Exp $
#
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_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_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_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_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();
}
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_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_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_delete_free_gift($id) {
$query = "DELETE FROM xcart_freegifts WHERE id = " . $id;
db_query($query);
return mysql_affected_rows();
}
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
Now create another file for handling all the administration modifications to the Free Gifts by saving it as xcart/provider/freegifts.php
Code:
<?php
#
# $Id: freegifts.php,v 1.0 2007/10/06 svowl Exp $
#
require "./auth.php";
require $xcart_dir."/include/security.php";
include $xcart_dir."/include/categories.php";
include $xcart_dir."/include/func.php";
$location[] = array(func_get_langvar_by_name("lbl_free_gifts"), "");
$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");
# Assign the current location line
$smarty->assign("location", $location);
@include $xcart_dir."/modules/gold_display.php";
func_display("provider/home.tpl",$smarty);
?>
STEP 5
Now open the file skin1/single/home.tpl and find the following line of code
Code:
{elseif $main eq "coupons"}
{include file="modules/Discount_Coupons/coupons.tpl"}
and add the following code after it:
Code:
{elseif $main eq "freegifts"}
{include file="provider/main/freegifts.tpl"}
contiued on next post...