Customize data fields in S-Cart

1. Custom data fields in S-Cart
2. Supported data tables
3. Instructions for adding data fields
4. Instructions for getting data
5. Helper

Since version 7.2.5, S-Cart has had updates to better support flexibility in user data.
Normally, to add a field for an object, you must add a new column of data. This causes you to change the data structure and edit the code to handle adding, deleting, and editing data.
Too complicated and difficult for those who don't understand laravel well. Besides, it is also an obstacle when users want to transfer data from other systems to S-Cart when they are different in data fields.

1. What is a custom field in S-Cart?

We will not directly add new data fields in each table, that will make the data structure bloated, difficult to control, costly to code.
Instead, all new data fields are added unlimitedly in the 2 tables  shop_custom_field and  shop_custom_field_detail. They are linked to each of the main data tables.

2. From version 7.2.5, the list of tables that support custom fields includes:

+ shop_customer
+ shop_product
+ shop_banner
+ shop_brand
+ shop_supplier
+ shop_category
+ shop_news
+ shop_page

3. Instructions for adding custom fields:
Access to  Admin -> SystemConfig -> Setting system -> Custom field data
The "Default" value column is only for data selections of "Select", "Check box", "Radio". Default value is Json type, for example: {"value1": "Name 1", "value2":"Name 2"}

4. Instructions for getting data
a. Get a list of custom data fields
Example get all custom data fields for customers

$listCustomFields = (new \SCart\Core\Front\Models\ShopCustomField)->getCustomField($type = 'shop_customer')

b. Get custom data

$customer = (new \SCart\Core\Front\Models\ShopCustomer)->find($id);
$allDataCustomeField = $customer->getCustomFields(); or $customer->custom_fields;
$dataViaCode = $customer->getCustomField($code);//webding-day

5. Helper
Helper functions for custom fields placed in vendor/S-Cart/core/src/Library/Helpers/custom_field.php

See full data relationship in S-Cart here:


Related topics

Latest Document