mada72
233
15
Laravel

update stock

Posted 3 weeks ago by mada72

Hello,

if( $product->quantity - $item->qty < 0)....return..???

I need a condition...can you help me please?

my code

public function checkout(){



        if(Request::isMethod('post')){
            $product_id = Request::get('product_id');
            $product = Product::find($product_id);
            Cart::add(array('id' => $product_id, 'name' => $product->name, 'qty' => 1, 'price' => $product->price));
        }
        $cart = Cart::content();
        //increment
        if (Request::get('product_id') && (Request::get('increment')) == 1) {
            $item = Cart::search(
                function($key, $value) {
                    return $key->id == Request::get('product_id');
                })->first();
            Cart::update($item->rowId, $item->qty + 1);
        }
        if (Request::get('product_id') && (Request::get('decrease')) == 1) {
            $item = Cart::search(function($key, $value) { return $key->id == Request::get('product_id'); })->first();
            Cart::update($item->rowId, $item->qty - 1);

        }

        $this->decreaseQuantities();


        return view('checkout', compact ('cart', 'token','cities', 'cit','countries', 'addresses'));
    }

    public function decreaseQuantities()
    {
        foreach (Cart::content() as $item) {
            $product = Product::find($item->id);
            $product->update(['quantity' => $product->quantity - $item->qty]);

        }
    }

Thx!

Please sign in or create an account to participate in this conversation.