nscott's avatar

Cart items not getting added back into stock when fully removed from cart

I have an e-commerce site that I developed with Laravel. I am trying to get the stock quantity of a product to update in the database when a user clicks on the delete item button. This is different than if a user clicks the decrease item button. I have not yet posted to these forums so I am not sure what protocol is or information is needed. Below is the code for removing all quantities of one item from the cart.

 public function deleteItemFromCart(Request $request, $id) {

        $cart = Session::get('cart');

        $deletedItems = Session::get('cart', $cart->items[$id]['quantity']);

        if(array_key_exists($id, $cart->items)){
            unset($cart->items[$id]);
        }

        $prevCart = $request->session()->get("cart");
        $updatedCart = new Cart($prevCart);
        $updatedCart->updatePriceAndQunatity();

        $request->session()->put("cart", $updatedCart);

        $product = Product::findOrFail($id);

        $upateStockQuantity = $product->stock_quantity + $deletedItems;

        DB::table('products')->where('id', $id)->update(array('stock_quantity' => $upateStockQuantity));

        return redirect()->route('shopping cart'); 
    }
0 likes
14 replies
jlrdw's avatar

If you are decreasing, it would be the opposite, to add stock back.

I suggest you find a laravel cart on github, and view the code to get ideas of how it's done.

nscott's avatar

This is to add the item back into the database. So say a user has 2 of one item in the cart, they have two options to remove that item. One is they decrease the item by one. The other is to click remove and it clears that item from the cart all at once. When the item is decreased by one it puts it back into stock one at a time and that function works fine, but I cannot figure out how to get the function working to put them all back at once.

martinbean's avatar

I’m not sure what the complication is? If a user has 2× of an item in their cart, and they remove that item from their cart, then increase the stock of that item by 2.

nscott's avatar

Yes, that is correct. So if they have 2 of item A and 3 of item B, they click on the delete from cart button for item A and should remove all of those items from the cart at once and update the product quantity in the database by 2.

jlrdw's avatar

Did you look at some laravel carts on GitHub to get some ideas.

nscott's avatar

I was just starting to look one over. I also updated my question with my code example.

martinbean's avatar
Level 80

Yes, that is correct. So if they have 2 of item A and 3 of item B, they click on the delete from cart button for item A and should remove all of those items from the cart at once and update the product quantity in the database by 2.

@nscott Right. So do that? You’ve explained what you want to happen in plain English, so now write the code to do that.

nscott's avatar

That is what I have been trying to do but cannot seem to make it work. It either deletes all the items from the cart and does not update the stock quantity or I can manage to produce an error: Object of class App\Cart could not be converted to number. I know that I am missing something super simple.

nscott's avatar

I finally figured this out. It took me a while to think it through, but essentially I was trying to update the quantities in the wrong spot.

1 like
jlrdw's avatar

This stuff would be much easier using Ajax , and adjust stock after order is placed.

A simple for each over the order and updating stock accordingly.

nscott's avatar

The way I had originally coded this was that inventory would only be updated once an order was fully completed, meaning it was paid for. I think that is the way most companies do this. However, the client does not like this as they are worried that someone will possibly order something that is already committed in another order. So I changed it around to commit the items once added. This is causing all kinds of coding headaches for me though.

abrada's avatar

Add the product to the basket - the product add to the array in the session.

Removed the product from the basket - the product delete from the array in the session.

From the warehouse, the product are removed - when the order created and paid.

In session:

basket =>[
  product_id=>count,
  product_id=>count,
  product_id=>count,
]
jlrdw's avatar

that is already committed in another order.

Normally you will have a pending or hold type of a status also.

I would suggest again to go ahead and download and install an up-to-date laravel cart and play with it and see how they handle such things.

Another thing you have to take into account is backorder products, so that's quite a bit to all the different statuses.

If someone orders five items, but you can only ship four, you need to track when the item is available to fulfill the order. Most companies will ship the available items, and turn around and ship the others later if that's what the customer wants.

nscott's avatar

I did not even think of those other statuses, but now that you mention them they make a lot of sense to have. Most of the items that they carry are one-off items, so they will not be able to back order the items. That was part of the reason that they wanted to change it to this method.

I will take a look at a cart for laravel and try to see how they might handle this.

Please or to participate in this conversation.