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 forceDelete product #2075

Draft
wants to merge 3 commits into
base: 1.x
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,16 @@ public function setUp(): void

$record->customerGroups()->detach();

$record->variants()->each(function (Model $variant) {
$variant->values()->detach();

$variant->prices()->delete();
});

$record->urls()->delete();

$record->variants()->delete();
$record->variants()->get()->each->forceDelete();
Comment on lines -23 to +25
Copy link
Contributor Author

Choose a reason for hiding this comment

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

cleanup to use ProductVariant observer for handling relationship


$record->productOptions()->detach();

$record->associations()->delete();

$record->channels()->detach();

$record->clearMediaCollection('images');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is unnecessary as handled by Spatie's Media observer for cleanup


return $record->forceDelete();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the whole code could be simplified to just this one line if we register a Product observer for handling the relationships.

user land can register own observer as well, i think 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alecritson need your input on this, not sure is ProductObserver is purposely left out

Copy link
Collaborator

Choose a reason for hiding this comment

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

@wychoong Yeah I think ultimately having this kind of logic in the panel is a mistake, we should really be using observers, whether within the model itself or in a separate class.

Copy link
Contributor Author

@wychoong wychoong Jan 27, 2025

Choose a reason for hiding this comment

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

@alecritson Alright, got it.
Can you review the part for media observer? Will prepare a separate pr for the product observer

});
});
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/Observers/MediaObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ protected function ensureOnlyOnePrimary(Media $media, bool $isDelete = false): v
return;
}

$owner = $media->model()->sole();
$owner = $media->model()->first();

if (blank($owner)) {
return;
}

if (! $isDelete && $media->getCustomProperty('primary')) {
$owner->getMedia($media->collection_name)
Expand Down
Loading