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

Supports: Config (Cấu Hình Sicommerce)

Class: \Ecommerce\Supports\Config
Namespace: Ecommerce\Supports

Class Config là lớp truy cập cấu hình cho toàn bộ Sicommerce. Tất cả config được lưu trong database (bảng options, key ecommerce_config) và được load vào Laravel config container với prefix sicommerce::.


Phương Thức

Config::get(string $keyword)

Lấy một giá trị config theo key dạng dot-notation.

use Ecommerce\Supports\Config;

// Lấy layout trang sản phẩm
$layout = Config::get('general.layout');

// Số sản phẩm mỗi trang
$perPage = Config::get('product.index.per_page');

// Có bật tính năng Brands không
$hasBrands = Config::get('general.brands'); // true/false

// Số cột trên desktop
$cols = Config::get('product.index.per_row.desktop'); // 4

// Cấu hình đặt hàng
$layoutDetail = Config::get('general.layout');
$weightUnit = Config::get('general.weight_unit'); // 'gram', 'kg'...

// Thông tin đơn hàng
$securityKey = Config::get('order.securityKey');

Tương đương với:

config('sicommerce::general.layout');

Config::all()

Lấy toàn bộ config của Sicommerce dưới dạng mảng phẳng (đã loại bỏ prefix sicommerce::).

$allConfig = Config::all();
// ['general' => [...], 'product' => [...], 'order' => [...], 'checkout' => [...]]

Config::update($key, $value)

Cập nhật một key trong config (lưu vào DB và làm mới container).

// Cập nhật số sản phẩm mỗi trang
Config::update('product.index.per_page', 20);

// Cập nhật layout
Config::update('general.layout', 'layout-products-2');

Config::save($config)

Lưu toàn bộ mảng config (thay thế toàn bộ).

$config = Config::all();
$config['product']['index']['per_page'] = 16;
Config::save($config);

Cấu Trúc Config Mặc Định

Config được định nghĩa trong plugins/sicommerce/config/. Dưới đây là các nhóm chính:

general – Cấu hình chung

KeyMô TảGiá Trị Mặc Định
general.layoutLayout trang danh sách SPlayout-products-1
general.brandsBật/tắt tính năng Thương hiệufalse
general.weight_unitĐơn vị cân nặnggram
general.currency_symbolKý hiệu tiền tệ

product – Cấu hình sản phẩm

KeyMô Tả
product.index.per_pageSố SP mỗi trang
product.index.per_row.desktopSố cột trên desktop
product.index.per_row.tabletSố cột trên tablet
product.index.per_row.mobileSố cột trên mobile
product.object.title.showHiển thị tiêu đề trong product item
product.object.title.positionPriority hiển thị tiêu đề
product.object.price.showHiển thị giá trong product item
product.object.price.positionPriority hiển thị giá
product.object.imgCấu hình box ảnh product item

order – Cấu hình đơn hàng

KeyMô Tả
order.securityKeyTên tham số security key trong URL
order.created.send_customerGửi email xác nhận cho khách
order.created.send_adminGửi email thông báo cho admin

checkout – Cấu hình thanh toán

KeyMô Tả
checkout.payment.defaultCổng thanh toán mặc định được chọn sẵn
checkout.shipping.defaultĐơn vị vận chuyển mặc định

Đọc Config Trong Plugin Của Bạn

// Trong ServiceProvider hoặc bất kỳ file nào của plugin
use Ecommerce\Supports\Config;

// Kiểm tra theme đang dùng layout nào
$layout = Config::get('general.layout');
if($layout == 'layout-products-2') {
// Plugin custom giao diện cho layout 2
}

// Lấy đơn vị cân nặng cho shipping
$weightUnit = Config::get('general.weight_unit');

// Số sản phẩm mỗi trang (dùng cho query)
$limit = (int)Config::get('product.index.per_page');
if($limit <= 0) $limit = 12; // Fallback

Thêm Config Của Plugin Vào Sicommerce Config Group

Bạn có thể mở rộng group config của Sicommerce để Admin có thể cấu hình plugin của bạn từ trang Hệ thống → Commerce:

// Đăng ký tab Setting mới
add_filter('admin_system_tabs', function($tabs) {
$tabs['your_plugin_settings'] = [
'label' => 'Your Plugin',
'group' => 'commerce', // Thuộc nhóm Commerce
'action' => 'admin_your_plugin_setting',
];
return $tabs;
}, 80);

// Render form cấu hình
add_action('admin_your_plugin_setting', function() {
$apiKey = Config::get('your_plugin.api_key') ?? '';
$form = form();
$form->text('your_plugin[api_key]', ['label' => 'API Key'], $apiKey);
echo $form->html();
});

// Lưu cấu hình
add_action('admin_system_commerce_your_plugin_setting_save', function() {
$data = request()->input('your_plugin');
Config::update('your_plugin.api_key', $data['api_key'] ?? '');
response()->success('Đã lưu cấu hình');
});

Lưu ý: Config của Sicommerce được merge với file config trong database khi load. Nếu bạn thêm key mới, hãy đảm bảo có giá trị fallback trong file config/ của plugin Sicommerce hoặc plugin của bạn, tránh trường hợp Config::get() trả về null khi chưa được cấu hình.