Skip to content
Advertisement

Add to cart multiple tickets of single competitions in PHP Laravel

I have a website prospectcompetitions.co.uk. I want to add multiple tickets of single competitions into cart. Every single competition consist of multilple tickets. if a user select 3 tickets then add them to cart so that user can add multiple tickets of single competitions and multiple tickets of multiple competitions please have a look prospectcompetitions.co.uk

public function add_to_cart(Request $req)
{
    $quantity = '0';
    $competition_id = $req->get('competitions_id');
    $action = $req->get('action');
    if($action == "add")
    {
        $p_name = $req->get('p_name');
    $product_id = $req->get('product_id');
    $product_name = $req->get('product_name');
    $product_price = $req->get('product_price');

    $cart = session()->get('cart');
     $old_session = $req->session()->get('cart');
    if(!$cart) {
    for($count = 0; $count < count($product_id); $count++)
    {
        

        $cart = [
            $count => [
                "id" => $product_id[$count],
                "quantity" => 1,
                "price" => $product_price[$count],
                "name" => $product_name[$count],
                'p_name'=>$p_name,
                'count'=>$count,
                'competition_id'=>$competition_id
            ]
        ];

        session()->put('cart', $cart);
        $old_session =  session()->put('cart', $cart);
        
        }
    }


    for($count = 0; $count < count($product_id); $count++)
    { 
        
    $cart[$count] = [
        "id" => $product_id[$count],
                "quantity" => 1,
                "price" => $product_price[$count],
                "name" => $product_name[$count],
                'p_name'=>$p_name,
                'count'=>$count,
                 'competition_id'=>$competition_id

    ];
    
    //array_merge($item,$oldCart['cart']);
    
}

    session()->put('cart', $cart);  

}
}

Advertisement

Answer

Basically We Used 2D Array to solve this issue.

public function add_to_cart(Request $req)
{
  $quantity = '0';
  $competition_id = $req->get('competitions_id');
  $action = $req->get('action');
  if($action == "add")
  {
    $p_name = $req->get('p_name');
    $product_id = $req->get('product_id');
    $product_name = $req->get('product_name');
    $product_price = $req->get('product_price');

    $cart = session()->get('cart');
    
    if(!$cart) {
        $cart = [];
      for($count = 0; $count < count($product_id); $count++)
      {
    

        $cart[$product_id[$count]] = [
            // $count => [
                "id" => $product_id[$count],
                "quantity" => 1,
                "price" => $product_price[$count],
                "name" => $product_name[$count],
                'p_name'=>$p_name,
                'count'=>$count,
                'competition_id'=>$competition_id
            // ]
        ];

        session()->put('cart', $cart);
       
        
      }
    }else{

     for($count = 0 ; $count <   count($product_id); $count++)
      { 
    
        $cart[$product_id[$count]] = [
            "id" => $product_id[$count],
                    "quantity" => 1,
                    "price" => $product_price[$count],
                    "name" => $product_name[$count],
                    'p_name'=>$p_name,
                    'count'=>$count,
                     'competition_id'=>$competition_id

        ];
      
      }
    }
        
   
    
    session()->put('cart', $cart);  


  }
  }
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement