Click here to Skip to main content
15,887,944 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, i created cart detail page and fetched data of user selected product. But, i don't know how to calcute product price as subtotal.

What I have tried:

PHP
<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
			$total = 0;
			$pro_id = $_COOKIE['product_id'];   
			$pro_id = explode(',', $pro_id);
			foreach ($pro_id as $val) {
				$ans = "SELECT * FROM wm_products WHERE pro_id='$val'";
				$result = $conn->query($ans);
				while($row = $result->fetch_assoc()){
				?>
					<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="-" type="button">
								<label>Quantity:</label>
								<input size="4" class="input-text qty text" title="Qty" value="1" name="cart[92f54963fc39a9d87c2253186808ea61][qty]" max="29" min="0" step="1" type="number">
								<input class="plus" value="+" type="button">
							</div>
						</td>
						<td data-title="Total" class="product-subtotal" align="right">
							<span class="amount">Rs. <?php echo $row['pro_price']; ?></span>
						</td>
						<td class="product-remove">
							<a class="remove remove_cart" href="#" dataid="<?php echo $row['pro_id']; ?>">×</a>
						</td>
					</tr>
				<?php
				$subtotal = $total + ($row['pro_price']);
				}                                   
			}			
			?>
			<tr>			
				<td colspan="4" align="right">Subtotal :</td>
				<td align="right">
					<span class="amount">Rs.
						<?php
							echo $subtotal;
						?>
					</span>
				</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="submit">

			<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>
Posted
Updated 30-Jun-18 1:24am

1 solution

SELECT Id, SUM(Quantity * (select Price from Product where Id = Id)) as qty
FROM OrderItem o
WHERE OrderId = @OrderId
 
Share this answer
 
Comments
CHill60 3-Jul-18 7:31am    
I think I would prefer a join to a correlated sub-query. Id = Id will produce an error as SQL will not be able to determine which table each Id refers to

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900