Chuyển tới nội dung chính

Product (Sản phẩm)

Class: \Ecommerce\Models\Product
Alias: Product (đã đăng ký toàn cục)
Table: products
Namespace: Ecommerce\Models


Cấu trúc Cột (Columns)

Tên CộtKiểuMặc địnhMô tả
titlestringTên sản phẩm
attribute_labelstringLabel biến thể đã chọn (tổng hợp)
slugstringĐường dẫn URL
codestringMã SKU (tự sinh: SP000012)
contentwysiwygNội dung mô tả dài
excerptwysiwygMô tả ngắn
seo_titlestringTiêu đề SEO
seo_descriptionstringMô tả SEO
seo_keywordsstringTừ khoá SEO
priceprice0Giá gốc (VND)
price_saleprice0Giá khuyến mãi (0 = không KM)
statusstringpublicTrạng thái (public/draft/private)
status1int0Collection: Yêu thích (1=có)
status2int0Collection: Bán chạy (1=có)
status3int0Collection: Hot (1=có)
imageimageẢnh đại diện
publicint1Hiển thị công khai (1/0)
trashint0Đã xóa mềm (1/0)
orderint0Thứ tự sắp xếp
parent_idint0ID sản phẩm cha (cho biến thể)
brand_idint0ID thương hiệu
weightint0Cân nặng (gram)
longint0Chiều dài (mm)
widthint0Chiều rộng (mm)
heightint0Chiều cao (mm)
hasVariationint0Có biến thể không (1=có)
typestringproductLoại: product hoặc variations

Lưu ý: Mỗi sản phẩm còn có thể có metadata mở rộng trong bảng products_metadata (truy cập qua Product::getMeta($id, $key, true))


Truy Vấn Cơ Bản

// Tất cả sản phẩm đang hiển thị (public=1 được áp tự động trên Frontend)
$products = Product::all();

// Tìm theo ID
$product = Product::find(12);

// Tìm theo slug
$product = Product::where('slug', 'ao-phong-nam')->first();

// Sản phẩm trong danh mục (bao gồm danh mục con)
$products = Product::whereByCategory($categoryId)->get();
$products = Product::whereByCategory($categoryObject)->get();

// Sản phẩm bán chạy
$products = Product::where('status2', 1)->limit(8)->get();

// Sản phẩm có biến thể (loại trừ record variations)
$products = Product::widthVariation()->get();
// tương đương: whereIn('type', ['product', 'variations'])

Query Scopes (Tùy chỉnh Truy vấn)

scopeWidthVariation()

Lọc chỉ lấy sản phẩm thuộc type productvariations (loại trừ các record là biến thể con).

Product::widthVariation()->get();

scopeOnlyVariation()

Chỉ lấy các bản ghi có type là variations.

Product::onlyVariation()->where('parent_id', 10)->get();

scopeWhereByCategory($idOrObject)

Lọc sản phẩm theo danh mục, tự động bao gồm danh mục con (nested set).

// Theo ID
Product::whereByCategory(5)->get();

// Theo Object danh mục
$category = ProductCategory::find(5);
Product::whereByCategory($category)->get();

// Theo mảng nhiều danh mục
Product::whereByCategory([5, 7, 9])->get();

scopeRelated($idOrObject) – Sản phẩm liên quan

Lấy sản phẩm cùng danh mục, loại trừ bản thân.

$related = Product::related($product)->limit(6)->get();

Sự kiện Model (Model Events)

EventTriggerHành động mặc định
retrievedSau khi lấy từ DBGọi filter products_model_retrieved
savingTrước khi lưuTự điền seo_title, seo_description nếu trống
saved (add)Sau khi thêm mớiTự sinh mã code (SP000012), lưu categories
saved (update)Sau khi cập nhậtSync categories, xóa cache
trashedSau khi xóa mềmXóa cache, remove menu items
deletedSau khi xóa vĩnh viễnXóa variations, attributes, gallery, categories, menu

Hooks Liên Quan đến Model

Filter products_model_retrieved

Được gọi mỗi khi một Product được lấy từ database. Dùng để thêm data tùy chỉnh:

add_filter('products_model_retrieved', function(Product $product) {
// Thêm field tính toán
$product->discount_percent = 0;
if($product->price > 0 && $product->price_sale > 0) {
$product->discount_percent = round(
(($product->price - $product->price_sale) / $product->price) * 100
);
}
return $product;
});

Action delete_product_success

Được gọi sau khi xóa vĩnh viễn một sản phẩm:

add_action('delete_product_success', function($productId) {
// Dọn dẹp dữ liệu liên quan trong plugin của bạn
DB::table('your_plugin_product_data')->where('product_id', $productId)->delete();
});

Làm việc với Metadata

Sicommerce sử dụng bảng products_metadata để lưu thêm dữ liệu cho sản phẩm:

// Lấy một meta value (single = true trả về giá trị trực tiếp)
$defaultVariationId = Product::getMeta($product->id, 'default', true);

// Lấy tất cả meta của sản phẩm
$allMeta = Product::getMeta($product->id, '', false);

// Lưu meta
Product::updateMeta($product->id, 'custom_field', 'value');

Xử lý Biến Thể (Variations)

Khi sản phẩm có hasVariation = 1, các biến thể được lưu trong cùng bảng products với type = 'variations'parent_id = {product_id}. Dùng Model Variation để truy vấn:

use Ecommerce\Models\Variation;

// Lấy tất cả biến thể của sản phẩm
$variations = Variation::where('parent_id', $product->id)->get();

// Mỗi variation có đầy đủ price, price_sale, weight, code...
foreach ($variations as $variation) {
echo $variation->price;
echo $variation->code;
echo $variation->attribute_label; // VD: "Đỏ - XL"
}