From f92a96a73cc5a61e1702541e18f3cb15adc7bd6e Mon Sep 17 00:00:00 2001 From: Adrien Leloup Date: Tue, 18 Jan 2022 18:22:22 +0100 Subject: [PATCH] Added getDefaultPriceType method --- src/HasPrices.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/HasPrices.php b/src/HasPrices.php index 44ee6a3..9d37b71 100644 --- a/src/HasPrices.php +++ b/src/HasPrices.php @@ -25,7 +25,7 @@ public function prices() */ public function price() { - return $this->morphOne(Price::class, 'priceable')->where('type', 'selling')->current(); + return $this->morphOne(Price::class, 'priceable')->where('type', $this->getDefaultPriceType())->current(); } /** @@ -73,4 +73,30 @@ public function setPrice( return $this; } + + /** + * Get the price, as a model or as a whitecube/php-prices instance + * + * @param boolean $asModel + * @return void + */ + public function getPrice(bool $asModel = false) + { + if (! $asModel) { + return $this->price; + } + + return $this->price()->first(); + } + + /** + * Get the default price type + * + * @return string + */ + protected function getDefaultPriceType(): string + { + return 'selling'; + } + }