Skip to content

Developer notes

Technical reference for developers who customize Discount Rules.

Item Value
Add-on id kv_discount_rules
Function prefix fn_kv_discount_rules_
Language variables kv_discount_rules.*
CSS scope .kv-discount-rules
PHP namespace Tygh\Addons\KvDiscountRules

The core calculates a cart in fn_calculate_cart_content(): catalog promotions while it collects the product data, then fn_promotion_apply('cart'), then shipping and taxes. The add-on sits between the promotions and the taxes:

  1. promotion_apply_pre (zone cart): remembers, per cart line, the catalog discount and the promotions that produced it.
  2. calculate_cart_taxes_pre (arguments $cart, $cart_products, $product_groups, $calculate_taxes, $auth): reads what the cart promotions did per line (from the cumulative promotions[id].bonuses[].discount) and per order (cart.promotions[id].total_discount of order_discount bonuses), asks Engine::compute() for the final amount of every line and promotion, and writes the result back: line price, discount, promotions, cart.subtotal, cart.subtotal_discount. Taxes and totals are calculated afterwards by the core.
  3. calculate_cart_items resets the remembered state at the start of every calculation, so a cart that is recalculated without cart promotions is never touched with old data.

If anything fails, the cart keeps the core result. The add-on does nothing when it is disabled, unlicensed, when the cart has stored (manual) prices or discounts, or when the core does not recalculate promotions (order edit).

Tygh\Addons\KvDiscountRules\Engine is plain PHP: integer minor units, no CS-Cart calls, covered by tests/unit.php. Rounding: the discount of a whole line is calculated in minor units; per unit it is floor(total / quantity) and the remainder (less than one minor unit per unit) is added to the order-level discount, so the order total is exact.

Multi-Vendor: fn_place_suborders() copies the parent cart and recalculates it with parent_order_id. The parent calculation stores its result in cart.kv_dr (kept in the copied cart); in a suborder the stored amounts of the suborder’s lines are applied instead of a new decision, so the suborders add up to the parent order.

PHP: calculate_cart_items, promotion_apply_pre, calculate_cart_taxes_pre, place_order, delete_order, update_promotion_post, delete_promotions_post, update_product_post, delete_product_post, clone_product_post, init_templater_post (upgrade synchronizer, only while pending).

Template hooks: promotions:general_content (admin), products:product_update_price (admin), checkout:product_info and orders:product_info (storefront), orders:product_info and orders:totals (admin). Controllers: promotions.post.php, products.post.php, orders.post.php (admin) and orders.post.php (storefront) only assign template variables.

None.

Table Content
?:kv_dr_rules One row per promotion that has its own policy or cap: promotion_id, policy (stack, best, never or empty), cap_pct, cap_amt.
?:kv_dr_costs The own cost field: product_id, cost. Used when Product Cost & Profit Reports is not active (or the source is “Own field”).
?:kv_dr_orders One row per placed order that Discount Rules touched: order_id, data (JSON summary).

The notes of a line are also kept in the order line data (extra.kv_dr). All three tables are dropped on uninstall.

extra.kv_dr is a list of {t, n, a}: t the note type (replaced, kept, blocked, cap_line, cap_promo, cap_order, floor), n the promotion name(s), a the amount in the primary currency (0 when not applicable).

Storefront templates live in design/themes/<theme>/templates/addons/kv_discount_rules/. To customize one, copy it to your own theme’s folder; do not edit the add-on’s files, they are replaced on update.

Path Purpose
app/addons/kv_discount_rules/Tygh/Addons/KvDiscountRules/Engine.php The combination engine (pure PHP).
.../Cart.php Reads the core result, calls the engine, writes the result back.
.../Config.php, .../Costs.php Settings, per-promotion rules, cost lookup.
.../License/Client.php Domain-bound license check (generated by kv license-client).
.../Upgrade/Synchronizer.php Self-healing in-place upgrade (generated by kv synchronizer).
app/addons/kv_discount_rules/func.php, init.php Hook handlers and registration.
app/addons/kv_discount_rules/controllers/ Template variables for the promotion, product and order pages; the license “Check now” action.
design/backend/templates/addons/kv_discount_rules/ Admin templates (promotion block, cost field, order notes, license card).
var/themes_repository/responsive/templates/addons/kv_discount_rules/ Storefront notes (cart, checkout, order page).
tests/unit.php, tests/php/store_test.php, tests/e2e/ Engine tests, store test and HTTP e2e.

The add-on ships the Kavio Labs license client: License\Client::isActive() is the runtime gate (a stored result, no remote call per request). Without a valid license carts are calculated as in stock CS-Cart; placed orders keep their notes.