Skip to content

Developer notes

  • app/addons/kv_post_purchase_upsell/Tygh/Addons/KvPostPurchaseUpsell/: Engine (offer of a page, accept, decline), Matcher (rules), Pricing, SameOrder (order edit), Linked (quick checkout line), Token (HMAC), Events (funnel rows), Stats, Statuses, RateLimit, Offers, Config.
  • Tables: kv_ppu_offers (offers), kv_ppu_events (one row per order and offer: state V viewed, W being accepted, A added to the same order, P quick checkout started, C quick checkout order placed, D declined; revenue, paid / void flags, target order), kv_ppu_rate (rate limit buckets).
  • Controllers: kv_ppu (storefront: accept, decline, POST only; admin / vendor panel: manage, add, update, delete, m_delete, update_status), checkout.post.php (offer for checkout.complete).
  • Storefront template: addons/kv_post_purchase_upsell/components/offer.tpl via the hook checkout:order_confirmation.
Hook Why
generate_cart_id an offer line gets its own cart id (never merged with the same product the customer added)
pre_get_cart_product_data fixed offer price for a valid quick-checkout line (no catalog promotion on top, quantity capped); invalid / expired / used lines fall back to the normal price
pre_place_order storefront checkout only (not for an edit of an existing order): named MySQL lock per offer line and “already used” check
place_order points the offer at the new order (on the vendor order that holds the line; Multi-Vendor parent orders and edits are skipped); the offer stays P while that order is incomplete / failed
change_order_status P -> C when the order leaves the incomplete / failed / cancelled statuses; paid / void flags of the report
delete_order a deleted order no longer counts as revenue; the customer link of the event is removed

All hooks exist in CS-Cart 4.17.2 and in 4.21.2 / 4.22 with the same arguments.

SameOrder::add() follows Tygh\Api\Entities\Orders::update() of the core: fn_form_cart() (with ORDER_MANAGEMENT so the original lines keep their stored prices), fn_store_shipping_rates(), the new line with stored_price = Y and the offer price, fn_calculate_cart_content(), fn_update_payment_surcharge(), fn_update_order() and fn_change_order_status() back to the old status with the notification flags from the settings. Before fn_update_order() it checks (under the named lock kvppu_o_<hash>_<order id>): one line more, the new line with the requested quantity, cart subtotal = old subtotal + new line (to the cent), no shipping failure, same chosen shipping methods, same promotions and subtotal discount, a grand total not lower than before, and (when the storefront sent it) the total shown next to the button. SameOrder::preview() runs the same calculation without writing for kv_ppu.preview (JSON, no-store, rate limited). The return of fn_change_order_status() is checked (one retry); a failure leaves the event in state X (needs review) with no revenue. It refuses (and the customer gets the quick checkout) for: Multi-Vendor parent orders, an order of another vendor, a status that is not in the list, and a payment with a processor script that is not in the extra list.

extra.kv_ppu = {e, o, s, p, q, x, h}: event, offer, source order, price in cents, quantity, expiry, HMAC-SHA256 over the fields with a 256-bit secret in storage data (kv_ppu_secret, removed on uninstall). The price applies only for the offered product, while the signature is valid, the event is in state P (without a target order, or with a target order that is still incomplete / failed / cancelled), the time has not passed, the line is the first one with this event in the cart, and the visitor may see the source order (fn_is_order_allowed). A visitor can post any extra with add-to-cart requests, so all of these conditions are checked on every cart calculation.

  • schemas/security/csrf_validation.post.php adds kv_ppu to the storefront controllers that need the form security hash.
  • Order access uses fn_is_order_allowed() (owner, or the order ids of a guest session).
  • Accept and decline claim the event with a conditional UPDATE ... WHERE state = 'V' (ROW_COUNT() = 1), so a double click or two tabs cannot add the item twice; a W row older than 120 s (a crashed request) can be claimed again; while one offer of an order is being added, another one is refused.
  • Rate limit: 30 accepts / declines per 10 minutes per client network (ClientIp::rateKey(): IPv4 /32, IPv6 /64; REMOTE_ADDR, the Cloudflare header only from Cloudflare ranges, your own proxy only from the trusted list; never fn_get_ip()).
  • Template output is printed plain (CS-Cart escapes printed values); no nofilter on merchant or customer text.

The offer is shown only on checkout.complete, which needs the session and is never served from the full page cache. The add-on prints nothing on cacheable pages (home, categories, products, pages).

kv_ppu_events.user_id links a funnel row to a customer. It is set to 0 when the order is deleted. The offer tables hold no names, addresses or e-mail addresses.

tests/unit.php (price maths, rules, A/B split, z-test, statuses, tokens), tests/cli/e2e.php (real orders in a dev store: same order, quick checkout, tampering, rules, countdown, window, status flow, report, Multi-Vendor scope, rate limit), tests/e2e/e2e.mjs (HTTP: customer, other customer, guest, CSRF, admin, vendor panel).