使用cookies添加到购物车 [英] Add to cart using cookies
本文介绍了使用cookies添加到购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了cart.php页面。在此页面中显示产品图片,数量,产品价格,总产品价格(*数量)和产品价格的总和。我使用cookies在cart.php页面上显示产品。如果数量增加或减少,我想计算产品价格。另外,我想要显示产品价格的总计。请帮忙解决这个问题。我提供以下代码供您参考。
我尝试过:
< table 类 = shop_table shop_table_responsive cart >
< thead >
< tr >
< th class = product-thumbnail > ; 图片< / th >
< th class = product-name > 产品< / th >
< th class = product-price > 价格< / th >
< th class = product-quantity > 数量< / th >
< th 类 = product-小计 > 总< / th >
< class = product-remove > < / th >
< / tr >
< / thead >
< tbody >
<? php
$ pro_id = $ _COOKIE [' product_id'];
$ pro_id = explode(' ,' ,$ pro_id);
$ cnt = count(explode( ,,$ _COOKIE [' product_id']));
foreach ($ pro_id as $ val){
$ ans = SELECT * FROM wm_products WHERE pro_id ='$ val'< /跨度>;
$ result = $ conn-> query($ ans);
$ totalprice = 0;
$ totalqunty = 0;
while($ row = $ result-> fetch_assoc()){
$ cnt = $ pro_id [$ val] [' 数量'];
$ sub_total = $ row [' pro_price'] * $ cnt;
?>
< tr class = cart_item >
< td class = product-thumbnail >
< a href = single-product.html > 跨度> < img src = <?php echo $ row ['pro_img_path']; ?> alt = width = 180 高度 = 180 > < / a >
< / td >
< td data-title = < span class =code-keyword> Product class = product-name >
< a href = single-product.html > <? php echo $ row [' pro_name']; ?> < / a < span class =code-keyword>>
< / td >
< td data-title = < span class =code-keyword>价格 class = product-price >
< span 类 = 金额 > Rs。 <? php echo $ row [' pro_price']; ?> < / span < span class =code-keyword>>
< / td >
< td data-title = < span class =code-keyword>数量 class = product-quantity >
< div 类 = 数量buttons_added >
< 输入 class = minus value = - onclick =' javascript:subtractQty();' type = 按钮 >
< 标签 > 数量:< / label >
< 输入 id =' qty' size = 4 class = input-text qty text title = 数量 值 = {$ cnt} 名称 = 数量 max = < span class =code-keyword> 29 min = 0 步骤 = 1 type = number >
< 输入 class = < span class =code-keyword> plus value = + onclick =' javascript:document.getElementById(qty)。value ++;' < span class =code-attribute> type = 按钮 >
< / div >
< / td >
< td data-title = 总计 class = product-subtotal >
< span class = amount > Rs。 <? php echo $ sub_total ; ?> < / span < span class =code-keyword>>
< / td >
< td class = product-remove >
< a class = remove remove_cart href = # dataid = <?php echo $ row ['pro_id']; ?> > ×< / a >
< / td >
< / tr >
<? php
}
}
?>
< tr >
< td class = actions colspan = 6 >
< div class = 优惠券 >
< label = coupon_code > 优惠券:< / label > < 输入 占位符 = 优惠券代码 < span class =code-attribute> value = id = coupon_code class = 输入文本 名称 = coupon_code type = text > < 输入 value = 应用优惠券 名称 = apply_coupon class < span class =code-keyword> = button 类型 = 提交 >
< / div >
< 输入 value = 更新购物车 名称 = update_cart class = 按钮 类型 = 按钮 >
< div class = wc-proceed-to-checkout >
< a class = 结帐 - 按钮alt wc-forward href = checkout.php > 继续结帐< / a >
< / div >
< 输入 value = 1eafc42c5e name = _ wpnonce id = _ wpnonce 类型 = 隐藏 > < input value = / electro / cart / name = _ wp_http_referer type = hidden >
< / td >
< / tr >
< / tbody < span class =code-keyword>>
< / table >
解决方案
pro_id =
_COOKIE [' product_id'];
pro_id = explode(' ,跨度>,
I have create cart.php page. In this page show product image, quantity, product price, total Product price(*quantity) and grand total of product price. I show product on cart.php page using cookies. I want to calculate product price if quantity is increase or decrease. Also, i want show grand total of product price. Please help to solve this problem. I provide following my code for your reference.
What I have tried:
<table class="shop_table shop_table_responsive cart">
<thead>
<tr>
<th class="product-thumbnail">Image</th>
<th class="product-name">Product </th>
<th class="product-price">Price</th>
<th class="product-quantity">Quantity</th>
<th class="product-subtotal">Total</th>
<th class="product-remove"> </th>
</tr>
</thead>
<tbody>
<?php
$pro_id = $_COOKIE['product_id'];
$pro_id = explode(',', $pro_id);
$cnt = count(explode(",", $_COOKIE['product_id']));
foreach ($pro_id as $val) {
$ans = "SELECT * FROM wm_products WHERE pro_id='$val'";
$result = $conn->query($ans);
$totalprice=0;
$totalqunty=0;
while($row = $result->fetch_assoc()){
$cnt=$pro_id[$val]['quantity'];
$sub_total=$row['pro_price']*$cnt;
?>
<tr class="cart_item">
<td class="product-thumbnail">
<a href="single-product.html"><img src="<?php echo $row['pro_img_path']; ?>" alt="" width="180" height="180"></a>
</td>
<td data-title="Product" class="product-name">
<a href="single-product.html"><?php echo $row['pro_name']; ?></a>
</td>
<td data-title="Price" class="product-price">
<span class="amount">Rs. <?php echo $row['pro_price']; ?></span>
</td>
<td data-title="Quantity" class="product-quantity">
<div class="quantity buttons_added">
<input class="minus" value="-" onclick='javascript: subtractQty();' type="button">
<label>Quantity:</label>
<input id='qty' size="4" class="input-text qty text" title="Qty" value="{$cnt}" name="quantity" max="29" min="0" step="1" type="number">
<input class="plus" value="+" onclick='javascript: document.getElementById("qty").value++;' type="button">
</div>
</td>
<td data-title="Total" class="product-subtotal">
<span class="amount">Rs. <?php echo $sub_total; ?></span>
</td>
<td class="product-remove">
<a class="remove remove_cart" href="#" dataid="<?php echo $row['pro_id']; ?>">×</a>
</td>
</tr>
<?php
}
}
?>
<tr>
<td class="actions" colspan="6">
<div class="coupon">
<label for="coupon_code">Coupon:</label> <input placeholder="Coupon code" value="" id="coupon_code" class="input-text" name="coupon_code" type="text"> <input value="Apply Coupon" name="apply_coupon" class="button" type="submit">
</div>
<input value="Update Cart" name="update_cart" class="button" type="button">
<div class="wc-proceed-to-checkout">
<a class="checkout-button button alt wc-forward" href="checkout.php">Proceed to Checkout</a>
</div>
<input value="1eafc42c5e" name="_wpnonce" id="_wpnonce" type="hidden"><input value="/electro/cart/" name="_wp_http_referer" type="hidden">
</td>
</tr>
</tbody>
</table>
解决方案
pro_id =
_COOKIE['product_id'];
pro_id = explode(',',
这篇关于使用cookies添加到购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文