Laravel 11 E-Commerce - Move Product From Wishlist to Cart

Welcome back to the Laravel E-Commerce Tutorial. In this video, we will learn about moving products from a wishlist to a cart.

Step 1: Create a Function in the Wishlist Controller

First, let's create a function in the Wishlist Controller:


public function move_to_cart($rowId)
{
$item = Cart::instance('wishlist')->get($rowId);
Cart::instance('wishlist')->remove($rowId);
Cart::instance('cart')->add($item->id,$item->name,1,$item->price)->associate('App\Models\Product');
return redirect()->back();
}

Step 2: Create a Route for the Function

Next, create a route for this function:


Route::post('/wishlist/move-to-cart/{rowId}',[WishlistController::class,'move_to_cart'])->name('wishlist.move.to.cart');

Step 3: Add a Form to the Wishlist View

Now, go to the wishlist.blade.php file and add a form before the "Move to Cart" button:


<td>
<div class="del-action">
<form method="POST" action="{{route('wishlist.move.to.cart',['rowId'=>$wishlistItem->rowId])}}">
@csrf
<button type="submit" class="remove-cart btn btn-sm btn-warning">Move to Cart</button>
</form>
</div>
</td>

Step 4: Add a Form to the Wishlist View for Moving to Cart

Now, go to the wishlist.blade.php file and add a form:

Now that we've completed the setup, let's check it out! Refresh the page and move a product to the cart by clicking on the "Move to Cart" button. You should see that the product has been removed from the wishlist and added to the cart.

Let's check the cart to confirm:

Click on the cart link and you should see the product that we just moved from the wishlist.

In this way, you can move products from a wishlist to a cart.

That's all about moving products from a wishlist to a cart.