Use Product::getUrlAttribute to get product url.

This commit is contained in:
Edward Yang 2023-03-14 21:13:03 +08:00
parent 8ab99143b9
commit 88255f6e7b
4 changed files with 16 additions and 11 deletions

View File

@ -19,16 +19,16 @@ class ProductController extends Controller
public function index(Request $request)
{
$requestData = $request->all();
$productList = ProductRepo::list($requestData);
$products = ProductResource::collection($productList);
$requestData = $request->all();
$productList = ProductRepo::list($requestData);
$products = ProductResource::collection($productList);
$productsFormat = $products->jsonSerialize();
$data = [
'categories' => CategoryRepo::flatten(locale()),
'categories' => CategoryRepo::flatten(locale()),
'products_format' => $productsFormat,
'products' => $products,
'type' => 'products',
'products' => $products,
'type' => 'products',
];
$data = hook_filter('admin.product.index.data', $data);

View File

@ -27,12 +27,12 @@ class ProductResource extends JsonResource
'price_formatted' => currency_format($masterSku->price),
'active' => $this->active,
'position' => $this->position,
'url' => shop_route('products.show', $this->id),
'url' => $this->url,
'created_at' => time_format($this->created_at),
'deleted_at' => $this->deleted_at ? time_format($this->deleted_at) : '',
'url_edit' => admin_route('products.edit', $this->id),
];
return $data;
return hook_filter('resource.product', $data);
}
}

View File

@ -76,7 +76,10 @@ class Product extends Base
public function getUrlAttribute()
{
return shop_route('products.show', ['product' => $this]);
$url = shop_route('products.show', ['product' => $this]);
$filters = hook_filter('model.product.url', ['url' => $url, 'product' => $this]);
return $filters['url'] ?? '';
}
public function getImageAttribute()

View File

@ -37,12 +37,12 @@ class ProductSimple extends JsonResource
$images = $this->images ?? [];
}
return [
$data = [
'id' => $this->id,
'sku_id' => $masterSku->id,
'name' => $name,
'name_format' => $name,
'url' => shop_route('products.show', ['product' => $this]),
'url' => $this->url,
'price' => $masterSku->price,
'origin_price' => $masterSku->origin_price,
'price_format' => currency_format($masterSku->price),
@ -54,5 +54,7 @@ class ProductSimple extends JsonResource
return image_resize($item, 400, 400);
}, $images),
];
return hook_filter('resource.product.simple', $data);
}
}