Files
ladill-woo-manager/app/Models/WooProduct.php
T
isaaccladandCursor 085645fd0f
Deploy Ladill Woo Manager / deploy (push) Successful in 24s
Add WooCommerce-style featured image and product gallery flow.
Sync full image sets through the plugin, with upload/URL support in the product editor and thumbnails in the list.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-07 00:27:11 +00:00

118 lines
2.9 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class WooProduct extends Model
{
public const STATUS_PUBLISH = 'publish';
public const STATUS_DRAFT = 'draft';
public const STATUS_PENDING = 'pending';
public const STATUS_PRIVATE = 'private';
public const STATUSES = [
self::STATUS_PUBLISH => 'Published',
self::STATUS_DRAFT => 'Draft',
self::STATUS_PENDING => 'Pending review',
self::STATUS_PRIVATE => 'Private',
];
protected $fillable = [
'woo_store_id',
'external_id',
'name',
'slug',
'sku',
'status',
'type',
'regular_price_minor',
'sale_price_minor',
'stock_quantity',
'manage_stock',
'category_external_ids',
'images',
'short_description',
'description',
'wc_updated_at',
'metadata',
];
protected $casts = [
'manage_stock' => 'boolean',
'category_external_ids' => 'array',
'images' => 'array',
'wc_updated_at' => 'datetime',
'metadata' => 'array',
];
public function store(): BelongsTo
{
return $this->belongsTo(WooStore::class, 'woo_store_id');
}
public function priceMinor(): ?int
{
return $this->sale_price_minor ?? $this->regular_price_minor;
}
public function priceFormatted(): string
{
$minor = $this->priceMinor();
if ($minor === null) {
return '—';
}
$currency = strtoupper((string) ($this->store?->metadata['currency'] ?? 'GHS'));
return $currency.' '.number_format($minor / 100, 2);
}
public function statusLabel(): string
{
return self::STATUSES[$this->status] ?? ucfirst((string) $this->status);
}
/** @return array{id?: int, src?: string, alt?: string, position?: int}|null */
public function featuredImage(): ?array
{
$images = (array) $this->images;
return $images[0] ?? null;
}
/** @return list<array{id?: int, src?: string, alt?: string, position?: int}> */
public function galleryImages(): array
{
$images = array_values((array) $this->images);
return array_slice($images, 1);
}
public function thumbnailUrl(): ?string
{
$featured = $this->featuredImage();
return is_array($featured) && ! empty($featured['src'])
? (string) $featured['src']
: null;
}
/** @return list<string> */
public function categoryNames(): array
{
$ids = array_filter((array) $this->category_external_ids);
if ($ids === []) {
return [];
}
return WooCategory::query()
->where('woo_store_id', $this->woo_store_id)
->whereIn('external_id', $ids)
->orderBy('name')
->pluck('name')
->all();
}
}