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

ProductCategory & Các Model Phụ Trợ


ProductCategory (Danh mục sản phẩm)

Class: \Ecommerce\Models\ProductCategory
Alias: ProductCategory
Table: products_categories

Cột chính

Tên CộtKiểuMô tả
namestringTên danh mục
slugstringURL
contentwysiwygNội dung mô tả
excerptwysiwygMô tả ngắn
seo_titlestringSEO title
seo_descriptionstringSEO description
imageimageẢnh danh mục
statusintTrạng thái (0/1)
parent_idintID cha (nested set)
levelintCấp độ trong cây
lft / rgtintLeft/Right cho nested set
publicintHiển thị công khai
orderintThứ tự sắp xếp

Truy Vấn

// Lấy tất cả danh mục theo dạng cây (nested tree)
$tree = ProductCategory::tree()->get();

// Lấy danh mục đa cấp (multilevel)
$tree = ProductCategory::multilevel()->get();

// Select options (dạng array cho dropdown)
$options = ProductCategory::options()->get();
// [['id' => 1, 'name' => '── Áo', 'level' => 0], ['id' => 2, 'name' => ' ── Áo nam', 'level' => 1], ...]

// Danh mục của một sản phẩm
$categories = ProductCategory::byProduct($productId)->get();

// Lấy tất cả ID con (bao gồm chính nó)
$ids = ProductCategory::children(['andParent' => true, 'id' => 5]);
// Trả về: [5, 8, 11, 22] (ID danh mục cha + tất cả con/cháu)

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

Scope Hữu Ích

// Chỉ lấy danh mục cấp 1 (root)
ProductCategory::where('parent_id', 0)->get();

// Danh mục con trực tiếp
ProductCategory::where('parent_id', $parentId)->orderBy('order')->get();

Brands (Thương hiệu)

Class: \Ecommerce\Models\Brands
Alias: Brands
Table: brands

Cột chính

Tên CộtKiểuMô tả
namestringTên thương hiệu
slugstringURL
imageimageLogo thương hiệu
excerptwysiwygMô tả
seo_titlestringSEO title
seo_descriptionstringSEO description
orderintThứ tự sắp xếp

Truy Vấn

// Tất cả thương hiệu
$brands = Brands::orderBy('order')->get();

// Tìm theo slug
$brand = Brands::where('slug', 'nike')->first();

// Sản phẩm của thương hiệu
$products = Product::where('brand_id', $brand->id)->get();

Currencies (Tiền tệ)

Class: \Ecommerce\Models\Currencies
Alias: Currencies
Table: currencies

Tên CộtKiểuMô tả
currencystringMã tiền tệ (VD: VND, USD)
symbolstringKý hiệu (VD: đ, $)
positionstringVị trí ký hiệu (left/right)
ratefloatTỉ giá nhân (so với tiền tệ mặc định)
rateFixedfloatTỉ giá cộng thêm cố định
decimalsintSố chữ số thập phân
isDefaultintTiền tệ mặc định (1/0)
orderintThứ tự sắp xếp
// Lấy tiền tệ mặc định
$default = Currencies::where('isDefault', 1)->first();

// Lấy tiền tệ đang được user chọn (qua cookie)
$current = Prd::currency();
echo $current->symbol; // 'đ'
echo $current->position; // 'right'

// Định dạng giá theo tiền tệ
echo Prd::price(150000); // '150.000đ'
echo Prd::price(150000, $current); // Truyền object currency tùy chỉnh

Attributes & AttributesItem (Thuộc tính biến thể)

Attributes

Class: \Ecommerce\Models\Attributes
Alias: Attributes
Table: attribute

Tên CộtKiểuMô tả
titlestringTên thuộc tính (VD: Màu sắc, Kích thước)
slugstringSlug
option_typestringLoại hiển thị: label, color, image

AttributesItem

Class: \Ecommerce\Models\AttributesItem
Alias: AttributesItem
Table: attribute_item

Tên CộtKiểuMô tả
titlestringGiá trị (VD: Đỏ, XL, S)
option_idintID của Attributes cha
valuestringGiá trị màu (hex) hoặc ảnh
typestringLoại: label/color/image
imageimageẢnh (nếu type = image)
// Tất cả nhóm thuộc tính
$attributes = Attributes::all();

// Thuộc tính của sản phẩm
$attrIds = DB::table('products_attribute')
->where('product_id', $productId)
->pluck('attribute_id');
$attributes = Attributes::whereIn('id', $attrIds)->get();

// Giá trị của thuộc tính
$items = AttributesItem::where('option_id', $attributeId)->get();

// Loại hiển thị tuỳ chỉnh
$displayType = PrdCartHelper::attributeDisplay($attributeId, 'color');
// 'circle' | 'rectangle-radius' | ''

// Lấy danh sách loại thuộc tính
$types = getAttributesType(); // ['label' => [...], 'color' => [...], 'image' => [...]]

// Thêm loại thuộc tính tùy chỉnh
add_filter('attribute_type', function($types) {
$types['button'] = ['label' => 'Nút bấm (Button)'];
return $types;
});

Variation (Biến thể sản phẩm)

Class: \Ecommerce\Models\Variation
Alias: Variation
Table: products (dùng chung, filter type = 'variations')

Model Variation dùng chung bảng với Product nhưng luôn filter type = 'variations'. Mỗi biến thể có parent_id trỏ đến sản phẩm cha.

// Biến thể của sản phẩm cha
$variations = Variation::where('parent_id', $productId)
->orderBy('order')
->get();

foreach ($variations as $v) {
echo $v->price; // Giá riêng của biến thể
echo $v->price_sale; // Giá KM
echo $v->code; // Mã SKU biến thể
echo $v->attribute_label; // Label tổng hợp: "Đỏ - XL"
echo $v->weight; // Cân nặng riêng
echo $v->stock; // Số lượng tồn kho (nếu có plugin stock)
}

// Lấy thuộc tính-giá trị của một biến thể
$attrItems = DB::table('products_attribute_item')
->where('variation_id', $variation->id)
->get();

Collection (Bộ sưu tập)

Class: \Ecommerce\Models\Collection
Table: products_collections

Bộ sưu tập là nhóm sản phẩm được quản trị viên tạo thủ công (khác với status1/2/3 là flag tự động).

// Tất cả bộ sưu tập
$collections = \Ecommerce\Models\Collection::all();

// Sản phẩm trong bộ sưu tập
$products = Product::whereIn('id', function($q) use ($collectionId) {
$q->select('product_id')
->from('products_collections_relationships')
->where('collection_id', $collectionId);
})->get();

Session (Kho lưu giỏ hàng của user đăng nhập)

Class: \Ecommerce\Models\Session Table: session Primary Key: session_id Alias: không có — phải dùng namespace đầy đủ

Model này là nơi driver Cart\Driver\Database lưu giỏ hàng cho người dùng đã đăng nhập (khách vãng lai dùng Cart\Driver\Session lưu trong PHP session). Cột payload được cast sang mảng.

Cẩn thận: class này trùng tên với Ecommerce\Cart\Driver\Session — luôn import bằng namespace đầy đủ.

Bạn thường không cần tương tác trực tiếp; dùng facade Scart thay thế.


Extra Options (Tùy chọn phụ tính thêm tiền)

Bốn model phục vụ tính năng Extra Product Options — chỉ hoạt động khi bật config general.extras.

ClassAliasTableMô tả
\Ecommerce\Models\ExtraTemplateExtraTemplateproduct_extra_templatesTemplate tùy chọn dùng chung cho nhiều sản phẩm
\Ecommerce\Models\ExtraGroupExtraGroupproduct_extra_groupsNhóm tùy chọn trong một template
\Ecommerce\Models\ExtraItemExtraItemproduct_extra_itemsTừng field tùy chọn (và mức tiền cộng thêm)
\Ecommerce\Models\ExtraUploadExtraUploadproduct_extra_uploadsFile khách tải lên, gắn theo dòng đơn hàng

Logic nghiệp vụ nằm ở ExtraOptionsService, được nối vào các hook cart_add, pre_insert_order_item_data, checkout_after_success, ecommerce_order_deleted trong bootstrap/extra-options.php.