funkydunk |
04-19-2004 07:18 AM |
Add multiple products to cart
As it is my birthday tomorrow :) (cakes appreciated)
Here is a little mod that allows you to run the product listing page as an order form where the visitor can add in amounts for several products at a time and have them all added to the cart simultaeneously.
First of all create a new php script in customer: add_multiple.php
Code:
<?php
// add_multiple.php funkydunk 2004
require "./auth.php";
include $xcart_dir."/shipping/shipping.php";
//include "./nocookie_warning.php";
x_session_register("cart");
x_session_register("intershipper_rates");
x_session_register("intershipper_recalc");
x_session_unregister("secure_oid");
x_session_register("extended_userinfo");
x_session_register("anonymous_checkout");
#
# $order_secureid (for security reasons)
#
x_session_register("order_secureid");
#
# Loop through products and add to cart
# code taken from cart.php
if ($amount) {
foreach ($amount as $key=>$value) {
$productid = $key;
$amount = $value;
$added_product = func_select_product($productid, (!empty($user_account['membership'])?$user_account['membership']:""), false, true);
$amount = abs(intval($amount));
#
# Add to cart amount of items that is not much than in stock
#
if ($config["General"]["unlimited_products"]=="N" && $added_product["product_type"]!="C")
if ($amount > $added_product["avail"])
$amount = $added_product["avail"];
if ($productid && $amount) {
if ($amount < $added_product["min_amount"]) {
func_header_location ("error_message.php?minimum_amount");
}
#
# Do addition to cart
# With options
#
$options=array();
if(!empty($product_options)) {
if (is_array($product_options))
foreach($product_options as $key=>$product_option)
$product_options[$key] = stripslashes($product_option);
if ($active_modules["Product_Options"])
func_check_product_options ($productid, $product_options);
foreach($product_options as $key=>$product_option)
$options[]=array("optclass"=>$key,"optionindex"=>$product_option);
}
$found = false;
if (!empty($cart) and @$cart["products"] and $added_product["product_type"]!="C") {
foreach ($cart["products"] as $k=>$v) {
if (($v["productid"] == $productid) && (!$found) && ($v["options"] == $options) && empty($v["hidden"])) {
$found = true;
$distribution = array_pop(func_query_first("select distribution from $sql_tbl[products] where productid='$productid'"));
if (($cart["products"][$k]["amount"] >=1) && ($distribution))
{$cart["products"][$k]["amount"]=1; $amount=0;}
$cart["products"][$k]["amount"] += $amount;
}
}
}
if (!$found) {
#
# Add product to the cart
#
if (!empty($price))
# price value is defined by customer if admin set it to '0.00'
$free_price = abs(doubleval($price));
$cartid = func_generate_cartid($cart["products"]);
$cart["products"][]=array("cartid"=>$cartid, "productid"=>$productid,"amount"=>$amount, "options"=>$options, "free_price"=>@price_format(@$free_price));
if(!empty($active_modules["Product_Configurator"]))
include $xcart_dir."/modules/Product_Configurator/pconf_customer_cart.php";
}
$intershipper_recalc = "Y";
}
}
} // end of loop to add all products to the cart
#
# Redirect
#
/*
if($mode=="add" and $productid) {
if($config["General"]["redirect_to_cart"]=="Y")
func_header_location("cart.php");
else
func_header_location("home.php?cat=$cat&page=$page");
}
*/
func_header_location("cart.php");
x_session_save();
$smarty->display("customer/home.tpl");
?>
Then create two language fields:
err_minimum_amount
err_minimum_amount_txt
These will be shown at the top of the error page that is shown if the user does not order enough of something as follows:
Then create a new template in skin1/main/minimum_amount.tpl :
Code:
{* minimum_amount.tpl - funkydunk 2004 *}
{ include file="location.tpl" last_location=$lng.err_minimum_amount}
{$lng.err_minimum_amount_txt}
Then amend skin1/common_templates.tpl by inserting:
Code:
{* new funkydunk *}
{elseif $main eq "minimum_amount"}
{include file="main/minimum_amount.tpl"}
{* end *}
before the final {else}
Then amend the products.tpl and products_t.tpl as follows:
(this is the code for products.tpl without images but you can get the idea from this :)
Code:
{* $Id: products.tpl,v 1.36 2003/11/11 14:02:37 svowl Exp $ amended by funkydunk 2004 *}
{if $usertype eq "C" and $config.Appearance.products_per_row ne "" and $config.Appearance.products_per_row gt 0 and $config.Appearance.products_per_row lt 4 and ($featured eq "Y" or $config.Appearance.featured_only_multicolumn eq "N")}
{include file="customer/main/products_t.tpl" products=$products}
{else}
{if $products}
<form action="add_multiple.php" method ="post" name="orderform">
<table border="0" width="100%" cellpadding="2" cellspacing="0" align="center">
<tr>
<td valign="top" class="headerrow">product</td>
<td valign="top" width="80" class="headerrow" align="right">price</td>
<td valign="top" width="80" class="headerrow" align="center">info</td>
<td valign="top" width="100" class="headerrow" align="center">quantity</td>
</tr>
{section name=product loop=$products}
{counter assign="row"}
<tr>
<td valign="top" class="listrow"><a href=product.php?productid={$products[product].productid}>{$products[product].product}</a>
{if $products[product].min_amount gt 1}
<FONT class=small>({$lng.txt_need_min_amount} {$products[product].min_amount} {$lng.lbl_items})</FONT>{/if}
</td>
<td valign="top" width="80" class="listrow" nowrap align="right">{include file="currency.tpl" value=$products[product].price}</td>
<td valign="top" width="80" class="listrow" align="center"><a href=product.php?productid={$products[product].productid}>[img]{$ImagesDir}/info.gif[/img]</a></td>
<td valign="top" width="100" class="listrow" align="center">
{if ($active_modules.Product_Options ne "" and $products[product].product_options)}
{else}
<input type="text" size="4" maxlength="10" name="amount[{$products[product].productid}]" value="0">
{/if}
</td>
</tr>
{/section}
<tr>
<td valign="top" colspan="4" align="right">
{if $js_enabled}
{include file="buttons/button.tpl" button_title="ADD ALL TO CART" style="button"}
{else}
{include file="submit_wo_js.tpl" value="ADD ALL TO CART"}
{/if}
</td>
</tr>
</table>
</form>
{else}
{$lng.lbl_no_products_found}
{/if}
{/if}
There are a few style sheets references in there that aren't standard but you should be able to get it working.
PLEASE NOTE THAT YOU SHOULD BACKUP BEFORE DOING ANY MODS FROM THE FORUM.
This was built for version 3.5.x gold but should work on Pro and may even work on 3.4 shops.
Have fun :wink:
|