laravel中的ajax无法使用购物车功能

时间:2019-06-10 17:03:59

标签: ajax laravel cart

我在laravel上用ajax编写了添加到购物车功能。在我的代码中,我想将值获取到会话变量中,并希望传递另一个函数会话值。

public function getCart(Request $request)
{
    $total_price = 0;
    $total_item = 0;
    //$shop = $request->shopping_cart;
    //Session::set('shopping_cart', $shopping_cart);
    $cart = session()->get('cart');
    session()->put('cart', $cart);

    $output = '
        <div class="table-responsive border-top" id="order_table">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Product</th>
                    <th>price</th>
                    <th>Quantity</th>
                    <th>Total</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>';
    //dd($shopping_cart);
    if(!empty($cart))
    {
        foreach($cart as $keys => $values){
            $output .= '
            <tr>
                <td>'.$values["product_name"].'</td>
                <td><span>Rs.'.$values["product_price"].'</span></td>
                <td>'.$values["product_quantity"].'</td>
                <td><span>Rs.'.number_format($values["product_quantity"] * $values["product_price"], 2).'</span></td>
                <td><button class="btn btn-danger btn-sm text-white" data-toggle="tooltip" data-original-title="Delete" id="'.$values["product_id"].'"><i class="fa fa-trash-o"></i></button></td>
            </tr>
            ';
        }
    }
    else{
        $output .= '
            <tr>
                <td colspan="5" align="center">
                    Your cart is empty
                </td>    
            </tr>
        ';
    }        

    $output .= '
        </tbody>
            </table>
        </div>
        ';

    echo $output;    
}

这是我在表函数中显示的购物车项目。这是我的动作,用于添加购物车。

public function cartAdd()
{
    if(request()->action){

        $cart = session()->get('cart');

        if(request()->action == "add"){
            $product_id = request()->product_id;
            $product_name = request()->product_name;
            $product_price = request()->product_price;
            //$shopping_cart = $request->shopping_cart;

            for($count = 0; $count > count($product_id); $count++){

                if(in_array($product_id[$count], $cart_product_id))
                {
                     $cart[$product_id[$count]]["product_quantity"]++; 
                     session()->put('cart', $cart);

                }
                else{
                    $item_array = array(
                        'product_id' => $product_id[$count],
                        'product_name' => $product_name[$count],
                        'product_price' => $product_price[$count],
                        'product_quantity' => 1
                    );

                    $cart[$product_id[$count]] = $item_array;
                    session()->put('cart', $item_array);

                }

                $cart_product_id = array_keys($cart);
                session()->put('cart', $cart_product_id);

            }
        }
    }
}

,但是此getCart函数会话变量始终为null。这是我的剧本

<script>
$(document).ready(function(){
load_product();
load_cart_data();
function load_product(){
$.ajax({
 url: "{{route('item.list')}}",
 method: "POST",
 data: {_token: '{{ csrf_token() }}'},
 success: function(data){

     $('#display_item').html(data);
   }
    })
}
function load_cart_data(){
  $.ajax({
    url: "{{route('item.cart')}}",
    method: "POST",
    data: {_token: '{{ csrf_token() }}'},
    success: function(data){                             
          $('#shopping_cart').html(data);
    }
    });
  }

$('#add_to_cart').click(function(){
   var product_id = [];
   var product_name = [];
   var product_price = [];
   var action = "add";
   $('.select_product').each(function(){
if($(this).prop('checked') == true)
 {                           
       product_id.push($(this).data('product_id'));                          
       product_name.push($(this).data('product_name'));                          
       product_price.push($(this).data('product_price'));
}
   });
if(product_id.length > 0)
{
   $.ajax({
    url: "{{route('cart.add')}}",
    method: "POST",
    data: {_token: '{{ csrf_token() }}', product_id: product_id, 
            product_name: product_name, product_price: product_price, 
            action: action},
    success: function(data){             
         $('#select_product').each(function(){           
         if($(this).prop('checked') == true)
    {                            
         $(this).attr('checked', false);
    }
      });                        
         load_cart_data();
     alert("Item has been added into cart");
    }
      });
}
else
{
    alert("Select at least one item");
}
    });
      });           
</script>

如何解决此问题并在购物车表格中显示商品?

0 个答案:

没有答案