vanilophp / cart

Cart Module for Vanilo (or any Laravel app)
https://vanilo.io
MIT License
50 stars 17 forks source link

Variable prices for products? #19

Open adrienne opened 2 years ago

adrienne commented 2 years ago

Our site allows donations of arbitrary amounts. Is there a way to make the price of a product variable?

adrienne commented 2 years ago

@fulopattila122 - help please!

adrienne commented 2 years ago

(We are using specific modules rather than the whole framework, if that makes a difference to how we have to set things up.) Can we do this with Properties? The documentation on those is a little confusing as to how to set them up with only the Cart module.

fulopattila122 commented 2 years ago

No, properties won't help here. I'm afraid right now, you can only do this by changing the price of the product on the fly. This can be even temporary, without actually saving the price of the product to the DB.

A naive example:

class CartController
{
    public function addItem(Request $request)
    {
        $product = Product::findBySku('DONATION');
        $product->price = $request->get('amount_to_donate');

        Cart::addItem($product);

        return redirect()->to('/cart');
    }
}

Nevertheless, I must admit that there should be an easier way provided by the cart module out of the box.