View Single Post
  #1  
Old 10-21-2005, 01:02 PM
  anoack's Avatar 
anoack anoack is offline
 

Senior Member
  
Join Date: Dec 2002
Location: Austin, TX
Posts: 113
 

Default Showing Accessories for Product(s)

What it is & how it works:

Live demo:
http://headsetinnovations.com/cart/product.php?productid=16266&cat=250&page=1

Screenshot:


Description: What it does is include the products listing for a defined category (Accessories) and lists them under a defined product. Product & Accessories associations are stored in the x-cart MySQL database for fast processing.

Installation:

1. Create a table in your x-cart database called 'sub_accessories' by running the following MySQL Query:
Code:
CREATE TABLE `sub_accessories` ( `active` char(1) NOT NULL default '0', `productid` int(11) NOT NULL default '0', `cat` int(11) NOT NULL default '0' ) TYPE=MyISAM;

2. Create a directory under /cart/skin1 called 'subaccess' in that folder create a file called 'subaccessphp.tpl' and put the following code in subaccessphp.tpl:
(Make sure you put in your MySQL databse & login information AND CHANGE 'change-to-your-store.com' to your website URL below...)
Code:
{php} // Gets your database login info from config.php global $sql_host, $sql_user, $sql_db, $sql_password; // Determine if user is in HTTP or HTTPS if ($HTTP_SERVER_VARS[SERVER_PORT] == 80) { $protocol = "HTTP"; } elseif ($HTTP_SERVER_VARS[SERVER_PORT] == 443) { $protocol = "HTTPS"; } // Read global vars global $protocol; global $productid; global $printable; // Connect and query mysql database mysql_connect($sql_host, $sql_user, $sql_password); mysql_select_db($sql_db) || die("Could not connect to SQL db"); $query = sprintf("SELECT active, cat FROM sub_accessories WHERE productid='$productid'"); $result = mysql_query($query); $query2 = sprintf("SELECT product FROM xcart_products WHERE productid='$productid'"); $result2 = mysql_query($query2); // Arrange mysql result and assign to vars while ($row = mysql_fetch_assoc($result)) { $sql_cat = $row['cat']; $sql_active = $row['active']; } while ($row = mysql_fetch_assoc($result2)) { $sql_product = $row['product']; } // Formats product title for URI $url_product = str_replace(" ", "+", $sql_product); // Adds vars to the include url // CHANGE 'change-to-your-store.com' to your website URL!!! $inc_url = sprintf('http://change-to-your-store.com/cart/subaccess.php?cat=%s&tm=%s&accprod=%s', $sql_cat, $protocol, $url_product); // Check to see if there even is a database record if not do nothing and don't print on printable if ($sql_active == 1) { if ($printable == Y) { }else{ include $inc_url; } }else{ } {/php}

2a. Then create a file under the same dir (cart/skin1/subaccess) called 'subaccessdiag.tpl' and put the following code: (This is the template that controls how the accessories are listed)
Code:
{capture name=dialogacc}{section name=product loop=$products}{assign var="discount" value=0} <table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td width="100%"><A href="product.php?productid={$products[product].productid}&cat={$cat}&page={$navigation_page}" class="ProductTitle"> {$products[product].product}<span style="text-decoration: none"></span></font></A></td> <td nowrap valign="top"> <p align="right"><font color="#333333">{$products[product].productcode}</font></td> </tr> <tr> <td width="100%" colspan="2"> <table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td width="2" valign="top"> <p align="left"><A href="product.php?productid={$products[product].productid}&cat={$cat}&page={$navigation_page}">{if $tm eq "HTTPS"}{include file="product_thumbnail_secure.tpl" productid=$products[product].productid product=$products[product].product tmbn_url=$products[product].tmbn_url}{else}{include file="product_thumbnail.tpl" productid=$products[product].productid product=$products[product].product tmbn_url=$products[product].tmbn_url}{/if} [img]{$ImagesDir}/viewdetails.gif[/img]</A><td valign="top"> <table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td width="100%" valign="top"> <p style="margin-left: 2px"><font size="2">{$products[product].descr}</font></td> <td valign="top"> <p style="margin-left: 2px">{if $products[product].taxed_price ne 0} {if $products[product].list_price gt 0 and $products[product].taxed_price lt $products[product].list_price} {math equation="100-(price/lprice)*100" price=$products[product].taxed_price lprice=$products[product].list_price format="%d" assign=discount} {math equation="lprice-price" price=$products[product].taxed_price lprice=$products[product].list_price assign=discountsum} {if $discount gt 0} {$lng.lbl_market_price}: <font color="#333333">{include file="currency.tpl" value=$products[product].list_price}</font></FONT> {/if} {/if} <FONT class="ProductPrice">{$lng.lbl_our_price}: {include file="currency.tpl" value=$products[product].taxed_price}</FONT> <FONT class="MarketPrice">{include file="customer/main/alter_currency_value.tpl" alter_currency_value=$products[product].taxed_price}</FONT>{if $discount gt 0}You Save: {include file="currency.tpl" value=$discountsum}{/if} {if $products[product].taxes} {include file="customer/main/taxed_price.tpl" taxes=$products[product].taxes}{/if} {else} <FONT class="ProductPrice">{$lng.lbl_enter_your_price}</FONT> {/if} {if $active_modules.Feature_Comparison ne '' && $products[product].fclassid > 0} {include file="modules/Feature_Comparison/compare_checkbox.tpl" id=$products[product].productid} {/if} {include file="customer/main/buy_now.tpl" product=$products[product]} </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> [img]{$ImagesDir}/productshr.gif[/img] </p>{/section} {/capture} {include file="dialog.tpl" title="Accessories for $accprod" content=$smarty.capture.dialogacc extra="width=100%"}

2b. Create a file under /cart/skin1 called 'product_thumbnail_secure.tpl' and put the following code:
Code:
{if $config.Appearance.show_thumbnails eq "Y"}[img]{if $tmbn_url}{$tmbn_url|replace:[/img]{/if}
(This was added because the php include function does not do HTTPS, this is the workaround to keep IE from giving you the "Insecure Items" warning.)

3. Create a file under /cart called 'subaccess.php' and put the following code:
Code:
<?php # # $Id: home.php,v 1.1.2.7 2005/01/12 07:41:42 svowl Exp $ # define('OFFERS_DONT_SHOW_NEW',1); require "./auth.php"; require $xcart_dir."/include/categories.php"; if ($active_modules["Manufacturers"]) include $xcart_dir."/modules/Manufacturers/customer_manufacturers.php"; if (empty($products)) include "./featured_products.php"; if (!empty($cat)) include "./products.php"; if($active_modules["Bestsellers"]) include $xcart_dir."/modules/Bestsellers/bestsellers.php"; if (!empty($current_category) and is_array($current_category["category_location"])) { foreach ($current_category["category_location"] as $k=>$v) $location[] = $v; } if (!empty($active_modules["Special_Offers"])) { include $xcart_dir."/modules/Special_Offers/category_offers.php"; } # # Assign Smarty variables and show template # $smarty->assign("main","catalog"); # Assign the current location line $smarty->assign("location", $location); # assign the transfer mode and accessories product name $smarty->assign("tm", $tm); $smarty->assign("accprod", $accprod); func_display("subaccess/subaccessdiag.tpl",$smarty); ?>

4. Then go to cart/skin1/customer/main/product.tpl and at the very bottom add this code:
Code:
{include file="subaccess/subaccessphp.tpl"}

5. Now its time to associate products with their accessories.
each field under the sub_accessories MySQL table has a purpose:
active: 0=Disabled, 1=Enabled (Use this to turn accessory listing on or off)
productid: (Set this to the productid of the product you want it to list under. You can get the productid by looking at the url where it says 'product.php?productid=16266' 16266 would be the product id you would enter.
cat: (Set this to the category where your accessories are. So if you accessories are under Accessories > Product go there and look at the url and get the cat #. E.G. 'home.php?cat=628' 628 would be the cat id you would enter for your accessories.

Example MySQL field setup:
active: 1
productid: 16266
cat: 628
This would list accessories for productid 16266 by including products in cat 628.
You can create multiple records for as many as you want!

Well, I thought I would share this with everyone just in case some one had a use for this. Im not php expert im still learning PHP. Im sure there are some better ways to do some of this but this is how I have it and it works GREAT! Any suggestions/comments are welcome.

Thanks, Austin


Changelog:
[10/30/05] - Removed MySQL login/pass & db info and added the following in subaccessphp.tpl:
Code:
global $sql_host, $sql_user, $sql_db, $sql_password;
[10/31/05] - Added function so accessories won't show up in printable version. Added the following to subaccessphp.tpl
Code:
global $printable; // Check to see if there even is a database record if not do nothing and don't print on printable if ($sql_active == 1) { if ($printable == Y) { }else{ include $inc_url; } }else{ }

I have edited the the code above to reflect this change.
__________________
X-Cart: 4.0.13 [Linux]
Reply With Quote