Add WooCommerce product and category management.
Deploy Ladill Woo Manager / deploy (push) Successful in 55s
Deploy Ladill Woo Manager / deploy (push) Successful in 55s
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>
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user