Developer notes
Technical reference for developers who work with SKU & Product Code Generator.
Add-on identity
Section titled “Add-on identity”| Item | Value |
|---|---|
| Add-on id | kv_sku_code_generator |
| Function prefix | fn_kv_sku_code_generator_ |
| Language variables | kv_sku_code_generator.* |
| CSS scope | .kv-skug, .kv-skug-* |
| PHP namespace | Tygh\Addons\KvSkuCodeGenerator |
| Database tables | ?:kv_skug_rules, ?:kv_skug_short_codes, ?:kv_skug_sequences, ?:kv_skug_codes, ?:kv_skug_locks, ?:kv_skug_runs |
Hooks used
Section titled “Hooks used”| Hook | Purpose |
|---|---|
update_product_post |
Saves the lock flag of the product form, drops stale reservations and gives a NEW product with an empty code its generated code. |
delete_product_post |
Removes the reservation and the lock of the deleted product. |
variation_group_save_group |
After a variation group is saved: replaces the code the core proposed for each new variation (<parent code>_XXXX) with a code from the variation rule; a code typed by hand is kept. |
The product code is the core column ?:products.product_code (varchar 64). Nothing else in core is changed.
Classes
Section titled “Classes”Pattern: parser, validator and renderer of patterns.Pattern::fit()joins the parts to at most 64 characters (mb-safe).Translit: transliteration and automatic short codes.ShortCodes,Rules: storage and resolution (own company first, then the owner; most specific category first).Sequence: counters and reservations.Sequence::next()hands out a number with oneUPDATE ... SET last_value = LAST_INSERT_ID(last_value + 1)on an InnoDB row, so concurrent callers are serialised by the row lock.Sequence::reserve()inserts the code into?:kv_skug_codes(unique key); if another product holds it the generator takes the next number.Generator:product($id, $mode)plans or applies one product (dry run when constructed withtrue: counters are simulated in memory in the same order, so a preview equals the run).Generator::assignNew($id)andGenerator::forVariation()are used by the hooks.Runs: selections, batches (500, cursor claimed with a compare-and-swap), run log, undo file, duplicate report.
use Tygh\Addons\KvSkuCodeGenerator\Generator;
// Fill the code of one product if it is empty (no-op when it has a code or is locked).$code = Generator::assignNew($product_id);Config::companyId() is runtime.company_id: 0 for the root administrator, the vendor on Multi-Vendor, the storefront for a store-bound administrator on Ultimate. Every object with a company (rule, run, product, vendor row) is checked with Config::canAccessCompany() in the controller; selections always add p.company_id = <scope>. Multi-Vendor Ultimate storefronts are not companies and the add-on does not read the storefront id, so rules and runs there are per vendor.
Undo files
Section titled “Undo files”var/kv_sku_code_generator/undo/run_<id>_<random>.csv (columns product_id, old_code, new_code), protected by an .htaccess file and an index.html, removed after 30 days. The log row stays.
Upgrades
Section titled “Upgrades”The add-on ships the Kavio Labs Upgrade Synchronizer: replacing the files in place (manual upload or Upgrade Center) adds missing tables, settings and language variables on the next request.
Database note
Section titled “Database note”The core has no index on the product code, so every uniqueness check would scan the whole products table. The add-on adds one plain index, kv_skug_product_code, to the products table when it is installed (an index that already exists on the column is used as it is, and the outcome is stored in kv_skug_sequences, so no request repeats the check). cscart_products is a MyISAM table, where ALTER TABLE ... ADD INDEX copies the table and blocks product writes while it runs. A table with more than 300,000 rows is therefore never altered during install or inside a storefront or product request: the add-on pages show a notice with a Create index now button for the root administrator, and the statement is ALTER TABLE <prefix>products ADD INDEX kv_skug_product_code (product_code) if you prefer to run it yourself at a quiet time. Without the index the add-on stays correct but a bulk run on a large catalogue is slow. Uninstalling removes only the index with this name.