X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   Product options on products.tpl (https://forum.x-cart.com/showthread.php?t=2342)

ucmarty 04-17-2003 01:10 PM

Product options on products.tpl
 
Has anyone or can anyone figure out how to get product options to display while viewing multiple products? :?:

I already have an the add to cart and quantity showing on this page but am totally stumped for getting options to show. :?

Any help would be greatly appreciated!! :!:

B00MER 04-17-2003 02:12 PM

This requires quite a bit of backend PHP modifications for such to exist. If you wish you can submit a request to me via the link in my signature and I can provide you quote for such modifications. ;)

funkydunk 04-26-2003 11:16 AM

Boomer

This is quite an interesting one to solve - just did it myself for a client. 8)

minorgod 04-30-2003 09:09 AM

Almost finished my own version...
 
If anyone wants to clue me into how you chose to write your javascript validation and exceptions code to work on multiple products on the same page, I'd love to hear it. I don't need the code, just the logic. I've resorted to using a preg_replace to parse out the function names and give them numbers. Was this part of your solutions or did you find a more elegant approach?

I've got the code working on my test server (though I still need to add the quantity select menu to the products.tpl) with only one minor problem left to solve (one of my Javascript functions is being incorrectly numbered because I'm using the wrong increment variable).

Post UPdate: I think I've finally conquered this one. It was definitely a brain teaser and I still haven't tested it with the validation exceptions, but now my script displays product options (but not quantity) on the products.tpl page. It ain't pretty and I had to stoop so low as to grab some php array values by numeric index rather than by associative index and do some lame preg_replace-ing, but it seems to work for me. I'll post the results of my hair-pulling session in a bit.

minorgod 04-30-2003 09:52 PM

Solution to product options on products.tpl
 
Well it was harder than it should have been and this may not be the best way, but here's the code to do it in xcart 3.3.1.

You will need to modify the following files:
customer/products.php
skindirectory/customer/main/products.tpl

And you'll need to create the following file:
skindirectory/Modules/Product_Options/customer_options_productspage.tpl

I am pasting the code below with no warranty whatsoever. I have left extensive comments in the customer_options_productspage.tpl where certain functionality remains unfinished because I don't need it yet. It is also there to give other programmers a possible starting point for finishing this script.

Note: among the unimplemented functionality is price modification based on chosen options. In other words, if you have options that modify the price of your product, this code will not work as is. Othewise, you should be okay using this code, but I don't guarantee it.

Here's the code:

customer/products.php
Code:

<?
/*****************************************************************************\
+-----------------------------------------------------------------------------+
| X-Cart                                                                      |
| Copyright (c) 2001-2002 Ruslan R. Fazliev. All rights reserved.            |
+-----------------------------------------------------------------------------+
| The Ruslan R. Fazliev forbids, under any circumstances, the unauthorized  |
| reproduction of software or use of illegally obtained software. Making      |
| illegal copies of software is prohibited. Individuals who violate copyright |
| law and software licensing agreements may be subject to criminal or civil  |
| action by the owner of the copyright.                                      |
|                                                                            |
| 1. It is illegal to copy a software, and install that single program for    |
| simultaneous use on multiple machines.                                      |
|                                                                            |
| 2. Unauthorized copies of software may not be used in any way. This applies |
| even though you yourself may not have made the illegal copy.                |
|                                                                            |
| 3. Purchase of the appropriate number of copies of a software is necessary  |
| for maintaining legal status.                                              |
|                                                                            |
| DISCLAIMER                                                                  |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY Ruslan R. Fazliev ``AS IS'' AND ANY  |
| EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE      |
| DISCLAIMED.  IN NO EVENT SHALL Ruslan R. Fazliev OR ITS          |
| CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,      |
| EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,        |
| PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,    |
| WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR    |
| OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF      |
| ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                                  |
|                                                                            |
| The Initial Developer of the Original Code is Ruslan R. Fazliev.          |
| Portions created by Ruslan R. Fazliev are Copyright (C) 2001-2002          |
| Ruslan R. Fazliev. All Rights Reserved.                                    |
+-----------------------------------------------------------------------------+
\*****************************************************************************/

#
# $Id: products.php,v 1.30 2002/11/19 12:06:10 alfiya Exp $
#
# Navigation code
#

$objects_per_page = $config["General"]["products_per_page"];

$total_nav_pages = ceil($current_category["product_count"]/$config["General"]["products_per_page"])+1;

require "../include/navigation.php";

if($active_modules["Advanced_Statistics"])
    include "../modules/Advanced_Statistics/cat_viewed.php";
       
//if($active_modules["Product_Options"])
//        include "../modules/Product_Options/customer_options_productspage.php";


#
# Get products data for current category and store it into $products array
#
$search_query = "($sql_tbl[products].categoryid='$cat' or $sql_tbl[products].categoryid1='$cat' or $sql_tbl[products].categoryid2='$cat' or $sql_tbl[products].categoryid3='$cat') and $sql_tbl[products].forsale='Y'";

$products = func_search_products($search_query, $user_account['membership'], $first_page,$current_category["product_count"]);
if (count($products) ==0) $products="";

if($active_modules["Subscriptions"]) {
    include "../modules/Subscriptions/subscription.php";
}

//--------Brett's Additions
if($active_modules["DigitalSubscriptions"]) {
    include "../modules/DigitalSubscriptions/digital_subscription.php";
}
//------------end Brett's Additions

// Product Options Mod by Brett Brewer
foreach($products as $key=>$value){
// first we loop through the products and get their options if they have any
        $query="SELECT * from $sql_tbl[product_options],$sql_tbl[product_options_js] WHERE $sql_tbl[product_options].productid='$value[productid]' AND $sql_tbl[product_options_js].productid='$value[productid]'";
        $products[$key]['product_options']=func_query($query);
}
// now we loop through the products again and  parse the options
foreach($products as $key=>$value){
        if(is_array($value['product_options']))
        foreach($value['product_options'] as $k=>$v){
                //we need to parse the options  so that the template can understand them...
                //  this is brute force value grabbing...the index 4 should be changed to a reference to an associative array index,
                //  but I didn't see it when viewing the variables in the modified x-cart debugger window.
                $products[$key]['product_options'][$k]['options']=func_parse_options($v[4]);
        }
       
}

// now that we have all the options and the javascript code we need to parse the names of the
// javascript functions and number them the same way the forms and form elements on the template will be numbered.
// I'm not sure this is the best solution for this, but it was the only one I could think of.
               
for($i=0;$i<count($products);$i++){
       
        for($j=0;$j<count($products[$i]['product_options']);$j++){
                $javascript=$products[$i]['product_options'][$j]['javascript_code'];
                $pattern="/product_option/i";
                $replacement="product_option_".$i."_".$j;
                $new_javascript=preg_replace($pattern,$replacement,$javascript);
                $products[$i]['product_options'][$j]['javascript_code']=$new_javascript;
                //echo $replacement;
        }

}
//  Now, presumably, the data is correctly formatted for the template. The code segments below represent fragments of the rest of the product.php page
//  that we are sort of emulating above. The code below needs to be implemented in some form so that any options that affect the price of each
//  product are taken into account. I haven't been able to figure this out yet.
/*
    if($products)
    foreach($products as $key=>$value) {

                $productid = $value["productid"];
                $amount = $value["amount"];
                $options = $value["product_options"];
                                //echo "
<pre>\$options:";
                                //print_r($options);
                                //echo "</pre>";
                        //        echo "end \$options
";

#
# Product options code
#
                                $query="select * from $sql_tbl[product_options] where productid='$value[productid]' order by orderby";
                $product_option_lines = func_query($query);
                                //echo $query;
                $product_options = array();
                                //echo "
\$product_option_lines: <pre>";
                                //print_r($product_option_lines);
                                //echo "</pre>";
                                //echo "end \$product_option_lines
";
                if($product_option_lines)
                        foreach($product_option_lines as $product_option_line)
                                $product_options[$product_option_line["optclass"]] = func_parse_options($product_option_line["options"]);

                $absolute_surcharge = 0;
                $percent_surcharge = 0;
                $this_product_options = "";
                                //echo "
\$product_options: <pre>";
                                //print_r($product_options);
                                //echo "</pre>";
                                //echo "end \$product_options
";

*/
#
# Calculate option surcharges
#
/*       
                                echo "
\$options: <pre>";
                                print_r($options);
                                echo "</pre>";
                                echo "end \$options
";
                if(is_array($options))
                                foreach($options as $option) {
                        $my_option = $product_options[$option["optclass"]][$option["optionindex"]];
                                                        echo "
\$my_option: <pre>";
                                                        print_r($my_option);
                                                        echo "</pre>";
                                                        echo "end \$my_option
";

                        if(!empty($product_options[$option["optclass"]])) {
                                $this_product_options.="$option[optclass]: $my_option[option]\n";
                                if($my_option["type"]=="absolute")
                                        $absolute_surcharge+=$my_option["surcharge"];
                                elseif($my_option["type"]=="percent")
                                        $percent_surcharge+=$my_option["surcharge"];
                        } else
                                $this_product_options.="$option[optclass]: $option[optionindex]\n";

                }
                $this_product_options = chop($this_product_options);
                                echo "
\$this_product_options:<pre>";
                                print_r($this_product_options);
                                echo "</pre>";
                                echo "end \$this_product_options
";
                                if($products[$key]['product_options'])
                                foreach($products[$key]['product_options'] as $k=>$v){
                                        $products[$key]['product_options'][$k]['options']=$this_product_options;
                                }

                               
#
# /Product options
#
        }*/

//foreach($products as $key=>$value){
//        if(is_array($value['product_options']))
//        foreach($value['product_options'] as $k=>$v){
//                $products[$key]['product_options'][$k]['options']=$product_options;
//        }
//}
//echo "
\$products:
<pre>";
//print_r($products);
//echo "</pre>";

//-----END Product Options Mod by Brett Brewer

$smarty->assign("products",$products);
$smarty->assign("navigation_script","home.php?cat=$cat");
?>


skindirectory/customer/main/products.tpl
Code:

{* $Id: products.tpl,v 1.21.2.1 2003/01/20 12:57:59 svowl Exp $ *}
{section name=product loop=$products}
<form name=orderform_{%product.index%} method=post action="cart.php?mode=add">
  <table border=0 width=100%>
    <tr>
      <td width=90 align=center valign=top> <a href=../product.php?productid={$products[product].productid}&cat={$cat}&page={$navigation_page}>{include
        file="product_thumbnail.tpl" productid=$products[product].productid image_x=70
        product=$products[product].product}

        {$lng.lbl_see_details}</a> </td>
      <td valign=top> {$products[product].product|escape}</font>
        <font size=1>

       

        {$products[product].descr|truncate:300:"...":true}

        </font> <hr size=1 noshade width=230 align=left>
        {if $active_modules.Subscriptions ne "" and $products[product].catalogprice}
        {include file="modules/Subscriptions/subscription_info_inlist.tpl"} {else}
        {if $products[product].price ne 0} {if $products[product].discount ne
        0} <font class=MarketPrice>{$lng.lbl_market_price}: <s> {math equation="price/(1-(discount/100))"
        price=$products[product].price discount=$products[product].discount format="%d.00"
        assign=unformatted}{include file="currency.tpl" value=$unformatted} </s></font>

        {/if} <font class=ProductPrice>{$lng.lbl_our_price}: {include file="currency.tpl"
        value=$products[product].price}</font><font class=MarketPrice>{include
        file="customer/main/alter_currency_value.tpl" alter_currency_value=$products[product].price}</font>{if
        $products[product].discount ne 0}, save {$products[product].discount}%{/if}{if
        $config.General.use_vat eq "Y"}, {$lng.lbl_including_vat} {$products[product].vat}%{/if}
        {else} <font class=ProductPrice>{$lng.lbl_enter_your_price}</font> {/if}
        {/if}
 
 <table border="0" cellspacing="0" cellpadding="5">
          {if $active_modules.Product_Options ne ""}{ include file="modules/Product_Options/customer_options_productspage.tpl"}
          {/if}
          <tr>
            <td>{$lng.lbl_quantity}
                        {if $product.min_amount gt 1}

                                <font class=ProductDetailsTitle>{$lng.txt_need_min_amount}{$product.min_amount}{$lng.lbl_items}</font>
                        {/if}
                        </td>
            <td>
                        {if $config.General.show_in_stock eq "Y" and ($product.avail le 0 or $product.avail lt $product.min_amount)}               
                                {$lng.txt_out_of_stock}
            {else}
                                {if $config.General.show_in_stock ne "Y"}
                                        {assign var="mq" value=$config.General.max_select_quantity}
                                {else}
                                        {math equation="min(maxquantity+minamount, productquantity)+1" assign="mq" maxquantity=$config.General.max_select_quantity minamount=$product.min_amount productquantity=$product.avail}
                                {/if}
                      {if $products[product].distribution eq ""}
                              <select name=amount>
                                        {if $product.min_amount le 1}
                                                {assign var="start_quantity" value=1}
                                        {else}
                                                {assign var="start_quantity" value=$product.min_amount}
                                        {/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>
                      {else}
                                        <font class=ProductDetailsTitle>1</font>
                              {$lng.txt_product_downloadable}<input type=hidden name=amount value=1>
                                  {/if}
                          {/if}
                          </td>
          </tr>
        </table>
        <a href="javascript:{if $products[product].product_options ne ""}if (FormValidation_{%product.index%}()){/if} document.orderform_{%product.index%}.submit()">
        {include file="buttons/add_to_cart.tpl"}</a>
        <input type=hidden name=mode value=add>
        <input type=hidden name=productid value="{$products[product].productid}">
        <input type=hidden name=cat value="{$smarty.get.cat}">
        <input type=hidden name=page value="{$smarty.get.page}">
        <a href="product.php?productid={$products[product].productid}&cat={$cat}&page={$navigation_page}">{include
        file="buttons/more_info.tpl"}</a>
</td>
    </tr>
  </table>
</form>
<hr size="1" noshade>
{/section}


skindirectory/modules/Product_Options/customer_options_productspage.tpl
Code:

{* $Id: customer_options.tpl,v 1.6 2002/10/22 10:27:05 olga Exp $ *}
{section name=product_option loop=$products[product].product_options}

<SCRIPT language=JavaScript1.2>
<!--
function product_option_{%product.index%}_{%product_option.index%}(name_of_option)
{literal}{
{/literal}
for(i=0; i<={php}echo count($product_options) {/php}; i++)
  if (document.orderform_{%product.index%}[i].name.search(name_of_option) != -1)
        return document.orderform_{%product.index%}[i];
return -1;     
{literal}
}
{/literal}
function FormValidation_{%product.index%}()
{literal}{ 
{/literal}
{if $products[product].product_options[product_option].javascript_code}
{$products[product].product_options[product_option].javascript_code}
{else}
return true;
{/if}
{literal}
}
-->
</SCRIPT>
{/literal}
<tr>
<td valign=right align="left">
{$products[product].product_options[product_option].opttext}
</td>
<td valign=left>
{if $products[product].product_options[product_option].options ne ""}
{assign var="options" value=$products[product].product_options[product_option].options}
<select name="product_options[{$products[product].product_options[product_option].optclass}]">
{section name=option loop=$options}
<option value={$smarty.section.option.index}>{$options[option].option}{if $options[option].surcharge ne 0} ({if $options[option].type eq "absolute"}{$config.General.currency_symbol}{else}%{/if}{$options[option].surcharge}){/if}</option>
{/section}
</select>
{else}
<input type=text name="product_options[{$products[product].product_options[product_option].optclass}]" size=32>
{/if}
</td>
</tr>
{/section}
{if $err eq "options" and $products[product].product_options[product_option].exceptions ne ""}
<TR>
<TD colspan=2><FONT class=CustomerMessage>Note: this product cannot be added to the cart with the following options combinations:</FONT></TD>
</TR>
<TR valign=top>
<TD colspan=2>
{section name=oi loop=$products.product_options[product_option].exceptions}
* {$products.product_options[product_option].exceptions[oi].exception}

{/section}
</TD>
</TR>
{/if}


The above code really needs some cleaning up and there's still some code in there for my own custom mods that you may not have. I'll come back later and clean it up if anyone wants me to.

Enjoy.

ucmarty 05-06-2003 01:34 PM

Thanks
 
Thanks minorgod for posting this. It is Greatly Appreciated! :D

I haven't tried it out yet but I will take a look through it right away.

I do need prices to change based on different options but I will see if I can figure that out and post it back here. If you get that figured first I'd love to see it.

Thanks again,

Marty

minorgod 05-06-2003 02:17 PM

Just pay attention to all the code I commented out. It would probably be easier for you to just delete all that extra code and start writing your own price modifier from scratch or based on the original product options code. If you need any explanation of what's happening in my code, just let me know. It's still working great for me, but it's only tested in my version of x-cart which is heavily modified already (so much so that I will probably never be upgrading). Good luck, and please do post your results if you figure it out.

ucmarty 05-07-2003 10:55 AM

Not working properly
 
I tried the code but am getting just the empty text field instead of the options. For some reason the product_options array isn't loading in. I don't quite understand where you got the index array #4 from. Maybe it needs to be different for me since it isn't an associative array. That's all I can think of right now that might not be making it work properly.

If you could explain your code a bit, that might help me. :?

Thanks,
Marty :)

minorgod 05-07-2003 12:05 PM

You should modify your xcartdir/smarty.php file. Change the line that reads
Code:

$smarty->debug_tpl="file:debug_templates.tpl";
to read
Code:

$smarty->debug_tpl="file:debug.tpl";

Now launch your store in webmaster mode...open a new customer window and browse to a product category where the options should be displaying. In the debug window you will see a full list of all assigned PHP variables and their values.

In my PHP code, I used the numeric index [4] because I didn't see an associative index containing my options as a string (which needs to be parsed by the func_parse_options() function). Look for an index in the $products[i][product_options] array that contains your options as such and replace the index [4] with whatever that index turns out to be. This might fix things for you.

The original product options code was designed to work with only a single product and therefore the old code produces a $product_options array which only holds options for the specific product being viewed on the detail page. Since our new script must keep track of options for more than one product, I've simply extended the $products array which is already generated by x-cart, to include product_options with each product. So my code essentially loops through the $products array, and creates a sub array in each product called "product_options".

Oh...and your empty text field showing up, means that the code is nearly working...it just can't find the parsed options. The options code is designed to default to an empty text field if you haven't specified any options (such as blue\n,yellow\n,red\n,etc.) when setting up your options for that product in the admin interface. So it seems you are on the right track. I think if you fix that array index to point to a valid index, it should work for you. Let me know if you have any luck.

ucmarty 05-08-2003 09:58 AM

Got the box to fill
 
I figured out the associative array and now the option box is populating properly. Instead of putting the number 4 in you just have to put 'options' in. For some reason the hyperlink to the detail page isn't working properly for me with your template code. I am going to have to look in to that now and then I will try to figure out the pricing issues.

Thanks for your help. The modified debugging window really helps. I didn't know about that one.

Thanks,
Marty :D

minorgod 05-08-2003 10:04 AM

Hey, thanks for figuring out the array index I needed. Let me know if you figure out the price modification code or if you see any other flaws in my code. I may be generating more dynamic javascript functions than are necessarry to make the options work...haven't yet tried to optimize the code in any way.

ucmarty 05-08-2003 11:03 AM

Price changing
 
Minorgod,

The price modification is working for me with the code that you gave me. I haven't made any changes besides the array change. I am however using a different products.tpl file than you. I don't think this would really affect anything. I can post my code if you want. Just let me know.

:D Marty

minorgod 05-08-2003 11:16 AM

Sweet...I hadn't even tried using the price modification part of the product options, but that actually makes sense now that I think about it, since the original product options code is what still gets run by all the other areas of the site. Since the prices on the products.tpl output aren't javascript-accessible in their current form, it would be a serious pain in the ass to have to modify the price display directly on the products.tpl page based on what options are chosen. Anyway, this makes me happy. Thanks for the info!

ucmarty 05-08-2003 11:40 AM

Thanks for working with me too!

I appreciate the collaboration! :D

minorgod 05-15-2003 11:11 AM

updated products.php page...
 
Below is an update to my modified customer/products.php page for displaying options on the products.tpl page.

Code:

<?
/*****************************************************************************\
+-----------------------------------------------------------------------------+
| X-Cart                                                                      |
| Copyright (c) 2001-2002 Ruslan R. Fazliev. All rights reserved.            |
+-----------------------------------------------------------------------------+
| The Ruslan R. Fazliev forbids, under any circumstances, the unauthorized  |
| reproduction of software or use of illegally obtained software. Making      |
| illegal copies of software is prohibited. Individuals who violate copyright |
| law and software licensing agreements may be subject to criminal or civil  |
| action by the owner of the copyright.                                      |
|                                                                            |
| 1. It is illegal to copy a software, and install that single program for    |
| simultaneous use on multiple machines.                                      |
|                                                                            |
| 2. Unauthorized copies of software may not be used in any way. This applies |
| even though you yourself may not have made the illegal copy.                |
|                                                                            |
| 3. Purchase of the appropriate number of copies of a software is necessary  |
| for maintaining legal status.                                              |
|                                                                            |
| DISCLAIMER                                                                  |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY Ruslan R. Fazliev ``AS IS'' AND ANY  |
| EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE      |
| DISCLAIMED.  IN NO EVENT SHALL Ruslan R. Fazliev OR ITS          |
| CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,      |
| EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,        |
| PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,    |
| WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR    |
| OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF      |
| ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                                  |
|                                                                            |
| The Initial Developer of the Original Code is Ruslan R. Fazliev.          |
| Portions created by Ruslan R. Fazliev are Copyright (C) 2001-2002          |
| Ruslan R. Fazliev. All Rights Reserved.                                    |
+-----------------------------------------------------------------------------+
\*****************************************************************************/

#
# $Id: products.php,v 1.30 2002/11/19 12:06:10 alfiya Exp $
#
# Navigation code
#

$objects_per_page = $config["General"]["products_per_page"];

$total_nav_pages = ceil($current_category["product_count"]/$config["General"]["products_per_page"])+1;

require "../include/navigation.php";

if($active_modules["Advanced_Statistics"])
    include "../modules/Advanced_Statistics/cat_viewed.php";

#
# Get products data for current category and store it into $products array
#
$search_query = "($sql_tbl[products].categoryid='$cat' or $sql_tbl[products].categoryid1='$cat' or $sql_tbl[products].categoryid2='$cat' or $sql_tbl[products].categoryid3='$cat') and $sql_tbl[products].forsale='Y'";

$products = func_search_products($search_query, $user_account['membership'], $first_page,$current_category["product_count"]);
if (count($products) ==0) $products="";

if($active_modules["Subscriptions"]) {
    include "../modules/Subscriptions/subscription.php";
}


// Begin Product Options Mod by Brett Brewer
if(is_array($products))
foreach($products as $key=>$value){
// first we loop through the products and get their options if they have any
        $query="SELECT * from $sql_tbl[product_options],$sql_tbl[product_options_js] WHERE $sql_tbl[product_options].productid='$value[productid]' AND $sql_tbl[product_options_js].productid='$value[productid]'";
        $products[$key]['product_options']=func_query($query);
}
// now we loop through the products again and  parse the options
if(is_array($products))
foreach($products as $key=>$value){
        if(is_array($value['product_options']))
        foreach($value['product_options'] as $k=>$v){
                //we need to parse the options  so that the template can understand them...
                //  this is brute force value grabbing...the index 4 should be changed to a reference to an associative array index,
                //  but I didn't see it when viewing the variables in the modified x-cart debugger window.
                $products[$key]['product_options'][$k]['options']=func_parse_options($v['options']);
        }
       
}

// now that we have all the options and the javascript code we need to parse the names of the
// javascript functions and number them the same way the forms and form elements on the template will be numbered.
// I'm not sure this is the best solution for this, but it was the only one I could think of.
               
for($i=0;$i<count($products);$i++){
       
        for($j=0;$j<count($products[$i]['product_options']);$j++){
                $javascript=$products[$i]['product_options'][$j]['javascript_code'];
                $pattern="/product_option/i";
                $replacement="product_option_".$i."_".$j;
                $new_javascript=preg_replace($pattern,$replacement,$javascript);
                $products[$i]['product_options'][$j]['javascript_code']=$new_javascript;
                //echo $replacement;
        }

}

//-----END Product Options Mod by Brett Brewer

$smarty->assign("products",$products);
$smarty->assign("navigation_script","home.php?cat=$cat");
?>


minorgod 06-18-2003 12:19 PM

Possibly a faster way to do this...
 
After I created the mod above, I noticed that my site was running slower and I suspected it was because the code above was looping through the products and doing a separate query for each product options and then lopping through the products again and doing a separate query for each product javascript options.

I rewrote the script so that it only does two queries, one to get all the options, and one to get all the javascript. I then loop through the products and and options and javascripts to associate the proper options and javascript with each product without doing any more MySQL queries.

In the end, I'm not sure if this way is faster since I'm now forced to loop through all the options for each product to associate them, and then loop through all the javascripts for each product to associate the correct javascript with each product. So ultimately, there's a lot more loops happening and I can't tell if I've made it faster or not. At any rate, here's the code...it still works with the modified templates posted above.

Code:

<?
/*****************************************************************************\
+-----------------------------------------------------------------------------+
| X-Cart                                                                      |
| Copyright (c) 2001-2002 Ruslan R. Fazliev. All rights reserved.            |
+-----------------------------------------------------------------------------+
| The Ruslan R. Fazliev forbids, under any circumstances, the unauthorized  |
| reproduction of software or use of illegally obtained software. Making      |
| illegal copies of software is prohibited. Individuals who violate copyright |
| law and software licensing agreements may be subject to criminal or civil  |
| action by the owner of the copyright.                                      |
|                                                                            |
| 1. It is illegal to copy a software, and install that single program for    |
| simultaneous use on multiple machines.                                      |
|                                                                            |
| 2. Unauthorized copies of software may not be used in any way. This applies |
| even though you yourself may not have made the illegal copy.                |
|                                                                            |
| 3. Purchase of the appropriate number of copies of a software is necessary  |
| for maintaining legal status.                                              |
|                                                                            |
| DISCLAIMER                                                                  |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY Ruslan R. Fazliev ``AS IS'' AND ANY  |
| EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE      |
| DISCLAIMED.  IN NO EVENT SHALL Ruslan R. Fazliev OR ITS          |
| CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,      |
| EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,        |
| PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,    |
| WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR    |
| OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF      |
| ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                                  |
|                                                                            |
| The Initial Developer of the Original Code is Ruslan R. Fazliev.          |
| Portions created by Ruslan R. Fazliev are Copyright (C) 2001-2002          |
| Ruslan R. Fazliev. All Rights Reserved.                                    |
+-----------------------------------------------------------------------------+
\*****************************************************************************/

#
# $Id: products.php,v 1.30 2002/11/19 12:06:10 alfiya Exp $
#
# Navigation code
#

$objects_per_page = $config["General"]["products_per_page"];

$total_nav_pages = ceil($current_category["product_count"]/$config["General"]["products_per_page"])+1;

require "../include/navigation.php";

if($active_modules["Advanced_Statistics"])
    include "../modules/Advanced_Statistics/cat_viewed.php";
       
//if($active_modules["Product_Options"])
//        include "../modules/Product_Options/customer_options_productspage.php";


#
# Get products data for current category and store it into $products array
#
$search_query = "($sql_tbl[products].categoryid='$cat' or $sql_tbl[products].categoryid1='$cat' or $sql_tbl[products].categoryid2='$cat' or $sql_tbl[products].categoryid3='$cat') and $sql_tbl[products].forsale='Y'";

$products = func_search_products($search_query, $user_account['membership'], $first_page,$current_category["product_count"]);
if (count($products) ==0) $products="";

if($active_modules["Subscriptions"]) {
    include "../modules/Subscriptions/subscription.php";
}

//--------Brett's Additions
if($active_modules["DigitalSubscriptions"]) {
    include "../modules/DigitalSubscriptions/digital_subscription.php";
}
//------------end Brett's Additions

// Product Options Mod by Brett Brewer
if(is_array($products)){

        $i=0;
        $productcount=count($products);
       
        // first we build a query to get all the product options with a single query
        $query="SELECT * from $sql_tbl[product_options] WHERE ";
        foreach($products as $key=>$value){
                        $query.="productid='$value[productid]' ";
                        if($i < $productcount-1){
                                $query.="OR ";
                        }
                        $i++;
        }
        //now get the product options
        $product_options=func_query($query);
       
        //now loop through the products and match each one with its corresponding options
        foreach($products as $key=>$product){
                if(is_array($product_options)){
                        foreach($product_options as $options){
                                if($product['productid']==$options['productid']){
                                        $products[$key]['product_options'][]=$options;
                                }
                        }
                }
        }

        // now we build a query to get all the javascript option code for the products
        $i=0;
        $query="SELECT * from $sql_tbl[product_options_js] WHERE ";
        foreach($products as $key=>$value){
                        $query.="productid='$value[productid]' ";
                        if($i < $productcount-1){
                                $query.="OR ";
                        }
                        $i++;
        }

        //now get the js_opt_code
        $js_opt_code=func_query($query);

        //now loop through the products and associate the correct javascript code with each option.
        //Currently, x-cart is set up to use only one Javascript function for each product, so we will
        //actually be creating more javascript code than we need...
        foreach($products as $key=>$value){
                        //Make sure there are options so no errors or warnings are thrown.
                        if(is_array($value['product_options'])){
                                //get the count up front so we don't waste cpu cycles on recounting at each iteration
                                $optionscount=count($value['product_options']);
                                for($i=0;$i<$optionscount;$i++){
                                        //now make sure we have some js code before we try to manipulate it
                                        if(is_array($js_opt_code)){
                                                //get the count up front so we don't waste cpu cycles on recounting at each iteration
                                                $js_optioncode_count=count($js_opt_code);
                                                //now loop through the js code and associate the correct javascript code with the correct options
                                                for($j=0;$j<$js_optioncode_count;$j++){
                                                        if($value['productid']==$js_opt_code[$j]['productid']){
                                                                $products[$key]['product_options'][$i]['javascript_code']=$js_opt_code[$j]['javascript_code'];
                                                        }
                                                }
                                        }
                                        //we need to parse the options  so that the template can understand them...
                                        $products[$key]['product_options'][$i]['options']=func_parse_options($products[$key]['product_options'][$i]['options']);
                                       
                                        // now that we have all the options and the javascript code we need to parse the names
                                        //of the javascript functions and number them the same way the forms and form elements
                                        //on the template will be numbered. I'm not sure this is the best solution for this,
                                        //but it was the only one I could think of.
                                        $javascript=$products[$key]['product_options'][$i]['javascript_code'];
                                        $pattern="/product_option/i";
                                        $replacement="product_option_".$key."_".$i;
                                        $new_javascript=preg_replace($pattern,$replacement,$javascript);
                                        $products[$key]['product_options'][$i]['javascript_code']=$new_javascript;
                                }
                               
                        }
       
        }
}

//  Now, presumably, the data is correctly formatted for the template.
//-----END Product Options Mod by Brett Brewer

$smarty->assign("products",$products);
$smarty->assign("navigation_script","home.php?cat=$cat");
?>


If anyone knows if this is faster than the previous method, let me know please. Thanks...and...uh...you're welcome.

Jon 06-30-2003 09:08 PM

Thanks Brett :)

If you do any more optimization, can you please keep this thread updated.

Appreciated.

Jon 06-30-2003 10:45 PM

How can we add options to the featured products page?

minorgod 07-01-2003 06:39 AM

Can't you just use the above example? I haven't looked at it yet, but I'll need to modify my featured products page at some point. If I do the modifications I'll post them here.

Jon 07-01-2003 01:09 PM

I used the code above, but it didn't work for the featured products.

Jon 07-02-2003 09:39 AM

Any thoughts anybody? This is important for me. Thanks.

minorgod 07-02-2003 01:22 PM

Did you modify the template as well? You need to modify both the PHP source code and the template code to display the options properly.

Jon 07-02-2003 02:04 PM

Yes I modified both, and it works well for the category listings. However it doesnt' work for featured products.

Joe Schwartz 06-23-2005 09:15 PM

SQL error in version 4.0.12
 
I've tried to adapt this code for my 4.0.12 x-cart store, but keep getting this error:

Quote:

SELECT * from WHERE productid='13' Error code : 1064 Description : You have an error in your SQL syntax near 'WHERE productid='13'' at line 1


The error occurs with any product (any productid) regardless of whether or not it has product options. I'm pretty sure the error is in the products.php file in this section:

Code:

  // first we build a query to get all the product options with a single query
  $query="SELECT * from $sql_tbl[product_options] WHERE ";
  foreach($products as $key=>$value){
        $query.="productid='$value[productid]' ";
        if($i < $productcount-1){
            $query.="OR ";
        }
        $i++;
  }


I know this is an old thread, but if anyone has adapted this code for 4.x.x of x-cart, I'd be grateful for any insight.

Many thanks,

Joe

minorgod 06-24-2005 09:36 AM

It looks like you have no value for $sql_tbl[product_options]. Make sure this is defined properly. They may have changed the name of the table in the newer version of x-cart, so just go into your config file or look at your xcart database and make sure you are usnig the correct key name in your $sql_tbl array to specify the product_options table.

Joe Schwartz 06-24-2005 04:30 PM

SQL Error gone, but options still not shown on products.tpl
 
minorgod,

Thanks so much. You were right on the money that the SQL values were not properly defined. I went into config.php and added:

Code:

        "product_options" => "xcart_product_options_ex",
        "product_options_js" => "xcart_product_options_js",


to the SQL Table aliases (there is also "xcart_product_options_lng" which I have not figured out yet how it works with "xcart_product_options_ex").

I noticed that in the SQL table for xcart_product_options_ex there was no productid field and that now it was called optionid. As such I edited your mod in products.php to reflect this:

Code:

  $query="SELECT * from $sql_tbl[product_options] WHERE ";
  foreach($products as $key=>$value){
        $query.="optionid='$value[productid]' ";
        if($i < $productcount-1){
            $query.="OR ";
        }
        $i++;
  }


This removed the SQL errors, however the products.tpl does not display the product options. In fact, the product pages display as if no changes have been made. Obviously I am missing something (not surprising as I feel I am in over my head with this mod). Any suggestions on where to look next would be most appreciated.

Joe

dgreen 10-11-2005 07:06 AM

product options on products page
 
Did you ever get this figured out for 4.0.x?

I am looking for a solution

Joe Schwartz 10-11-2005 07:21 AM

Re: product options on products page
 
Quote:

Originally Posted by dgreen
Did you ever get this figured out for 4.0.x?

I am looking for a solution


I never did find a solution for this. As I needed this for only a couple of products, I ended up making a workaround in which I formatted my product.tpl to resemble my products.tpl and then linked directly via the Categories list to individual products.

Joe


All times are GMT -8. The time now is 06:29 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.