Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix getting default price for a product #48

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Stripe

## Unreleased

- Fixed getting a default price for a Product element. ([#40](https://github.com/craftcms/stripe/pull/40))

## 1.2.0.2 - 2024-11-08

- Fixed a PHP error that could occur when saving a new user.
Expand Down
10 changes: 8 additions & 2 deletions src/elements/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,15 +596,21 @@ public function getData(): array
public function getDefaultPrice(): Price|null
{
if (!isset($this->_defaultPrice)) {
if ($this->getData()['default_price'] === null) {
$defaultPriceId = $this->getData()['default_price'];
if ($defaultPriceId === null) {
return null;
}

// depending on whether we're expanding a default_price when getting a product from Stripe, this will be a string or an array
if (is_array($defaultPriceId)) {
$defaultPriceId = $defaultPriceId['id'];
}

/** @var ElementCollection<int|string, Price> $prices */
$prices = $this->getPrices();

$price = $prices
->filter(fn(Price $price) => $price->stripeId === $this->getData()['default_price'])
->filter(fn(Price $price) => $price->stripeId === $defaultPriceId)
->first();

if (!$price) {
Expand Down
Loading