Files
ladill-woo-manager/app/Models/WooProduct.php
T
isaaccladandCursor e4b6c2c1ba
Deploy Ladill Woo Manager / deploy (push) Successful in 55s
Add WooCommerce product and category management.
Sync catalog via plugin webhooks and REST proxy, with list/create/edit UI and updated logo from monolith assets.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-06 23:40:14 +00:00

93 lines
2.3 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 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();
}
}