如何使用jquery从其他表中获取价格

时间:2016-01-29 08:23:27

标签: javascript jquery json laravel-4

如何从表格pricee获取size_nameproduct_options

product_options表

-id
-product_id
-pricee
-size_name

控制器:

function addToCart() {
  $product_id = Input::get('product_id');
  $quantity = 1;
  $product = product::find($product_id);
  if ($product) {
    $item = new Cart($product_id, $quantity);
    Session::push('cart', serialize($item));
    return Response::json(1);
  }
  return Response::json(0);
}

function myCart() {
  $cart_items = [];
  $session_items;
  if (Session::has('cart') && !empty(Session::get('cart'))) {
    $session_items = Session::get('cart');
    foreach($session_items as $key => $item) {
      $unserialized_item = unserialize($item);
      $product = product::find($unserialized_item - > productId);
      $cart_item = new StdClass;
      $cart_item - > cart_id = $key;
      $cart_item - > productId = $product - > id;
      $cart_item - > seller_id = $product - > seller_id;
      $cart_item - > category_id = $product - > category_id;
      $cart_item - > product_name = $product - > product_name;
      $cart_item - > description = $product - > description;
      $cart_item - > price = $product - > price;
      $cart_item - > image = $product - > image;
      $cart_item - > quantity = $unserialized_item - > quantity;
      $cart_item - > total = ($cart_item - > price * $cart_item - > quantity);
      $counter = 0;
      $found = false;
      $index = null;
      foreach($cart_items as $it) {
        if ($it - > productId == $product - > id) {
          $index = $counter;
          $found = true;
        }
        $counter++;
      }
      if ($found) {
        $cart_items[$index] - > quantity = $cart_items[$index] - > quantity + $cart_item - > quantity;
        $cart_items[$index] - > total = ($cart_items[$index] - > quantity * $cart_item - > price);
      } else {
        $cart_items[] = $cart_item;
      }
    }
    if (Request::ajax())
    return $cart_items;
    else
      return View::make('cart', ['cart_items' => $cart_items]);
  }
  if (Request::ajax())
  return $cart_items;
  else
    return View::make('cart', ['cart_items' => $cart_items]);
}
}

主页:

cartlist();

function cartList() {
  $.get("{{ URL::route('myCart') }}").done(function(data) {
    $(".dropdown-cartlist").html("");
    if (data.length == 0) {
      $(".dropdown-cartlist").html("<li><span class='item'><h4>No items on the cart.</h4></span></li>");
    } else {
      for (var i = 0; i < data.length; i++) {
        var cart_items = "<li>" +
            "<span class='item'>" +
            "<span class='item-left'>" +
            "<img width='50px' height='50px' src='{{ URL::asset('assets/images/uploads/') }}/" + data[i].image + "' alt='' />" +
            "<span class='item-info'>" +
            "<span>" + data[i].product_name + " x " + data[i].quantity + "($" + data[i].pricee + ")</span>" +
            "<span>$" + data[i].total + "</span>" +
            "</span>" +
            "</span>" +
            "<span class='item-right'>" +
            "<button data-id='" + data[i].cart_id + "' class='removeProduct btn btn-xs btn-danger pull-right'>x</button>" +
            "</span>" +
            "</span>" +
            "</li>";
        $(".dropdown-cartlist").append(cart_items);
      };
    }
  });
}

0 个答案:

没有答案