Search code examples
laravellistener

How can i get the buyers to still be able to purchase the product even if its unavailable?


I'm trying to create waitlist to buy product, but change the status of the transaction from 'Confirmed' to 'Waitlist'.

For that I have added a variable 'status' and a function in Transaction Model as follows:

const CONFIRMED_TRANSACTION = 'confirmed';
const WAITLIST_TRANSACTION = 'waitlist'; //waitlist product

public function isConfirmed() {
        return $this->status == Transaction::CONFIRMED_TRANSACTION;
    }

Default value of Transaction->status is 'CONFIRMED_TRANSACTION'. Now every time the Product->status changes to 'unavailable' i would like to change the value of the Transaction->status to 'WAITLIST_TRANSACTION' when the transaction is created.

I am trying to achieve it using Event Listeners as:

Transaction::created(function($transaction) {
            if(!$product->isAvailable()) {
                $transaction->status = Transaction::WAITLIST_TRANSACTION;
                $transaction->save();
            }
        });

But this gives me error :

**ErrorException: Undefined variable: product in file /home/vagrant/restfulapi7/app/Providers/AppServiceProvider.php on line 29**

How can I achieve the same in a better way?


Solution

  • This solves the issue:

    if(!$transaction->product->isAvailable())