Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

QTPro with SPPC and Products Attribute Sort Order, how do install?


eFFeKt

Recommended Posts

hi,

 

i'm tying to merge this 3 contibutions into one. i've finally insatlled sppc and products attributes sort order, but i have no clou how to include it into the pad_base.php file of qtpro. i hope somebody can help me out of this misery. i've spend already 3 days with this code and can't find any solution.

 

 

here ist the code of the first to contibs:

<?php
// BOF SPPC Hide attributes from customer groups
$products_attributes_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . (int)$HTTP_GET_VARS['products_id'] . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . (int)$languages_id . "' and find_in_set('".$customer_group_id."', attributes_hide_from_groups) = 0 ");
$products_attributes = tep_db_fetch_array($products_attributes_query);
if ($products_attributes['total'] > 0) {
?>
	  <table border="0" cellspacing="0" cellpadding="2">
		<tr>
		  <td class="main" colspan="2"><?php echo TEXT_PRODUCT_OPTIONS; ?></td>
		</tr>
<?php
  $products_options_name_query = tep_db_query("select distinct popt.products_options_id, popt.products_options_name, popt.products_options_sort_order from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . (int)$HTTP_GET_VARS['products_id'] . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . (int)$languages_id . "' and find_in_set('".$customer_group_id."', attributes_hide_from_groups) = 0 order by popt.products_options_sort_order");
  while ($products_options_name = tep_db_fetch_array($products_options_name_query)) {
	$products_options_array = array();
	$products_options_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix, pa.products_attributes_id, pa.attribute_sort from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and pa.options_id = '" . (int)$products_options_name['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . (int)$languages_id . "' and find_in_set('".$customer_group_id."', attributes_hide_from_groups) = 0 order by pa.attribute_sort ");
	$list_of_prdcts_attributes_id = '';
	$products_options = array(); // makes sure this array is empty again
	while ($_products_options = tep_db_fetch_array($products_options_query)) {
	$products_options[] = $_products_options;
	$list_of_prdcts_attributes_id .= $_products_options['products_attributes_id'].",";
}

  if (tep_not_null($list_of_prdcts_attributes_id) && $customer_group_id != '0') { 
	 $select_list_of_prdcts_attributes_ids = "(" . substr($list_of_prdcts_attributes_id, 0 , -1) . ")";
 $pag_query = tep_db_query("select products_attributes_id, options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES_GROUPS . " where products_attributes_id IN " . $select_list_of_prdcts_attributes_ids . " AND customers_group_id = '" . $customer_group_id . "'");
 while ($pag_array = tep_db_fetch_array($pag_query)) {
	 $cg_attr_prices[] = $pag_array;
 }

 // substitute options_values_price and prefix for those for the customer group (if available)
 if ($customer_group_id != '0' && tep_not_null($cg_attr_prices)) {
	for ($n = 0; $n < count($products_options); $n++) {
	 for ($i = 0; $i < count($cg_attr_prices); $i++) {
		 if ($cg_attr_prices[$i]['products_attributes_id'] == $products_options[$n]['products_attributes_id']) {
			$products_options[$n]['price_prefix'] = $cg_attr_prices[$i]['price_prefix'];
			$products_options[$n]['options_values_price'] = $cg_attr_prices[$i]['options_values_price'];
		 }
	 } // end for ($i = 0; $i < count($cg_att_prices); $i++)
	}
	} // end if ($customer_group_id != '0' && (tep_not_null($cg_attr_prices))
  } // end if (tep_not_null($list_of_prdcts_attributes_id) && $customer_group_id != '0')

  for ($n = 0; $n < count($products_options); $n++) {
	  $products_options_array[] = array('id' => $products_options[$n]['products_options_values_id'], 'text' => $products_options[$n]['products_options_values_name']);
	  if ($products_options[$n]['options_values_price'] != '0') {
		$products_options_array[sizeof($products_options_array)-1]['text'] .= ' (' . $products_options[$n]['price_prefix'] . $currencies->display_price($products_options[$n]['options_values_price'], tep_get_tax_rate($product_info['products_tax_class_id'])) .') ';
	  }
	}
// EOF SPPC attributes mod

	if (isset($cart->contents[$HTTP_GET_VARS['products_id']]['attributes'][$products_options_name['products_options_id']])) {
	  $selected_attribute = $cart->contents[$HTTP_GET_VARS['products_id']]['attributes'][$products_options_name['products_options_id']];
	} else {
	  $selected_attribute = false;
	}
?>
		<tr>
		  <td class="main"><?php echo $products_options_name['products_options_name'] . ':'; ?></td>
		  <td class="main"><?php echo tep_draw_pull_down_menu('id[' . $products_options_name['products_options_id'] . ']', $products_options_array, $selected_attribute); ?></td>
		</tr>
<?php
  }
?>

 

it has to be in here (pad_base.php):

function _build_attributes_array($build_stocked, $build_nonstocked) {
  global $languages_id;
  global $currencies;
  global $cart;

  if (!($build_stocked | $build_nonstocked)) return null;

  if ($build_stocked && $build_nonstocked) {
	$stocked_where='';
  }
  elseif ($build_stocked) {
	$stocked_where="and popt.products_options_track_stock = '1'";
  }
  elseif ($build_nonstocked) {
	$stocked_where="and popt.products_options_track_stock = '0'";
  }

  $products_options_name_query = tep_db_query("select distinct popt.products_options_id, popt.products_options_name, popt.products_options_track_stock from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . (int)$this->products_id . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . (int)$languages_id . "' " . $stocked_where . " order by popt.products_options_name");
  $attributes=array();

  while ($products_options_name = tep_db_fetch_array($products_options_name_query)) {
	$products_options_array = array();
	$products_options_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$this->products_id . "' and pa.options_id = '" . (int)$products_options_name['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . (int)$languages_id . "'");
	while ($products_options = tep_db_fetch_array($products_options_query)) {

$products_options_array[] = array('id' => $products_options['products_options_values_id'], 'text' => $products_options['products_options_values_name']);


if(PRODINFO_ATTRIBUTE_ACTUAL_PRICE_PULL_DOWN == 'True'){
//Option prices will displayed as a final product price. This can (currently) only be used with a satisfying result if you have only one option per product.

 if ($products_options['price_prefix'] == '-') {// in case price lowers, don't add values, subtract.
$show_price = 0.0 + $this->products_original_price - $products_options['options_values_price']; // force float (in case) using the 0.0;
 } else {
$show_price = 0.0 + $this->products_original_price + $products_options['options_values_price']; // force float (in case) using the 0.0;
 }

 $products_options_array[sizeof($products_options_array)-1]['text'] .= ' '.$currencies->display_price( $show_price, tep_get_tax_rate($this->products_tax_class_id)).' ';

}else{ //Display the option prices as differece prices with +/- prefix as usually
 if ($products_options['options_values_price'] != '0') {
$products_options_array[sizeof($products_options_array)-1]['text'] .= ' (' . $products_options['price_prefix'] . $currencies->display_price($products_options['options_values_price'], tep_get_tax_rate($this->products_tax_class_id)) .')';
 }
}

	}
	if (isset($cart->contents[$this->products_id]['attributes'][$products_options_name['products_options_id']]))
	  $selected = $cart->contents[$this->products_id]['attributes'][$products_options_name['products_options_id']];
	else 
	  $selected = 0;
	$attributes[]=array('oid'=>$products_options_name['products_options_id'],
						'oname'=>$products_options_name['products_options_name'],
						'ovals'=>$products_options_array,
						'default'=>$selected);
  }
  return $attributes;
}

Link to comment
Share on other sites

  • 5 months later...
  • 1 month later...

caio7

 

replace pad_base.php

 

with this code

 

-------------------------------------------------

<?php

/*

QT Pro Version 4.0

 

pad_base.php

 

Contribution extension to:

osCommerce, Open Source E-Commerce Solutions

http://www.oscommerce.com

 

Product Attribute Sorting added by Nicholas Rivera on 01/13/05.

Based off code by Don Mitchinson.

 

Copyright © 2004 Ralph Day

Released under the GNU General Public License

 

Based on prior works released under the GNU General Public License:

QT Pro prior versions

Ralph Day, October 2004

Tom Wojcik aka TomThumb 2004/07/03 based on work by Michael Coffman aka coffman

FREEZEHELL - 08/11/2003 [email protected] Copyright © 2003 IBWO

Joseph Shain, January 2003

osCommerce MS2

Copyright © 2003 osCommerce

 

*******************************************************************************************

 

QT Pro Product Attributes Display Plugin

 

pad_base.php - Base Class

 

Class Name: pad_base

 

This base class, although functional, is not intended to be installed and used

directly. It is extended by other classes to provide different display options

for product attributes on the product information page (product_info.php).

 

 

Methods:

 

pad_base constructor

_SetConfigurationProperties set local properties from DB config constants

draw draw the product attributes

_draw_table_start draw start of the table to enclose the attributes display

_draw_stocked_attributes draw attributes that stock is tracked for

_draw_nonstocked_attributes draw attributes that stock is not tracked for

_draw_table_end draw end of the table to enclose the attributes display

_draw_js_stock_array draw a Javascript array of in stock attribute combinations

_build_attributes_array build an array of the attributes for the product

_build_attributes_combinations build an array of the attribute combinations for the product

 

Properties:

 

products_id the product id for attribute display

products_tax_class_id the products tax class id

show_out_of_stock show out of stock attributes flag

mark_out_of_stock mark out of stock attributes flag

out_of_stock_msgline show out of stock message line flag

no_add_out_of_stock prevent add to cart of out of stock attributes combinations

 

 

*/

class pad_base {

var $products_id;

var $products_tax_class_id;

var $show_out_of_stock;

var $mark_out_of_stock;

var $out_of_stock_msgline;

var $no_add_out_of_stock;

 

 

/*

Method: pad_base

 

Class constructor

 

Parameters:

 

$products_id integer The product id of the product attributes are to be displayed for

 

Returns:

 

nothing

 

*/

function pad_base($products_id=0) {

 

 

$this->products_id = $products_id;

 

if ($this->products_id != 0) {

$tax_class_query = tep_db_query('SELECT products_tax_class_id

FROM ' . TABLE_PRODUCTS . "

WHERE products_id = '" . (int)$products_id . "'");

$tax_class_array = tep_db_fetch_array($tax_class_query);

$this->products_tax_class_id = $tax_class_array['products_tax_class_id'];

}

 

$this->_SetConfigurationProperties('PRODINFO_ATTRIBUTE_');

 

}

 

 

/*

Method: _SetConfigurationProperties

 

Set local configuration properties from osCommerce configuration DB constants

 

Parameters:

 

$prefix sting Prefix for the osCommerce DB constants

 

Returns:

 

nothing

 

*/

function _SetConfigurationProperties($prefix) {

 

$this->show_out_of_stock = constant($prefix . 'SHOW_OUT_OF_STOCK');

$this->mark_out_of_stock = constant($prefix . 'MARK_OUT_OF_STOCK');

$this->out_of_stock_msgline = constant($prefix . 'OUT_OF_STOCK_MSGLINE');

$this->no_add_out_of_stock = constant($prefix . 'NO_ADD_OUT_OF_STOCK');

 

}

 

 

/*

Method: draw

 

Draws the product attributes. This is the only method other than the constructor that is

intended to be called by a user of this class.

 

Attributes that stock is tracked for are grouped first and drawn with one dropdown list per

attribute. All attributes are drawn even if no stock is available for the attribute and no

indication is given that the attribute is out of stock.

 

Attributes that stock is not tracked for are then drawn with one dropdown list per

attribute.

 

Parameters:

 

none

 

Returns:

 

string: HTML for displaying the product attributes

 

*/

function draw() {

 

$out=$this->_draw_table_start();

 

$out.=$this->_draw_stocked_attributes();

 

$out.=$this->_draw_nonstocked_attributes();

 

$out.=$this->_draw_table_end();

 

return $out;

 

}

 

 

/*

Method: _draw_table_start

 

Draws the start of a table to wrap the product attributes display.

Intended for class internal use only.

 

Parameters:

 

none

 

Returns:

 

string: HTML for start of table

 

*/

function _draw_table_start() {

$out =' <table border="0" cellspacing="0" cellpadding="2">';

$out.=' <tr>';

$out.=' <td class="main" colspan="2"><b>' . TEXT_PRODUCT_OPTIONS . '</b></td>';

$out.=' </tr>';

return $out;

}

 

 

/*

Method: _draw_stocked_attributes

 

Draws the product attributes that stock is tracked for.

Intended for class internal use only.

 

Attributes that stock is tracked for are drawn with one dropdown list per attribute.

All attributes are drawn even if no stock is available for the attribute and no

indication is given that the attribute is out of stock.

 

Parameters:

 

none

 

Returns:

 

string: HTML for displaying the product attributes that stock is tracked for

 

*/

function _draw_stocked_attributes() {

$out = '';

 

$attributes = $this->_build_attributes_array(true, false);

if (sizeof($attributes)>0) {

foreach ($attributes as $stocked) {

$out .= '<tr><td align="right" class=main><b>' . $stocked['oname'] . ":</b></td><td class=main>" . tep_draw_pull_down_menu('id['.$stocked['oid'].']',array_values($stocked['ovals']),$stocked['default']) . "</td></tr>\n";

}

}

return $out;

}

 

 

/*

Method: _draw_nonstocked_attributes

 

Draws the product attributes that stock is not tracked for.

Intended for class internal use only.

 

Attributes that stock is not tracked for are drawn with one dropdown list per attribute.

 

Parameters:

 

none

 

Returns:

 

string: HTML for displaying the product attributes that stock is not tracked for

 

*/

function _draw_nonstocked_attributes() {

$out='';

$nonstocked_attributes = $this->_build_attributes_array(false, true);

foreach($nonstocked_attributes as $nonstocked)

{

$out.='<tr><td align="right" class=main><b>'.$nonstocked['oname'].":</b></td><td class=main>".tep_draw_pull_down_menu('id['.$nonstocked['oid'].']',$nonstocked['ovals'],$nonstocked['default'])."</td></tr>\n";

}

return $out;

}

 

 

/*

Method: _draw_table_end

 

Draws the end of a table to wrap the product attributes display.

Intended for class internal use only.

 

Parameters:

 

none

 

Returns:

 

string: HTML for end of table

 

*/

function _draw_table_end() {

return ' </table>';

}

 

 

/*

Method: _build_attributes_array

 

Build an array of the attributes for the product

 

Parameters:

 

$build_stocked boolean Flag indicating if stocked attributes should be built.

$build_nonstocked boolean Flag indicating if non-stocked attribute should be built.

 

Returns:

 

array: Array of attributes for the product of the form:

'oid' => integer: products_options_id

'oname' => string: products_options_name

'ovals' => array: option values for the option id of the form

'id' => integer: products_options_values_id

'text' => string: products_options_values_name

'default' => integer: products_options_values_id that the product id

contains for this option id and should be the

default selection when this attribute is drawn.

Set to zero if the product id did not contain

this option.

 

*/

function _build_attributes_array($build_stocked, $build_nonstocked) {

global $languages_id;

global $currencies;

global $cart;

 

if (!($build_stocked | $build_nonstocked)) return null;

 

if ($build_stocked && $build_nonstocked) {

$stocked_where='';

}

elseif ($build_stocked) {

$stocked_where="and popt.products_options_track_stock = '1'";

}

elseif ($build_nonstocked) {

$stocked_where="and popt.products_options_track_stock = '0'";

}

 

$products_options_name_query = tep_db_query("select distinct popt.products_options_id, popt.products_options_name, popt.products_options_track_stock from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . (int)$this->products_id . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . (int)$languages_id . "' " . $stocked_where . " order by popt.products_options_name");

$attributes=array();

while ($products_options_name = tep_db_fetch_array($products_options_name_query)) {

$products_options_array = array();

// Modified 01-13-05 by Nicholas Rivera. Added product atribute sorting. Original code: $products_options_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$this->products_id . "' and pa.options_id = '" . (int)$products_options_name['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . (int)$languages_id . "'");

$products_options_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix, pa.products_attributes_id from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$this->products_id . "' and pa.options_id = '" . (int)$products_options_name['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . (int)$languages_id . "'" . " order by pa.options_values_price, pa.products_attributes_id");

while ($products_options = tep_db_fetch_array($products_options_query)) {

$products_options_array[] = array('id' => $products_options['products_options_values_id'], 'text' => $products_options['products_options_values_name']);

if ($products_options['options_values_price'] != '0') {

$products_options_array[sizeof($products_options_array)-1]['text'] .= ' (' . $products_options['price_prefix'] . $currencies->display_price($products_options['options_values_price'], tep_get_tax_rate($this->products_tax_class_id)) .')';

}

}

if (isset($cart->contents[$this->products_id]['attributes'][$products_options_name['products_options_id']]))

$selected = $cart->contents[$this->products_id]['attributes'][$products_options_name['products_options_id']];

else

$selected = 0;

$attributes[]=array('oid'=>$products_options_name['products_options_id'],

'oname'=>$products_options_name['products_options_name'],

'ovals'=>$products_options_array,

'default'=>$selected);

}

return $attributes;

}

 

 

/*

Method: _build_attributes_combinations

 

A recursive method for building an array enumerating the attribute combinations for the product

 

Parameters:

 

$attributes array An array of the attributes that combinations will be built for.

Format is as returned by _build_attributes_array.

$showoos boolean Flag indicating if non-stocked attributes should be built.

$markoos string 'Left' if out of stock indication is to be appended in front of the

attribute combination text. 'Right' if out of stock indication is

to be appended at the end of the attribute combination text.

$combinations array Array of the attribute combinations is returned in this parameter.

Should be set to an empty array before an external call to this method.

'comb' => array: array of a single attribute combination

options_id => options_value_id

'id' => string: options/values string for this

combination in the form for the

key of the products_stock table

opt_id-val_id,opt_id-val_id,...

'text' => string: Text for this combination. Values text

is as built by _build_attributes_array

and contains the add/subtract price for

the option value if applicable. Form is:

values_text, values_text

$selected_combination integer Index into the $combinations array of the combination that should

be the default selection when the combination is drawn is returned in

this parameter. Determined from product id. Should be set to zero

before an external call to this method.

 

Parameters for internal recursion use only:

 

$oidindex integer Index into the $attributes array of the option to operate on.

$comb array Array containing option id/values of combination built so far

products_options_id => products_options_value_id

$id string Contains string of options/values built so far

$text string Text for the options values constructed so far.

$isselected boolean Flag indicating if so far all option values in this combination

were indicated to be defaults in the product id.

 

 

Returns:

 

see $combinations and $selected_combination parameters above

no actual function return value.

 

*/

function _build_attributes_combinations($attributes, $showoos, $markoos, &$combinations, &$selected_combination, $oidindex=0, $comb=array(), $id="", $text='', $isselected=true) {

global $cart;

 

foreach ($attributes[$oidindex]['ovals'] as $attrib) {

$newcomb = $comb;

$newcomb[$attributes[$oidindex]['oid']] = $attrib['id'];

$newid=$id.','.$attributes[$oidindex]['oid'].'-'.$attrib['id'];

$newtext = $text.", ".$attrib['text'];

if (isset($cart->contents[$this->products_id]['attributes'][$attributes[$oidindex]['oid']]))

$newisselected = ($cart->contents[$this->products_id]['attributes'][$attributes[$oidindex]['oid']] == $attrib['id']) ? $isselected : false;

else

$newisselected = false;

if (isset($attributes[$oidindex+1])) {

$this->_build_attributes_combinations($attributes, $showoos, $markoos, $combinations, $selected_combination, $oidindex+1, $newcomb, $newid, $newtext, $newisselected);

}

else {

$is_out_of_stock=tep_check_stock(tep_get_prid($this->products_id),1,$newcomb);

if (!$is_out_of_stock | ($showoos == true)) {

switch ($markoos) {

case 'Left': $newtext=($is_out_of_stock ? TEXT_OUT_OF_STOCK.' - ' : '').substr($newtext,2);

break;

case 'Right': $newtext=substr($newtext,2).($is_out_of_stock ? ' - '.TEXT_OUT_OF_STOCK : '');

break;

default: $newtext=substr($newtext,2);

break;

}

$combinations[] = array('comb'=>$newcomb, 'id'=>substr($newid,1), 'text'=>$newtext);

if ($newisselected) $selected_combination = sizeof($combinations)-1;

}

}

}

}

 

 

/*

Method: _draw_js_stock_array

 

Draw a Javascript array containing the given attribute combinations.

Generally used to draw array of in-stock combinations for Javascript out of stock

validation and messaging.

 

Parameters:

 

$combinations array Array of combinations to build the Javascript array for.

Array must be of the form returned by _build_attributes_combinations

Usually this array only contains in-stock combinations.

 

Returns:

 

string: Javacript array definition. Excludes the "var xxx=" and terminating ";". Form is:

{optval1:{optval2:{optval3:1,optval3:1}, optval2:{optval3:1}}, optval1:{optval2:{optval3:1}}}

For example if there are 3 options and the instock value combinations are:

opt1 opt2 opt3

1 5 4

1 5 8

1 10 4

3 5 8

The string returned would be

{1:{5:{4:1,8:1}, 10:{4:1}}, 3:{5:{8:1}}}

 

*/

function _draw_js_stock_array($combinations) {

$out='';

foreach ($combinations[0]['comb'] as $oid=>$ovid) {

$out.='{'.$ovid.':';

$opts[]=$oid;

}

$out.='1';

 

for ($combindex=1; $combindex<sizeof($combinations); $combindex++) {

$comb=$combinations[$combindex]['comb'];

for ($i=0; $i<sizeof($opts)-1; $i++) {

if ($comb[$opts[$i]]!=$combinations[$combindex-1]['comb'][$opts[$i]]) break;

}

$out.=str_repeat('}',sizeof($opts)-1-$i).',';

if ($i<sizeof($opts)-1) {

for ($j=$i; $j<sizeof($opts)-1; $j++)

$out.=$comb[$opts[$j]].':{';

}

$out.=$comb[$opts[sizeof($opts)-1]].':1';

}

$out.=str_repeat('}',sizeof($opts));

 

return $out;

}

 

}

?>

Link to comment
Share on other sites

  • 4 months later...

caio7

 

replace pad_base.php

 

with this code

 

-------------------------------------------------

<?php

/*

QT Pro Version 4.0

 

pad_base.php

 

Contribution extension to:

osCommerce, Open Source E-Commerce Solutions

http://www.oscommerce.com

 

Product Attribute Sorting added by Nicholas Rivera on 01/13/05.

Based off code by Don Mitchinson.

 

Copyright © 2004 Ralph Day

Released under the GNU General Public License

 

Based on prior works released under the GNU General Public License:

QT Pro prior versions

Ralph Day, October 2004

Tom Wojcik aka TomThumb 2004/07/03 based on work by Michael Coffman aka coffman

FREEZEHELL - 08/11/2003 [email protected] Copyright © 2003 IBWO

Joseph Shain, January 2003

osCommerce MS2

Copyright © 2003 osCommerce

 

*******************************************************************************************

 

QT Pro Product Attributes Display Plugin

 

pad_base.php - Base Class

 

Class Name: pad_base

 

This base class, although functional, is not intended to be installed and used

directly. It is extended by other classes to provide different display options

for product attributes on the product information page (product_info.php).

 

 

Methods:

 

pad_base constructor

_SetConfigurationProperties set local properties from DB config constants

draw draw the product attributes

_draw_table_start draw start of the table to enclose the attributes display

_draw_stocked_attributes draw attributes that stock is tracked for

_draw_nonstocked_attributes draw attributes that stock is not tracked for

_draw_table_end draw end of the table to enclose the attributes display

_draw_js_stock_array draw a Javascript array of in stock attribute combinations

_build_attributes_array build an array of the attributes for the product

_build_attributes_combinations build an array of the attribute combinations for the product

 

Properties:

 

products_id the product id for attribute display

products_tax_class_id the products tax class id

show_out_of_stock show out of stock attributes flag

mark_out_of_stock mark out of stock attributes flag

out_of_stock_msgline show out of stock message line flag

no_add_out_of_stock prevent add to cart of out of stock attributes combinations

 

 

*/

class pad_base {

var $products_id;

var $products_tax_class_id;

var $show_out_of_stock;

var $mark_out_of_stock;

var $out_of_stock_msgline;

var $no_add_out_of_stock;

 

 

/*

Method: pad_base

 

Class constructor

 

Parameters:

 

$products_id integer The product id of the product attributes are to be displayed for

 

Returns:

 

nothing

 

*/

function pad_base($products_id=0) {

 

 

$this->products_id = $products_id;

 

if ($this->products_id != 0) {

$tax_class_query = tep_db_query('SELECT products_tax_class_id

FROM ' . TABLE_PRODUCTS . "

WHERE products_id = '" . (int)$products_id . "'");

$tax_class_array = tep_db_fetch_array($tax_class_query);

$this->products_tax_class_id = $tax_class_array['products_tax_class_id'];

}

 

$this->_SetConfigurationProperties('PRODINFO_ATTRIBUTE_');

 

}

 

 

/*

Method: _SetConfigurationProperties

 

Set local configuration properties from osCommerce configuration DB constants

 

Parameters:

 

$prefix sting Prefix for the osCommerce DB constants

 

Returns:

 

nothing

 

*/

function _SetConfigurationProperties($prefix) {

 

$this->show_out_of_stock = constant($prefix . 'SHOW_OUT_OF_STOCK');

$this->mark_out_of_stock = constant($prefix . 'MARK_OUT_OF_STOCK');

$this->out_of_stock_msgline = constant($prefix . 'OUT_OF_STOCK_MSGLINE');

$this->no_add_out_of_stock = constant($prefix . 'NO_ADD_OUT_OF_STOCK');

 

}

 

 

/*

Method: draw

 

Draws the product attributes. This is the only method other than the constructor that is

intended to be called by a user of this class.

 

Attributes that stock is tracked for are grouped first and drawn with one dropdown list per

attribute. All attributes are drawn even if no stock is available for the attribute and no

indication is given that the attribute is out of stock.

 

Attributes that stock is not tracked for are then drawn with one dropdown list per

attribute.

 

Parameters:

 

none

 

Returns:

 

string: HTML for displaying the product attributes

 

*/

function draw() {

 

$out=$this->_draw_table_start();

 

$out.=$this->_draw_stocked_attributes();

 

$out.=$this->_draw_nonstocked_attributes();

 

$out.=$this->_draw_table_end();

 

return $out;

 

}

 

 

/*

Method: _draw_table_start

 

Draws the start of a table to wrap the product attributes display.

Intended for class internal use only.

 

Parameters:

 

none

 

Returns:

 

string: HTML for start of table

 

*/

function _draw_table_start() {

$out =' <table border="0" cellspacing="0" cellpadding="2">';

$out.=' <tr>';

$out.=' <td class="main" colspan="2"><b>' . TEXT_PRODUCT_OPTIONS . '</b></td>';

$out.=' </tr>';

return $out;

}

 

 

/*

Method: _draw_stocked_attributes

 

Draws the product attributes that stock is tracked for.

Intended for class internal use only.

 

Attributes that stock is tracked for are drawn with one dropdown list per attribute.

All attributes are drawn even if no stock is available for the attribute and no

indication is given that the attribute is out of stock.

 

Parameters:

 

none

 

Returns:

 

string: HTML for displaying the product attributes that stock is tracked for

 

*/

function _draw_stocked_attributes() {

$out = '';

 

$attributes = $this->_build_attributes_array(true, false);

if (sizeof($attributes)>0) {

foreach ($attributes as $stocked) {

$out .= '<tr><td align="right" class=main><b>' . $stocked['oname'] . ":</b></td><td class=main>" . tep_draw_pull_down_menu('id['.$stocked['oid'].']',array_values($stocked['ovals']),$stocked['default']) . "</td></tr>\n";

}

}

return $out;

}

 

 

/*

Method: _draw_nonstocked_attributes

 

Draws the product attributes that stock is not tracked for.

Intended for class internal use only.

 

Attributes that stock is not tracked for are drawn with one dropdown list per attribute.

 

Parameters:

 

none

 

Returns:

 

string: HTML for displaying the product attributes that stock is not tracked for

 

*/

function _draw_nonstocked_attributes() {

$out='';

$nonstocked_attributes = $this->_build_attributes_array(false, true);

foreach($nonstocked_attributes as $nonstocked)

{

$out.='<tr><td align="right" class=main><b>'.$nonstocked['oname'].":</b></td><td class=main>".tep_draw_pull_down_menu('id['.$nonstocked['oid'].']',$nonstocked['ovals'],$nonstocked['default'])."</td></tr>\n";

}

return $out;

}

 

 

/*

Method: _draw_table_end

 

Draws the end of a table to wrap the product attributes display.

Intended for class internal use only.

 

Parameters:

 

none

 

Returns:

 

string: HTML for end of table

 

*/

function _draw_table_end() {

return ' </table>';

}

 

 

/*

Method: _build_attributes_array

 

Build an array of the attributes for the product

 

Parameters:

 

$build_stocked boolean Flag indicating if stocked attributes should be built.

$build_nonstocked boolean Flag indicating if non-stocked attribute should be built.

 

Returns:

 

array: Array of attributes for the product of the form:

'oid' => integer: products_options_id

'oname' => string: products_options_name

'ovals' => array: option values for the option id of the form

'id' => integer: products_options_values_id

'text' => string: products_options_values_name

'default' => integer: products_options_values_id that the product id

contains for this option id and should be the

default selection when this attribute is drawn.

Set to zero if the product id did not contain

this option.

 

*/

function _build_attributes_array($build_stocked, $build_nonstocked) {

global $languages_id;

global $currencies;

global $cart;

 

if (!($build_stocked | $build_nonstocked)) return null;

 

if ($build_stocked && $build_nonstocked) {

$stocked_where='';

}

elseif ($build_stocked) {

$stocked_where="and popt.products_options_track_stock = '1'";

}

elseif ($build_nonstocked) {

$stocked_where="and popt.products_options_track_stock = '0'";

}

 

$products_options_name_query = tep_db_query("select distinct popt.products_options_id, popt.products_options_name, popt.products_options_track_stock from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . (int)$this->products_id . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . (int)$languages_id . "' " . $stocked_where . " order by popt.products_options_name");

$attributes=array();

while ($products_options_name = tep_db_fetch_array($products_options_name_query)) {

$products_options_array = array();

// Modified 01-13-05 by Nicholas Rivera. Added product atribute sorting. Original code: $products_options_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$this->products_id . "' and pa.options_id = '" . (int)$products_options_name['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . (int)$languages_id . "'");

$products_options_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix, pa.products_attributes_id from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$this->products_id . "' and pa.options_id = '" . (int)$products_options_name['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . (int)$languages_id . "'" . " order by pa.options_values_price, pa.products_attributes_id");

while ($products_options = tep_db_fetch_array($products_options_query)) {

$products_options_array[] = array('id' => $products_options['products_options_values_id'], 'text' => $products_options['products_options_values_name']);

if ($products_options['options_values_price'] != '0') {

$products_options_array[sizeof($products_options_array)-1]['text'] .= ' (' . $products_options['price_prefix'] . $currencies->display_price($products_options['options_values_price'], tep_get_tax_rate($this->products_tax_class_id)) .')';

}

}

if (isset($cart->contents[$this->products_id]['attributes'][$products_options_name['products_options_id']]))

$selected = $cart->contents[$this->products_id]['attributes'][$products_options_name['products_options_id']];

else

$selected = 0;

$attributes[]=array('oid'=>$products_options_name['products_options_id'],

'oname'=>$products_options_name['products_options_name'],

'ovals'=>$products_options_array,

'default'=>$selected);

}

return $attributes;

}

 

 

/*

Method: _build_attributes_combinations

 

A recursive method for building an array enumerating the attribute combinations for the product

 

Parameters:

 

$attributes array An array of the attributes that combinations will be built for.

Format is as returned by _build_attributes_array.

$showoos boolean Flag indicating if non-stocked attributes should be built.

$markoos string 'Left' if out of stock indication is to be appended in front of the

attribute combination text. 'Right' if out of stock indication is

to be appended at the end of the attribute combination text.

$combinations array Array of the attribute combinations is returned in this parameter.

Should be set to an empty array before an external call to this method.

'comb' => array: array of a single attribute combination

options_id => options_value_id

'id' => string: options/values string for this

combination in the form for the

key of the products_stock table

opt_id-val_id,opt_id-val_id,...

'text' => string: Text for this combination. Values text

is as built by _build_attributes_array

and contains the add/subtract price for

the option value if applicable. Form is:

values_text, values_text

$selected_combination integer Index into the $combinations array of the combination that should

be the default selection when the combination is drawn is returned in

this parameter. Determined from product id. Should be set to zero

before an external call to this method.

 

Parameters for internal recursion use only:

 

$oidindex integer Index into the $attributes array of the option to operate on.

$comb array Array containing option id/values of combination built so far

products_options_id => products_options_value_id

$id string Contains string of options/values built so far

$text string Text for the options values constructed so far.

$isselected boolean Flag indicating if so far all option values in this combination

were indicated to be defaults in the product id.

 

 

Returns:

 

see $combinations and $selected_combination parameters above

no actual function return value.

 

*/

function _build_attributes_combinations($attributes, $showoos, $markoos, &$combinations, &$selected_combination, $oidindex=0, $comb=array(), $id="", $text='', $isselected=true) {

global $cart;

 

foreach ($attributes[$oidindex]['ovals'] as $attrib) {

$newcomb = $comb;

$newcomb[$attributes[$oidindex]['oid']] = $attrib['id'];

$newid=$id.','.$attributes[$oidindex]['oid'].'-'.$attrib['id'];

$newtext = $text.", ".$attrib['text'];

if (isset($cart->contents[$this->products_id]['attributes'][$attributes[$oidindex]['oid']]))

$newisselected = ($cart->contents[$this->products_id]['attributes'][$attributes[$oidindex]['oid']] == $attrib['id']) ? $isselected : false;

else

$newisselected = false;

if (isset($attributes[$oidindex+1])) {

$this->_build_attributes_combinations($attributes, $showoos, $markoos, $combinations, $selected_combination, $oidindex+1, $newcomb, $newid, $newtext, $newisselected);

}

else {

$is_out_of_stock=tep_check_stock(tep_get_prid($this->products_id),1,$newcomb);

if (!$is_out_of_stock | ($showoos == true)) {

switch ($markoos) {

case 'Left': $newtext=($is_out_of_stock ? TEXT_OUT_OF_STOCK.' - ' : '').substr($newtext,2);

break;

case 'Right': $newtext=substr($newtext,2).($is_out_of_stock ? ' - '.TEXT_OUT_OF_STOCK : '');

break;

default: $newtext=substr($newtext,2);

break;

}

$combinations[] = array('comb'=>$newcomb, 'id'=>substr($newid,1), 'text'=>$newtext);

if ($newisselected) $selected_combination = sizeof($combinations)-1;

}

}

}

}

 

 

/*

Method: _draw_js_stock_array

 

Draw a Javascript array containing the given attribute combinations.

Generally used to draw array of in-stock combinations for Javascript out of stock

validation and messaging.

 

Parameters:

 

$combinations array Array of combinations to build the Javascript array for.

Array must be of the form returned by _build_attributes_combinations

Usually this array only contains in-stock combinations.

 

Returns:

 

string: Javacript array definition. Excludes the "var xxx=" and terminating ";". Form is:

{optval1:{optval2:{optval3:1,optval3:1}, optval2:{optval3:1}}, optval1:{optval2:{optval3:1}}}

For example if there are 3 options and the instock value combinations are:

opt1 opt2 opt3

1 5 4

1 5 8

1 10 4

3 5 8

The string returned would be

{1:{5:{4:1,8:1}, 10:{4:1}}, 3:{5:{8:1}}}

 

*/

function _draw_js_stock_array($combinations) {

$out='';

foreach ($combinations[0]['comb'] as $oid=>$ovid) {

$out.='{'.$ovid.':';

$opts[]=$oid;

}

$out.='1';

 

for ($combindex=1; $combindex<sizeof($combinations); $combindex++) {

$comb=$combinations[$combindex]['comb'];

for ($i=0; $i<sizeof($opts)-1; $i++) {

if ($comb[$opts[$i]]!=$combinations[$combindex-1]['comb'][$opts[$i]]) break;

}

$out.=str_repeat('}',sizeof($opts)-1-$i).',';

if ($i<sizeof($opts)-1) {

for ($j=$i; $j<sizeof($opts)-1; $j++)

$out.=$comb[$opts[$j]].':{';

}

$out.=$comb[$opts[sizeof($opts)-1]].':1';

}

$out.=str_repeat('}',sizeof($opts));

 

return $out;

}

 

}

?>

 

 

This works like a charm... Is there any way to reverse the order to have the smaller numbers come up first?

 

Example 1 2 3 4 5 NOT 5 4 3 2 1 Currently, I have a store with different show sizes and the attributes are

5.5 6 6.5 7 7.5 8 8.5 9 10 With this sort option, size 10 is defaulted and when you select the drop down, you see 10 9 8.5 8 , etc....

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...