Hey Guys,
I have been working on a better version of TC for awhile now and I thought I should share it with you guys.
I am not the best programmer around so my hope is for some of you others to add to what I have started.
1. The first thing I did was to get rid of the painful process of creating pages for each product, its time consuming and clients hate it/don’t understand it.
So how I did this was I made a snippet which pulls out the product info plus some customer fields that I added manually to the database like category, summary and info, these last two are used to replace what was normal add on the individual page created for each product, to which I added some nice richtext editors.
<?php
$cartItems = $modx->getFullTableName('treasure_chest');
$cart = $modx->db->select("*", $modx->getFullTableName('treasure_chest') );
$limit = $modx->recordCount($cart);
$query = "SELECT item_name, amount, image, summary, category, option_value_0 FROM $cartItems";
$result = mysql_query($query);
$category = $modx->getTemplateVarOutput(array("Categories"));
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$catz = $row['category'];
if($catz==$category['Categories'])
{
echo $modx->parseChunk('onlineResults', $modx->placeholders, '[+', '+]');
echo $modx->setPlaceholder('placeholdername', $row['item_name']);
echo $modx->setPlaceholder('placeholderAmount', $row['amount']);
echo $modx->setPlaceholder('placeholderImage', $row['image']);
echo $modx->setPlaceholder('placeholderOption', $row['option_value_0']);
echo $modx->setPlaceholder('placeholderSummary', $row['summary']);
}
}
?>
The category part works by pulling out the TV on the page that calls this snippets which is named "Categories", in which I have added a drop down list of all the categories and chosen one, so I still have pages for each category just not sub pages for each product, the snippet checks this and only pulls out those products. But if you only have a few products you can skip that part and get rid of the category but.
I then used a chunk onlineResults to style the product list.
I set up a button within the chunk that opens the more info page.
<a href="/index.php?q=ONLINE-PRODUCT-PAGE.html&fname=[+placeholdername+]">>> MORE INFO</a>
This is a page I created and it calls another snippet within that page:
<?php
$cartItems = $modx->getFullTableName('treasure_chest');
$query = "SELECT item_name, amount, image, option_value_0, info FROM $cartItems";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$name = $_GET["fname"];
if($name==$row['item_name'])
{
echo $modx->parseChunk('onlineProductDetails', $modx->placeholders, '[+', '+]');
echo $modx->setPlaceholder('placeholdername', $row['item_name']);
echo $modx->setPlaceholder('placeholderAmount', $row['amount']);
echo $modx->setPlaceholder('placeholderImage', $row['image']);
echo $modx->setPlaceholder('placeholderOption', $row['option_value_0']);
echo $modx->setPlaceholder('placeholderInfo', $row['info']);
}
}
?>
So all of the products use this one page as their more info page, the snippet saves the product name to fname and puts it into the url and then on the more info page it uses this fname to pull all the info out, does that make sense?
There are some other things I did like add new fields to the config_edit.htm file, and I added a bit of code to the module for the categories but that’s it.
2. the second thing I did was to get the options part working (colours, sizes etc), so far its very messy but I’ll try and explain the process.
I first used the <select class="os0_1" onchange="prepPaypal(this)" name="os0_1">
but then I realised that it copies over other products if they all have the 1 so then I changed each products os0_1 to be another number so os0_2, then os0_3....
Then I realised that if the first product with os0_1 wasn’t in the cart then it still didn’t work.
So I changed all of the ending numbers os0_1 to the product number eg: product id 58 os0_58.
I then changed the treasurechest.class.php on line 245 to $i = $product[’id’]; so it uses the product id instead of 1 and increasing i each time it loops.
Then I found out that the cart doesn’t work unless i starts at 1 so after a lot of trial and error I came up with this, lines 245-295:
$t = 1;
foreach ($cart_items as $item_id => $item_quantity)
{
$product = $this->ProductQuery('id', $item_id);
if (isset($product['inventory']) && $item_quantity > $product['inventory']) $item_quantity = $product['inventory'];
$product['quantity'] = $item_quantity;
$item_tpl = $this->cart_item_template;
$product['shipping'] = $product['shipping'] * $product['quantity'];
$product['handling'] = $product['handling'] * $product['quantity'];
$product['tax'] = $product['tax'] * $product['quantity'];
$cart_total = $cart_total + ($product['amount'] * $product['quantity']);
$i = $product['id'];
/*foreach ($key as $product['id'] => $i)
{*/
foreach ($product as $key => $value)
{
/*echo "<script language=javascript>alert('";
echo "$i";
echo "')</script>";*/
$ph = '[+cart.'.$key.'+]';
if ($key == 'option_value_0')
{
if ($_SESSION['TreasureChestOpt']['os0_'.$i])
{
$sval = $_SESSION['TreasureChestOpt']['os0_'.$i];
$script = '<script type="text/javascript" charset="utf-8">jQuery(document).ready(function(){jQuery(".os0_'.$i.'").val("'.$sval.'");});</script>';
$modx->regClientStartupScript($script);
}
$value = str_replace('[+cart.name+]', 'os0_'.$t.'" class="os0_'.$t.'" onchange="prepPaypal(this)', $value);
$item_tpl = str_replace($ph, $value, $item_tpl);
}
elseif ($key == 'option_value_1')
{
if ($_SESSION['TreasureChestOpt']['os1_'.$i])
{
$sval1 = $_SESSION['TreasureChestOpt']['os1_'.$i];
$script = '<script type="text/javascript" charset="utf-8">jQuery(document).ready(function(){jQuery(".os1_'.$i.'").val("'.$sval1.'");});</script>';
$modx->regClientStartupScript($script);
}
$value = str_replace('[+cart.name+]', 'os1_'.$t.'" class="os1_'.$t.'" onchange="prepPaypal(this)', $value);
$item_tpl = str_replace($ph, $value, $item_tpl);
}
else $item_tpl = str_replace($ph, $value, $item_tpl);
$item_tpl = str_replace('[+cart.alt+]', $alt, $item_tpl);
if ($key == 'id' || $key == 'description' || $key == 'inventory') $paypal .='';
elseif ($key == 'option_name_0') $paypal .= '<input type="hidden" class="on0_'.$t.'" name="on0_'.$t.'" value="'.$value.'" />';
elseif ($key == 'option_value_0') $paypal .= '<input type="hidden" class="os0_'.$t.'" name="os0_'.$t.'" value="'.$sval.'" />';
elseif ($key == 'option_name_1') $paypal .= '<input type="hidden" class="on1_'.$t.'" name="on1_'.$t.'" value="'.$value.'" />';
elseif ($key == 'option_value_1') $paypal .= '<input type="hidden" class="os1_'.$t.'" name="os1_'.$t.'" value="" />';
else $paypal .= '<input type="hidden" name="'.$key.'_'.$t.'" value="'.$value.'" />';
}
So what it does is it still starts at 1 but it also uses the product id to get the option say a colour and re-saves it upon clicking update to 1 or whatever the looping number is up to.
The only problem I had with this was that the products cant have the same id value as the number of products in the shopping cart, eg: if you have 7 products and one of those has the id 7 then it mucks up the colours, so I have to go into the database and re-start all of the ids from a higher number say 30 and then it all worked fine.
I hope that all makes sense and that it can help some people form a more full and cohesive TC!!!