mada72's avatar

update stock

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!

0 likes
15 replies
mada72's avatar

protected $table = 'products';
    protected $fillable=['photo_id','category_id','name','title','description','price','quantity'];

    public function category(){
        return $this->belongsTo('App\Category');
    }
    public function photo(){
        return $this->belongsTo('App\Photo');
    }

    /*public function stock(){
        return $this->hasMany('App\Stock');
    }*/
    public function order(){
        return $this->belongsToMany('App\Order', 'order_product', 'product_id', 'order_id');
    }
mstrauss's avatar

Maybe something like the below might work. I think your issue might be that you are performing a calculation within the parameters of the Update method.

    public function decreaseQuantities()
    {
        foreach (Cart::content() as $item) {
            $product = Product::find($item->id);
        $newQuantity = $product->quantity - $item->qty; 
            $product->quantity => $newQuantity; 
            $product->save(); 
        }
    }

Or

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

cviv's avatar

my suggestion is posted in the 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) //this can be if(Request::get('increment')) == 1)  < is it possible to submit a POST request without a product id? If so, please check your front-end logic> 
 {
            $item = Cart::search(
                function($key, $value) {
                    return $key->id == Request::get('product_id');
                })->first();
            Cart::update($item->rowId, $item->qty += 1); //you can also have Cart::update($item->rowId, 'increment'); and do your logic in update method
        }
        if  (Request::get('decrease')) == 1) //check first if $item->qty >0. you might disable your button if quantity is 0
{
            $item = Cart::search(function($key, $value) { return $key->id == Request::get('product_id'); })->first();
            Cart::update($item->rowId, $item->qty -= 1); //

        }
mada72's avatar

my function work but if I have $product->quantity =5 and $item->qty=7 my stock will be negative -2

I need to return a error in view

cviv's avatar

there is an error in your implementation, how do you check out your product? one at a time or by bulk quantity?

mada72's avatar

I tried something like this...

if($product->quantity >= $item->qty)
             $product->update(['quantity' => $product->quantity - $item->qty]);

        }return redirect()->route('shopping_cart')
        ->with('message', 'Add to cart successful');
mada72's avatar

ignore 'Add to cart successful'

cviv's avatar

my bad. sorry, you must implement it here

if($product->quantity - $item->qty<0);
do you logic here
else
$product->update(['quantity' => $product->quantity - $item->qty]);
cviv's avatar

you cannot return a redirect because you are in a loop. I guess you need to put your message in an array like: Product A added successfully, Product B failed, insufficent quantity

mada72's avatar

I miss that "do you logic here" my neurons don't work :)

mada72's avatar

In my view page I must "say" to user: " We don't have enough items in stock' And that must be : do something

cviv's avatar
cviv
Best Answer
Level 3
$message= array();//place outside the loop
if($product->quantity - $item->qty<0);
$message[] = 'Not enough Product' . $item->id;
else
$product->update(['quantity' => $product->quantity - $item->qty]);

then return $message to your view

Please or to participate in this conversation.