Code Generator Basket - Add Ketchup to Your Pizza

I am creating a pizzeria app in CI using cart. For some products (pizza, salad ...) it is possible to add ingredients. This is what I have:

Add_to _cart buton

<button class="but_add_cart"
        data-qty="1" 
        data-productid="<?php echo $food_item->item_id; ?>" 
        data-productname="<?php echo $food_item->item_name; ?>" 
        data-productprice="<?php echo $food_item->item_price; ?>"
        data-adds="<?php echo $food_item->item_price; ?>

      

With jquery I am calling ajax

$('.but_add_cart').click(function(){

        allVals = []

        $('#chk :checked').each(function() {
           allVals.push($(this).val());
         });
        // here I'm getting all ingredients from checkboxes and push to array

        var product_id = $(this).data("productid");
        var description = $(this).data("description");
        var product_name = $(this).data("productname");
        var product_price = $(this).data("productprice");
        var product_qty = $(this).data("qty");


        if(product_id != '' && product_id > 0) 
            {
                $.ajax({
                url:"<?php echo base_url(); ?>menu/add",
                type:"POST",
                async: true,
                data:{product_id:product_id, description:description, product_name:product_name, product_qty:product_qty, product_price:product_price, allVals:allVals},
                success:function(data)
                    {
                        $('#cart_details').html(data);

                    }
                });

            } 
        else
            {
                alert("Please Enter quantity");
            }
});

      

and add a function:

function add()
    {
        $data = array(
            "id"  => $_POST["product_id"],
            "qty"  => $_POST["product_qty"],
            "price"  => $_POST["product_price"],
            "name"  => $_POST["product_name"],
            "description"  => $_POST["description"],
            "allVals"  => $_POST["allVals"]
        );

            print_r($data);

            //$this->cart->insert($data);
            //echo $this->view();

    }

      

from print_r I have this array

Array
(
    [id] => 1
    [qty] => 1
    [price] => 19.90
    [name] => Pepperoni Pizza
    [description] => Extra-virgin olive oil, garlic, mozzarella cheese
    [allVals] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 4
            [3] => 6
        )
)

      

Now, in the basket, I want to group the Pepperoni pizza with ingredients (2, 3, 4, 6) as one item. If I add another Pepperoni pizza, but with different ingredients, it should be a separate item in the basket. I tried adding a random number to the rowid, but that is giving me some error.

Also, every ingredient is coastal, so I have to add these prices to the intermediate price for each item in the basket.

I know the problem is quite complex, but I would like to know if my approach is good or is there a better way to do this?

+3


source to share


1 answer


Is the answer here not using ingredients as a key?

asort($_POST['allVals']);
$key = implode('',$_POST['allVals']);

      



Combine the product ID and $ key and see if this product exists in the cart. If this happens, update the quantity. Are your ingredients your unique key?

0


source







All Articles