Files
isaacclad e709de6593
Deploy Ladill Woo Manager / deploy (push) Successful in 35s
Use WooCommerce store currency for product and order money.
Sync currency from the plugin, format product prices and totals with it, and show the store currency on product price fields instead of hard-coding GHS.
2026-07-24 15:38:07 +00:00

133 lines
3.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 '—';
}
if ($this->relationLoaded('store') || $this->store) {
return $this->store->formatMoney($minor);
}
return number_format($minor / 100, 2);
}
public function regularPriceFormatted(): string
{
if ($this->regular_price_minor === null) {
return '—';
}
if ($this->relationLoaded('store') || $this->store) {
return $this->store->formatMoney((int) $this->regular_price_minor);
}
return number_format(((int) $this->regular_price_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();
}
}