2 years ago

#71266

test-img

charlene

how to change or update the quantity and total price?

This is my code in cart.php. I want to change or update the quantity according to the user want also the total price when updating the quantities but i dont know how to do it. How to fix this? I have one but when u want to update the quantity u removed first ur cart item...that's not i want... my goal is to update the quantity and total price should be displayed on the cart form.

<?php 

if(isset($_POST["add_to_cart"]))
{
    if(isset($_SESSION["shopping_cart"]))
    {
        $item_array_id = array_column($_SESSION["shopping_cart"], "item_id");
        if(!in_array($_GET["id"], $item_array_id))
        {
            $count = count($_SESSION["shopping_cart"]);
            $item_array = array(
                'item_id'           =>  $_GET["id"],
                'item_name'         =>  $_POST["hidden_name"],
                'item_image'        =>  $_POST["hidden_image"],
                'item_price'        =>  $_POST["hidden_price"],
                'item_quantity'     =>  $_POST["quantity"],
            );
            $_SESSION["shopping_cart"][$count] = $item_array;
        }
        else
        {
            echo '<script>alert("Product was Already in your Cart.")</script>';
        }
    }
    else
    {
        $item_array = array(
            'item_id'           =>  $_GET["id"],
            'item_name'         =>  $_POST["hidden_name"],
            'item_image'        =>  $_POST["hidden_image"],
            'item_price'        =>  $_POST["hidden_price"],
            'item_quantity'     =>  $_POST["quantity"],
        );
        $_SESSION["shopping_cart"][0] = $item_array;
    }
}

if(isset($_GET["action"]))
{
    if($_GET["action"] == "delete")
    {
        foreach($_SESSION["shopping_cart"] as $keys => $values)
        {
            if($values["item_id"] == $_GET["id"])
            {
                unset($_SESSION["shopping_cart"][$keys]);
                echo '<script>alert("Item Removed")</script>';
            }
        }
    }
}
<div class="container">
    <div class="row">
        <div class="col-sm-12 col-md-10 col-md-offset-1">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>Quantity</th>
                        <th class="text-center">Price</th>
                        <th class="text-center">Amount</th>
                        <th> </th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <?php
                    if(!empty($_SESSION["shopping_cart"]))
                    {
                        $total =0;
                        foreach($_SESSION["shopping_cart"] as $keys => $values)
                        {
                        
                    ?>
                        <td class="col-sm-8 col-md-6">
                        <div class="media">
                            <a class="thumbnail pull-left" href="#"><img class="media-object" src="admin/public/img/<?php echo $values["item_image"]; ?>" style="width: 72px; height: 72px;">Order No:<?php echo $values["item_id"]; ?></a>
                            <div class="media-body">                                
                                <h4 class="media-heading"><a href="#"><?php echo $values["item_name"]; ?></a></h4>
                            </div>
                        </div></td>
                        <td class="col-sm-1 col-md-1" style="text-align: center">
                        <input type="number" class="form-control" value="<?php echo $values["item_quantity"]; ?>">
                        </td>
                        <td class="amount text-center"><strong>₱ <?php echo $values["item_price"]; ?>.00</strong></td>
                        <td class="col-sm-1 col-md-1 text-center"><strong>₱ <?php echo number_format($values["item_quantity"] * $values["item_price"], 2);?></strong></td>
                        <td class="col-sm-1 col-md-1">
                      </td>
                    </tr>
                    <?php
                            $total = $total + ($values["item_quantity"] * $values["item_price"]);

                        }
                    ?>
                    <tr>
                        <td>   </td>
                        <td>   </td>
                        <td>   </td>
                        <td><h3>Total:</h3></td>
                        <td class="text-right"><h3><strong>₱ <?php echo number_format($total, 2); ?></strong></h3></td>
                    </tr>
                    <tr>
                        <td>   </td>
                        <td>   </td>
                        <td>   </td>
                        <td>
                        <a class="btn btn-warning" href="index.php"><span class="glyphicon glyphicon-shopping-cart"></span>Continue Shopping</a></td>
                        <td>
                        <a style="float:right;" type="submit" class="btn btn-success" href="payments.php"><span class = "glyphicon glyphicon-credit-card"></span> Complete Order</a>
                    </td>
                    </tr>
                    <?php
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

php

mysql

shopping-cart

0 Answers

Your Answer

Accepted video resources