Skip to content
Open
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: 2 additions & 2 deletions src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ protected function url(): Attribute
// or by any tier pricing.
protected function price(): Attribute
{
return Attribute::get(function () {
return Attribute::get(function ($value) {
$customerGroupId = auth('magento-customer')
->user()
?->group_id ?: 0;
Expand All @@ -295,7 +295,7 @@ protected function price(): Attribute
->firstWhere('customer_group_id', $customerGroupId);

if ($price === null) {
return $this->getCustomAttribute('price')?->value;
return $value;
}

return $price->price ?: $price->min_price;
Expand Down
5 changes: 5 additions & 0 deletions src/Models/Traits/HasCustomAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,9 @@ protected function throwMissingAttributeExceptionIfApplicable($key)
{
return $this->getCustomAttribute($key);
}

protected function getAttributeFromArray($key)
{
return parent::getAttributeFromArray($key) ?? $this->getCustomAttribute($key);
}
Comment on lines +207 to +210
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to patch the internal function for $value to get the correct value?
What about calling $this->getAttributeFromArray('price') ?? $this->getCustomAttribute('price') manually?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you definitely could do that manually each time. It would turn the functions for images from

protected function image(): Attribute
{
    return Attribute::get($this->getImageFrom(...));
}

into something like:

protected function image(): Attribute
{
    return Attribute::get(function() {
        $value = $this->getCustomAttribute('image')?->value;
        $this->getImageFrom($value);
    });
}

I'm definitely not a fan of that.

You can see how I've also removed the need for this from the price attribute in this PR. Personally I don't think it should be necessary to call such an internal function every time, especially because there's no guarantee that it'll stay the same in the future.

}
Loading