Home > Blockchain >  How to make users cannot add product to cart session from different store?
How to make users cannot add product to cart session from different store?

Time:11-24

So, i want to make if else statement where if $cart session has different sponsor_id value, users cannot add product from another sponsor_id

heres my CartController

public function addToCart(Request $request, $id)
{
    $cart = session("cart");
    $produk = Produk::detail_produk($id);

    if (isset($cart[$id])) {
        $cart[$id]["qty"]  ;
        session()->put('cart', $cart);
    } else {
        $cart[$id] = [
            "sponsor_id" => $produk->sponsor_id,
            "nama_produk" => $produk->nama_produk,
            "harga_produk" => $produk->harga_produk,
            "berat" => $produk->berat,
            "gambar" => $produk->gambar,
            "qty" => $request->qty
        ];
    }
    session(["cart" => $cart]);
    return redirect()->back()->with('success', 'Produk Berhasil dimasukkan ke keranjang !!');
}

so, users has to complete the transaction before adding product from another sponsor_id to cart

CodePudding user response:

I think this code will do it by looping through the previous product data.

public function addToCart(Request $request, $id)
{
    $cart = session("cart");
    $produk = Produk::detail_produk($id);

    if (isset($cart[$id])) {
        $cart[$id]["qty"]  ;
        session()->put('cart', $cart);
    } else {
        $num = count($cart);
        if($num > 0){
          $this_sponsor_id = $produk->sponsor_id;
          foreach($cart as $produk_id => $produk_data){
            if($produk_data->sponsor_id != $this_sponsor_id){
              return false;
            }
          }
        }
        $cart[$id] = [
            "sponsor_id" => $produk->sponsor_id,
            "nama_produk" => $produk->nama_produk,
            "harga_produk" => $produk->harga_produk,
            "berat" => $produk->berat,
            "gambar" => $produk->gambar,
            "qty" => $request->qty
        ];
    }
    session(["cart" => $cart]);
    return redirect()->back()->with('success', 'Produk Berhasil dimasukkan ke keranjang !!');
}

I was also thinking that maybe you could store the items in separate carts, grouped by sponsor id. Then you could charge for the correct ones at checkout - separately.

public function addToCart(Request $request, $id)
{
    $newcart  = session("cart");
    $produk = Produk::detail_produk($id);
    $newcart = session("newcart");
    if(!isset($newcart[$produk->sponsor_id])){
      $newcart[$produk->sponsor_id] = [];
    }
    if(isset($newcart[$produk->sponsor_id][$id])){
      $newcart[$produk->sponsor_id][$id]["qty"]  ;
      session()->put('newcart', $newcart);
    } else {
      $newcart[$produk->sponsor_id][$id] = [
          "sponsor_id" => $produk->sponsor_id,
          "nama_produk" => $produk->nama_produk,
          "harga_produk" => $produk->harga_produk,
          "berat" => $produk->berat,
          "gambar" => $produk->gambar,
          "qty" => $request->qty
      ];
    }
    session(["newcart" => $newcart]);
    return redirect()->back()->with('success', 'Produk Berhasil dimasukkan ke keranjang !!');
}

CodePudding user response:

Little change for my case. in case someone has same problem with me

public function addToCart(Request $request, $id)
{
    $cart = session("cart");
    $produk = Produk::detail_produk($id);
    $num = count(array($cart));
    if (isset($cart[$id])) {
        $cart[$id]["qty"]  ;
        session()->put("cart", $cart);
    } else {
        if ($num > 0) {
            $this_sponsor_id = $produk->sponsor_id;
            foreach ($cart as $produk_id => $produk_data) {
                if ($produk_data["sponsor_id"] != $this_sponsor_id) {
                    return redirect()->back()->with('error', 'Bersihkan Keranjang sebelum Membeli dari Booth Lain');
                }
            }
        }
        $cart[$id] = [
            "sponsor_id" => $produk->sponsor_id,
            "nama_produk" => $produk->nama_produk,
            "harga_produk" => $produk->harga_produk,
            "berat" => $produk->berat,
            "gambar" => $produk->gambar,
            "qty" => $request->qty
        ];
    }
    // dd($cart);
    session(["cart" => $cart]);
    return redirect()->back()->with('success', 'Produk Berhasil dimasukkan ke keranjang !!');
}
  • Related