We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22574
    • 181 Posts
    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!!!

      • 37099
      • 338 Posts
      Thanks, great work smiley

      I’ve just started looking into getting an e-commerce solution and Treasure Chest looks like it fits the bill for what I want.

      And yes creating a page for every item did seem a pain, I like your solution. Only looked at the first bit so far, works for me, just a a couple of points you don’t need to echo all the setPlaceholder calls and the parseChunk should go at the end otherwise you miss the first row returned from the DB

      Cheers
      Mike
        • 22574
        • 181 Posts
        Awesome thanks Mike.

        I wondered why it was putting my products in a random order, and it was missing the first DB entry until I made the page un-chached, but I guess this is a better fix.
        Also if I don’t need to echo all of the setPlaceholder’s then how do I set them?
          • 37099
          • 338 Posts
          Quote from: pagalo at Apr 26, 2010, 10:54 PM

          Awesome thanks Mike.

          I wondered why it was putting my products in a random order, and it was missing the first DB entry until I made the page un-chached, but I guess this is a better fix.
          Also if I don’t need to echo all of the setPlaceholder’s then how do I set them?

          Just remove "echo" from the beginning of the line, calling $modx->setPlaceholder sets the placeholder then calling $modx->parseChunk puts them in place and echoing that returns the results.

          Mike
            • 20413
            • 2,877 Posts
            Thanks for sharing! smiley
              @hawproductions | http://mrhaw.com/

              Infograph: MODX Advanced Install in 7 steps:
              http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

              Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
              http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
              • 22574
              • 181 Posts
              Hey Guys,

              I was wondering how I would go about adding each product click "add to cart" as a separate item?

              The goal is so that the customer can buy say product A in colour red, and then product A in blue, at the moment it just copies over.

              Thanks
                • 22574
                • 181 Posts
                I guess a starting point would be to amend the product name to have the colour on the end.
                But not sure how to even start that???
                  • 37099
                  • 338 Posts
                  Quote from: pagalo at May 01, 2010, 06:47 PM

                  Hey Guys,

                  I was wondering how I would go about adding each product click "add to cart" as a separate item?

                  The goal is so that the customer can buy say product A in colour red, and then product A in blue, at the moment it just copies over.

                  Thanks

                  I’ve done that with my version.
                  I changed the session variable that stored the cart information from the comma delimited list to an array of arrays, so that the items are stored along with their options. You can change the options on the product listing page or in the shopping cart. It was quite a lot of work! I can send you the code if you want.

                  Mike

                    • 22574
                    • 181 Posts
                    Omg! I would love that code.

                    What I might do is combine yours and mine, and then call it TreasureChest 2.0? Ha
                    And then can we upload it here for everyone to use and fix up along the way.
                      • 37099
                      • 338 Posts
                      Quote from: pagalo at May 03, 2010, 01:01 AM

                      Omg! I would love that code.

                      What I might do is combine yours and mine, and then call it TreasureChest 2.0? Ha
                      And then can we upload it here for everyone to use and fix up along the way.

                      If you email me your email address I’ll send the code over.

                      I’ve got some more plans for what I’m working on:
                      1) I’m going to add a description and info field also
                      2) Add a categories choice for each product
                      3) I’m adding the option to add "base product(s)" in the manager that other products can be based on. So for example if your shop was selling screen printed clothing you could have a product: "Design A" that was a particular design that could be printed on anything. Then you could have base products that were t-shirts, hoodies etc. In your design A product you then indicate which base products it can be used on. You could then have Design A on your product page, the user could then choose from a drop down list whether they have a tshirt or vest etc. When they choose the base product the item display changes to the options for that particular base product.
                      4) Need to update to latest jQuery as there are errors in this version.
                      5) Need to test the paypal callback stuff

                      And hopefully if I manage to do all that I’ll make it freely available as an install package.....