Different stock values for different product features
Posted: Sun Mar 09, 2008 12:05 pm
Hello,
First of all congratulations on this place.
I'm developing a webstore in php, and I want to create the possibility of associating different stock values to different product features.
Example:
I have two t-shirts of the model A, one in green and one in white. As it is today, I can only assign a total stock ammount of 2 items (white + green).
I want to have the possibility of associating a stock value for the model A white t-shirt, and another stock value to model A green t-shirt.
The creator of the code told me that the feature code is managed in the following files: browse.php , details.php , cart.php and checkout.php.
As I'm a total newbie concerning programming, I'd like to request assistance from this community, if possible.
Now I'll post each file's code.
browse.php
details.php
cart.php
checkout.php
Thank you in advance
First of all congratulations on this place.
I'm developing a webstore in php, and I want to create the possibility of associating different stock values to different product features.
Example:
I have two t-shirts of the model A, one in green and one in white. As it is today, I can only assign a total stock ammount of 2 items (white + green).
I want to have the possibility of associating a stock value for the model A white t-shirt, and another stock value to model A green t-shirt.
The creator of the code told me that the feature code is managed in the following files: browse.php , details.php , cart.php and checkout.php.
As I'm a total newbie concerning programming, I'd like to request assistance from this community, if possible.
Now I'll post each file's code.
browse.php
Code: Select all
1) { exit(); } ?> 0) { if (!empty($_GET['num_page'])) { $num_page = $_GET['num_page']; } else { $num_page = 1; } $start_record = ($num_page -1) * $products_per_page; $limit = " LIMIT $start_record, $products_per_page"; } else { $limit = ""; } ?> 0 AND `CATID`=%s ORDER BY `$orderby_field` ASC", quote_smart($cat)); } else { $query = sprintf("SELECT * FROM `".$dbtablesprefix."product` WHERE CATID=%s ORDER BY `$orderby_field` ASC", quote_smart($cat)); } } elseif ($action == "shownew") { if ($stock_enabled == 1 && IsAdmin() == false) { // filter out products with stock lower than 1 $query = "SELECT * FROM `".$dbtablesprefix."product` WHERE `STOCK` > 0 AND `NEW` = '1' ORDER BY `$orderby_field` ASC"; } else { $query = "SELECT * FROM `".$dbtablesprefix."product` WHERE `NEW` = '1' ORDER BY `$orderby_field` ASC"; } } else { //search on the given terms $searchitem = explode (" ", $searchfor); if ($stock_enabled == 1) { $searchquery = "WHERE `STOCK` > 0 AND ((DESCRIPTION LIKE '%" . $searchitem[0] . "%') OR (PRODUCTID = '" . $searchitem[0] . "'))"; } else { $searchquery = "WHERE ((DESCRIPTION LIKE '%" . $searchitem[0] . "%') OR (PRODUCTID = '" . $searchitem[0] . "'))"; } $counter = 1; while (!$searchitem[$counter] == NULL){ $searchquery = $searchquery . $searchmethod . "(DESCRIPTION LIKE '%" . $searchitem[$counter] . "%')"; $counter = $counter +1; } $query = "SELECT * FROM `".$dbtablesprefix."product` $searchquery ORDER BY `$orderby_field` ASC"; } // total products without the limit $sql = mysql_query($query) or die(mysql_error()); $num_products = mysql_num_rows($sql); // products optionally with the limit $sql = mysql_query($query.$limit) or die(mysql_error()); if (mysql_num_rows($sql) == 0) { echo "
"; if ($action == "list") { echo "".$txt['browse4'].""; } ?> "; echo $txt['browse3']; // if we use VAT, then display that the prices are including VAT in the list below if ($no_vat == 0) { echo " (".$txt['general7']." ".$txt['general5'].")"; } echo "
"; if ($action == "list") { echo "".$txt['browse4'].""; } echo ""; ?>
".$txt['browse5']."
"; } else { $optel = 0; while ($row = mysql_fetch_row($sql)) { $optel++; if ($optel == 3) { $optel = 1; } if ($optel == 1) { $kleur = ""; } if ($optel == 2) { $kleur = " class=\"altrow\""; } // the price gets calculated here $printprijs = $row[4]; // from the database if ($db_prices_including_vat == 0 && $no_vat == 0) { $printprijs = $row[4] * $vat; } $printprijs = myNumberFormat($printprijs,$number_format); // format to our settings // reset values $picturelink = ""; $new = ""; $thumb = ""; // new product? if ($row[7] == 1) { $new = "" . $txt['general3']. ""; } // is there a picture? if ($search_prodgfx == 1 && $use_prodgfx == 1) { if ($pictureid == 1) { $picture = $row[0]; } else { $picture = $row[1]; } // determine resize of thumbs $width = ""; $height = ""; if ($pricelist_thumb_width != 0) { $width = " width=\"".$pricelist_thumb_width."\""; } if ($pricelist_thumb_height != 0) { $height = " height=\"".$pricelist_thumb_height."\""; } if (thumb_exists($product_dir ."/". $picture . ".jpg")) { $thumb = "\"\""; } if (thumb_exists($product_dir ."/". $picture . ".gif")) { $thumb = "\"\""; } if (thumb_exists($product_dir ."/". $picture . ".png")) { $thumb = "\"\""; } // if the script uses make_thumbs, then search for thumbs if ($make_thumbs == 1) { if (thumb_exists($product_dir ."/tn_". $picture . ".jpg")) { $thumb = "\"\""; } if (thumb_exists($product_dir ."/tn_". $picture . ".gif")) { $thumb = "\"\""; } if (thumb_exists($product_dir ."/tn_". $picture . ".png")) { $thumb = "\"\""; } } if ($thumb != "" && $thumbs_in_pricelist == 0) { // use a photo icon instead of a thumb $picturelink = ""; $thumb = ""; } } // see if you are an admin. if so, add a [EDIT] link to the line $admin_edit = ""; if (IsAdmin() == true) { $admin_edit = "
"; if ($stock_enabled == 1) { $admin_edit = $admin_edit.$txt['productadmin12']." ".$row[5]; } $admin_edit = $admin_edit." | ".$txt['browse7'].""; $admin_edit = $admin_edit." | ".$txt['browse8'].""; $admin_edit = $admin_edit." | ".$txt['browse10'].""; } // make up the description to print according to the pricelist_format and max_description if ($pricelist_format == 0) { $print_description = $row[1]; } if ($pricelist_format == 1) { $print_description = $row[3]; } if ($pricelist_format == 2) { $print_description = $row[1]." - ".$row[3]; } if ($max_description != 0) { $description = stringsplit($print_description, $max_description); // so lets only show the first xx characters if (strlen($print_description) != strlen($description[0])) { $description[0] = $description[0] . ".."; } $print_description = $description[0]; } $print_description = strip_tags($print_description); //remove html because of danger of broken tags echo ""; // see what the stock is if ($stock_enabled == 0) { if ($row[5] == 1) { $stockpic = "\"".$txt['db_stock1']."\" "; } // in stock if ($row[5] == 0) { $stockpic = "\"".$txt['db_stock2']."\" "; } // out of stock if ($row[5] == 2) { $stockpic = "\"".$txt['db_stock3']."\" "; } // in backorder } else { $stockpic = ""; } echo "".$stockpic."".$thumb.$print_description." ".$picturelink." ".$new." ".$admin_edit.""; echo "
"; if ($order_from_pricelist == 1) { ?>
". $currency_symbol_pre.$in_vat.$currency_symbol_post.""; } else { if ($db_prices_including_vat == 1) { $ex_vat = $row[4] / $vat; $in_vat = myNumberFormat($row[4],$number_format); $ex_vat = myNumberFormat($ex_vat,$number_format); } else { $in_vat = $row[4] * $vat; $ex_vat = myNumberFormat($row[4],$number_format); $in_vat = myNumberFormat($in_vat,$number_format); } echo "".$currency_symbol_pre.$in_vat.$currency_symbol_post.""; echo "
(".$currency_symbol_pre.$ex_vat.$currency_symbol_post." ".$txt['general6']." ".$txt['general5'].")"; } // product features $allfeatures = $row[8]; if (!empty($allfeatures)) { $features = explode("|", $allfeatures); $counter1 = 0; echo "
"; while (!$features[$counter1] == NULL){ $feature = explode(":", $features[$counter1]); $counter1 += 1; echo "
".$feature[0].": "; echo ""; } } ?>
:
".$currency_symbol." ".$printprijs.""; } echo "
"; echo ""; } ?>
0 && $num_products > $products_per_page) { $page_counter = 0; $num_pages = 0; $rest_products = $num_products; echo "
".$txt['browse11'].": "; for($i = 0; $i < $num_products; $i++) { $page_counter++; if ($page_counter == $products_per_page) { $num_pages++; $page_counter = 0; $rest_products = $rest_products - $products_per_page; if ($num_pages == $num_page) { echo "[$num_pages]"; } else { echo "[$num_pages]"; } echo " "; } } // the rest (if any) if ($rest_products > 0) { $num_pages++; if ($num_pages == $num_page) { echo "[$num_pages]"; } else { echo "[$num_pages]"; } } echo "
"; } ?>
"; ?>
"; ?>
"; ?>
Code: Select all
1) { exit(); } ?> $product_max_height) { $height = $product_max_height; $percent = ($size[1] / $height); $width = round(($size[0] / $percent)); $resized = 1; } if ($width > $product_max_width) { $width = $product_max_width; $percent = ($size[0] / $width); $height = round(($size[1] / $percent)); $resized = 1; } if ($resized == 0) { $screenshot = "\"\""; } else { if ($use_imagepopup == 0) { $screenshot = "\"\"/
".$txt['details9'].""; } else {$screenshot = "\"\"/
".$txt['details9'].""; } } } ?>
:
"; $admin_edit = $admin_edit."".$txt['browse7'].""; $admin_edit = $admin_edit." | ".$txt['browse8'].""; $admin_edit = $admin_edit." | ".$txt['browse10'].""; } ?>
:
*
" . $txt['details5'] . ": ". $currency_symbol_pre.$in_vat.$currency_symbol_post.""; } else { if ($db_prices_including_vat == 1) { $ex_vat = $row[4] / $vat; $in_vat = myNumberFormat($row[4],$number_format); $ex_vat = myNumberFormat($ex_vat,$number_format); } else { $in_vat = $row[4] * $vat; $ex_vat = myNumberFormat($row[4],$number_format); $in_vat = myNumberFormat($in_vat,$number_format); } echo "" . $txt['details5'] . ": ".$currency_symbol_pre.$in_vat.$currency_symbol_post.""; echo "
(".$currency_symbol_pre.$ex_vat.$currency_symbol_post." ".$txt['general6']." ".$txt['general5'].")"; } // product features $allfeatures = $row[8]; if (!empty($allfeatures)) { $features = explode("|", $allfeatures); $counter1 = 0; echo "
"; while (!$features[$counter1] == NULL){ $feature = explode(":", $features[$counter1]); $counter1 += 1; echo "
".$feature[0].": "; echo ""; } } ?>
:
Code: Select all
1) { exit(); } ?> $row[0] || $row[0] == 0) { $numordered = $numprod + $num_in_basket; $warning = $txt['checkout15']."
".$txt['checkout7']." ".$numordered."
".$txt['checkout8']." ".$row[0]; PutWindow($gfx_dir, $txt['general12'], $warning, "warning.gif", "50"); $error = 1; } } if ($error == 0) { // product features $query = sprintf("SELECT `FEATURES` FROM `".$dbtablesprefix."product` WHERE `ID` = %s", quote_smart($prodid)); $sql = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_row($sql); $allfeatures = $row[0]; $productfeatures = ""; if (!empty($allfeatures)) { $features = explode("|", $allfeatures); $counter1 = 0; echo "
"; while (!$features[$counter1] == NULL){ $feature = explode(":", $features[$counter1]); $counter1 += 1; if (!empty($_POST["$feature[0]"])) { $productfeatures .= $feature[0].": ".$_POST["$feature[0]"]; if (!empty($features[$counter1])) { $productfeatures .= ", "; } } } } // now lets check if the product we add is new, or we need to update an existing record $query = "SELECT `ID` FROM `".$dbtablesprefix."basket` WHERE `CUSTOMERID` = '".$customerid."' AND `PRODUCTID` = '".$prodid."' AND `FEATURES` = '". $productfeatures . "'"; $sql = mysql_query($query) or die(mysql_error()); if (mysql_num_rows($sql) == 0) { $query = "INSERT INTO `".$dbtablesprefix."basket` ( `CUSTOMERID` , `PRODUCTID` , `STATUS` , `ORDERID` , `LINEADDDATE` , `QTY` , `FEATURES`) VALUES ( '" . $customerid . "', '" . $prodid . "', 'BASKET', '0' , '" . Date("d-m-Y @ G:i") . "' , '" . $numprod . "', '".$productfeatures. "')"; } else { $query = "UPDATE `".$dbtablesprefix."basket` SET `QTY` = `QTY` + ". $numprod. " WHERE `PRODUCTID` = '".$prodid."' AND `CUSTOMERID` = '".$customerid."'"; } $sql = mysql_query($query) or die(mysql_error()); } } if ($action=="update"){ if ($numprod == 0) { $query = "DELETE FROM `".$dbtablesprefix."basket` WHERE ID = " . $basketid; $sql = mysql_query($query) or die(mysql_error()); } else { // if we work with stock amounts, then lets check if there is enough in stock if ($stock_enabled == 1) { $query = "SELECT `STOCK` FROM `".$dbtablesprefix."product` WHERE `ID` = '".$prodid."'"; $sql = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_row($sql); if ($numprod > $row[0] || $row[0] == 0) { PutWindow($gfx_dir, $txt['general12'], $txt['checkout15']."
".$txt['checkout7']." ".$numprod."
".$txt['checkout8']." ".$row[0], "warning.gif", "50"); $error = 1; } } if ($error == 0) { $query = "UPDATE `".$dbtablesprefix."basket` SET `CUSTOMERID` = " . $customerid . ", `PRODUCTID` = '" . $prodid . "', `STATUS` = 'BASKET', `ORDERID` = 0, `LINEADDDATE` = '" . Date("d-m-Y @ G:i") . "', `QTY` = " . $numprod . " WHERE `ID` = " . $basketid; $sql = mysql_query($query) or die(mysql_error()); } } } if ($action=="empty"){ $query = "DELETE FROM ".$dbtablesprefix."basket WHERE CUSTOMERID = " . $customerid; $sql = mysql_query($query) or die(mysql_error()); } // read basket $query = "SELECT * FROM ".$dbtablesprefix."basket WHERE (CUSTOMERID = " . $customerid . " AND STATUS = 'BASKET') ORDER BY ID"; $sql = mysql_query($query) or die(mysql_error()); $count = mysql_num_rows($sql); if ($count == 0) { PutWindow($gfx_dir, $txt['cart1'], $txt['cart2'], "carticon.gif", "50"); } else { ?>"; } if (thumb_exists($product_dir ."/". $picture . ".gif")) { $thumb = " \"\""; } if (thumb_exists($product_dir ."/". $picture . ".png")) { $thumb = "\"\""; } // if the script uses make_thumbs, then search for thumbs if ($make_thumbs == 1) { if (thumb_exists($product_dir ."/tn_". $picture . ".jpg")) { $thumb = "\"\""; } if (thumb_exists($product_dir ."/tn_". $picture . ".gif")) { $thumb = "\"\""; } if (thumb_exists($product_dir ."/tn_". $picture . ".png")) { $thumb = "\"\""; } } if ($thumb != "" && $thumbs_in_pricelist == 0) { // use a photo icon instead of a thumb $picturelink = ""; $thumb = ""; } } // make up the description to print according to the pricelist_format and max_description if ($pricelist_format == 0) { $print_description = $row_details[1]; } if ($pricelist_format == 1) { $print_description = $row_details[3]; } if ($pricelist_format == 2) { $print_description = $row_details[1]." - ".$row_details[3]; } if ($max_description != 0) { $description = stringsplit($print_description, $max_description); // so lets only show the first xx characters if (strlen($print_description) != strlen($description[0])) { $description[0] = $description[0] . ".."; } $print_description = $description[0]; } $print_description = strip_tags($print_description); //remove html because of danger of broken tags ?> >
(".$printvalue.")"; } } ?>
(".$currency_symbol_pre.$totaal_ex.$currency_symbol_post." ".$txt['general6']." ".$txt['general5'].")"; } ?>
"; // lets find out the category of the last added product $query = "SELECT `CATID` FROM `".$dbtablesprefix."product` WHERE `ID` = '".$prodid."'"; $sql = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_row($sql)) { $jump2cat = $row[0]; } echo ""; echo ""; echo ""; echo ""; } ?>
"; } else { echo "
"; } if ($ordering_enabled == 1) { echo ""; } ?>
Code: Select all
1) { exit(); } ?> "; while ($row = mysql_fetch_row($sql)) { $query_details = "SELECT * FROM ".$dbtablesprefix."product WHERE ID = '" . $row[2] . "'"; $sql_details = mysql_query($query_details) or die(mysql_error()); while ($row_details = mysql_fetch_row($sql_details)) { $product_price = $row_details[4]; // additional costs for features? if (!empty($row[7])) { // features might involve extra costs, but we don't want to show them $features = explode(", ", $row[7]); $counter1 = 0; $printvalue = ""; while (!$features[$counter1] == NULL){ $feature = explode("+",$features[$counter1]); $printvalue .= $feature[0]; // don't show the extra costs here, just show the feature $counter1 += 1; if (!empty($features[$counter1])) { $printvalue .= ", "; } $product_price += $feature[1]; // if there are extra costs, let's add them } } if ($no_vat == 0 && $db_prices_including_vat == 0) { $product_price = $product_price * $vat; } //$product_price *= $row[6]; this is wrong. it should be the price per piece, not the total price. // make up the description to print according to the pricelist_format and max_description if ($pricelist_format == 0) { $print_description = $row_details[1]; } if ($pricelist_format == 1) { $print_description = $row_details[3]; } if ($pricelist_format == 2) { $print_description = $row_details[1]." - ".$row_details[3]; } if ($max_description != 0) { $description = stringsplit($print_description, $max_description); // so lets only show the first xx characters if (strlen($print_description) != strlen($description[0])) { $description[0] = $description[0] . ".."; } $print_description = $description[0]; $print_description = strip_tags($print_description); // strip html because half html can mess up the layout } $print_description = strip_tags($print_description); //remove html because of danger of broken tags if (!empty($row[7])) { $print_description .= "
".$printvalue; } // product features $message .= "".$row[6].$txt['checkout4']."".$print_description."".$currency_symbol_pre."".myNumberFormat($product_price,$number_format).$currency_symbol_post.$txt['checkout5'].""; $total = $total + ($product_price * $row[6]); // update stock amount if needed if ($stock_enabled == 1) { if ($row[6] > $row_details[5] || $row_details[5] == 0) { // the product stock is too low, so we have to cancel this order include ($lang_file); PutWindow($gfx_dir, $txt['general12'], $txt['checkout15']." ".$print_description."
".$txt['checkout7']." ".$row[6]."
".$txt['checkout8']." ".$row_details[5], "warning.gif", "50"); $del_query = sprintf("DELETE FROM `".$dbtablesprefix."order` WHERE (`ID` = %s)", quote_smart($lastid)); $del_sql = mysql_query($del_query) or die(mysql_error()); $error = 1; } else { $new_stock = $row_details[5] - $row[6]; $update_query = "UPDATE `".$dbtablesprefix."product` SET `STOCK` = ".$new_stock." WHERE `ID` = '".$row_details[0]."'"; $update_sql = mysql_query($update_query) or die(mysql_error()); } } } } $message .= ""; if ($error == 0) { // if the customer added additional notes/questions, we will display them too if (!empty($_POST['notes'])) { $message = $message . $txt['checkout6'].$txt['checkout6']; // white line $message = $message . $txt['shipping3']."
".nl2br($notes); } // first the shipping description $query = sprintf("SELECT * FROM `".$dbtablesprefix."shipping` WHERE `id` = %s", quote_smart($shippingid)); $sql = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_row($sql)) { $shipping_descr = $row[1]; } // read the shipping costs $query = sprintf("SELECT * FROM `".$dbtablesprefix."shipping_weight` WHERE `ID` = %s", quote_smart($weightid)); $sql = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_row($sql)) { $sendcosts = $row[4]; } include ($lang_file); // update sendcost in language file $message = $message . $txt['checkout6'].$txt['checkout6']; // white line $message = $message . $txt['checkout16'].$shipping_descr; // shipping method $message = $message . $txt['checkout6'].$txt['checkout6']; // white line // shippingmethod 2 is pick up at store. if you don't support this option, there is no need to remove this if ($shippingid == "2") { $message = $message . $txt['checkout18']; // appointment line } else { $message = $message . $txt['checkout17']; // shipping address } $message = $message . $txt['checkout6'].$txt['checkout6']; // white line // now lets calculate the invoice total now we know the final addition, the shipping costs $total = $total + $sendcosts; $totalprint = myNumberFormat($total,$number_format); $print_sendcosts = myNumberFormat($sendcosts,$number_format); $total_nodecimals = number_format($total, 2,"",""); include ($lang_file); // now the payment $query = sprintf("SELECT * FROM `".$dbtablesprefix."payment` WHERE `id` = %s", quote_smart($payment)); $sql = mysql_query($query) or die(mysql_error()); // read the payment method while ($row = mysql_fetch_row($sql)) { $payment_descr = $row[1]; $payment_code = $row[2]; // there could be some variables in the code, like %total%, %webid% and %shopurl% so lets update them with the correct values $payment_code = str_replace("%total_nodecimals%", $total_nodecimals, $payment_code); $payment_code = str_replace("%total%", $total, $payment_code); $payment_code = str_replace("%webid%", $webid, $payment_code); $payment_code = str_replace("%shopurl%", $shopurl, $payment_code); } $message = $message . $txt['checkout19'].$payment_descr; // Payment method: $message = $message . $txt['checkout6']; // line break // paypal and ideal use extra code to checkout. if there is extra code, then we paste it here if ($payment_code <> "") { $message = $message . $payment_code; $message = $message . $txt['checkout6']; // line break } // the two standard build in payment methods if ($payment == "1") { $message = $message . $txt['checkout20']; // bank info } if ($payment == "2") { $message = $message . $txt['checkout21']; // pick up at store info } $message = $message . $txt['checkout6'].$txt['checkout6']; // white line $message = $message . $txt['checkout24']; // total invoice amount if ($no_vat == 0) { $message = $message ." ". $txt['checkout25']; } // including VAT line if needed // if pay at the store, you don't need to pay within 14 days if ($payment <> "2") { $message = $message . $txt['checkout6'].$txt['checkout6']; // new line $message = $message . $txt['checkout26']; // pay within xx days } $message = $message . $txt['checkout6'].$txt['checkout6']; // white line $message = $message . $txt['checkout9']; // direct link to customer order for online status checking // order update $query = "UPDATE `".$dbtablesprefix."order` SET `TOPAY` = '" . $total . "' WHERE `ID` = " . $lastid; $sql = mysql_query($query) or die(mysql_error()); //basket update $query = sprintf("DELETE FROM `".$dbtablesprefix."basket` WHERE (`CUSTOMERID` = %s)", quote_smart($customerid)); $sql = mysql_query($query) or die(mysql_error()); // now lets show the customer some details echo "
\"step \"step \"you \"step
"; // email and save the order in HTML $html_header = ""; $html_header = $html_header.""; $html_footer = ""; $full_message = $html_header.$message.$html_footer; $short_message = $txt['checkout10'].$txt['checkout9']; $subject = $txt['checkout10']; if (mymail($webmaster_mail, $to, $subject, $short_message, $smtp_server, $smtp_port, $smtp_user, $smtp_pass, $charset)) { PutWindow($gfx_dir, $txt['general13'], $txt['checkout11'], "notify.gif", "50"); } else { PutWindow($gfx_dir, $txt['general12'], $txt['checkout12'], "warning.gif", "50"); } mymail($webmaster_mail, $sales_mail, $subject, $short_message, $smtp_server, $smtp_port, $smtp_user, $smtp_pass, $charset); // no error checking here, because there is no use to report this to the customer // save the order in order folder for administration $security = " 1) { exit(); } ?>"; $handle = fopen ($orders_dir."/".strval($webid).".php", "w+"); if (!fwrite($handle, $security.$message)) { $retVal = false; } else { fclose($handle); } // now print the confirmation on the screen ?>