John Davidson

php - Increase Quantity If Product don't Exists yet In Cart

0 comments
Message:


i want to be able to increase and decrease quantity item before submiting data to the cart
this is my increase and decrese methods they work fine already tested


public function decrease($id)
{
$user = Auth::user();
$panier = $user->panier;
foreach ($panier->produits as $produit) {

if ($produit->id == $id) {
$qt = $produit->pivot->quantite;
$qt--;
if ($qt == 0) {
$panier->produits()->detach($produit->id);
} else {
$panier->produits()->updateExistingPivot($produit->id, ['quantite' => $qt]);
}
}
}
return redirect()->back();
}

public function increase($id)
{
$user = Auth::user();
$panier = $user->panier;

foreach ($panier->produits as $produit) {

if ($produit->id == $id) {
$qt = $produit->pivot->quantite;
$qt++;
$panier->produits()->updateExistingPivot($produit->id, ['quantite' => $qt]);
}
}

return redirect()->back();
}

the probelem is in my blade i remove the comment from the input value it return this error Attempt to read property "quatity" on null i don't know what to do this is the third day already


<div class="quantity">
<a href="{{route('decrQtePanier',['id'=> $prod->id])}}" class="quantity-button minus">
<i class="klbth-icon-minus"></i>
</a>
<input value="{{--$prod->pivot->quatity--}}" name="cart[15945][qty]" type="text" id="quantity_61a9f089c4906" class="input-text qty text" step="1" min="1" max="11" size="4" placeholder="" inputmode="numeric" />
<a href="{{route('incrQtePanier',['id'=> $prod->id])}}" class="quantity-button plus">
<i class="klbth-icon-plus"></i>
</a>
</div>
<a style="text-decoration:none; " href="{{ url('add-to-cart/'.$prod->id) }}">
<button style="border-radius: 50px; padding:0 20px; margin-left:40px" type="submit" name="add-to-cart" value="352" class="single_add_to_cart_button button alt">
Ajouter
</button>
</a>

this is my cart module


use HasFactory;

protected $fillable = ['user_id'];

public function user()
{
return $this->belongsTo('App\Models\User');
}

public function produits()
{
return $this->belongsToMany('App\Models\Produit','produits_paniers')
->withPivot('quantite');
}

and this is my product module


use HasFactory;

protected $fillable = [
'nom', 'prix', 'prixpromo', 'detals', 'image','categorie_id','user_id'
];

public function favories()
{
return $this->belongsToMany('App\Models\Favorie');
}

public function paniers()
{
return $this->belongsToMany('App\Models\Panier');
}

public function commandes()
{
return $this->belongsToMany('App\Models\Commande');
}

public function categories()
{
return $this->belongsTo('App\Models\Categorie','categorie_id');
}

this is my routes this operation i'm using is in the detail route i'm newbe and i'm really stuck in here


Route::get('/detail/{id}', 'App\Http\Controllers\HomePageController@product_detail');
Route::get('/decreasePanier/{id}', 'App\Http\Controllers\PanierController@decrease')->middleware('auth')->name("decrQtePanier");
Route::get('/increasePanier/{id}', 'App\Http\Controllers\PanierController@increase')->middleware('auth')->name("incrQtePanier");


My Answer:
't Exist

// Check if product exists in cart
$product_id = 123;
$quantity = 1;

if(isset($_SESSION['cart'][$product_id])) {
// Product already exists in cart, increase quantity
$_SESSION['cart'][$product_id] += $quantity;
} else {
// Product doesn't exist in cart, add it with quantity
$_SESSION['cart'][$product_id] = $quantity;
}

// Display updated cart
print_r($_SESSION['cart']);
?>

Rate this post

5 of 5 based on 5371 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us