Weight-Based Products — Developer Guide

Architecture, extension points, REST API, and runtime contracts.

Architecture at a glance

Extension units (custom kg/lb families, ml/L, etc.): hook lf_wbpw_register_units, LF_WBPW_Unit_Family_Interface::get_js_config(), and filters for meta conversion and REST.

Pricing engine contract

Main API:

LF_WBPW_Pricing::compute_line_pricing(array $context): array

Expected keys in $context:

Returns:

Order meta contracts

Meta keyWhere setPurpose
_weight_datacheckout item creationStructured payload for rendering and reporting
_selected_packagespacks mode checkoutStock reduce/restore source data
_total_weightpacks and free mode checkoutTotal weight snapshot; free mode drives inventory reduction
_wbpw_display_unit_at_ordercheckout item creationLocks stock writes to order-time unit bucket
_wbpw_sidescheckout item creationStructured sides payload for grouped/separate modes
_wbpw_weight_line_totalcheckout item creationWeight + paid options split for reporting
_wbpw_sides_totalcheckout item creationSides split for reporting
_weight_based_pack_badgesproduct save (Fixed Packs)Map of pack weight key → marketing slug (recommended, best_seller); empty = no badge
_wbpw_package_card_styleproduct saveOptional per-product override: list | grid; omitted/inherit uses global lf_wbpw_package_card_style

Free-weight inventory (product meta)

When set, remaining weight is stored per base unit bucket:

Cart-aware helpers: LF_WBPW_Public::get_cart_adjusted_fw_remaining($product), LF_WBPW_Public::get_fw_effective_max_weight($product).

Hooks and filters

Actions

Filters

REST (wc-wbp/v1)

WooCommerce GET /wp-json/wc/v3/products/{id} responses include a weight_based object for weight-based products: ordering_mode, unit_config, package_card_style (resolved list/grid), packs weights, etc. Filter: lf_wbpw_rest_product_weight_based_data.

Public methods developers commonly call

WC_Product_Weight_Based

LF_WBPW_Public (static)

LF_WBPW_Units

REST API

WooCommerce REST product responses are also extended via woocommerce_rest_prepare_product_object.

Suggestion mode semantics

Desired-weight / preset cap (Fixed Packs JS): Global option lf_wbpw_combo_max_items (default 25, range 1–500) limits total number of pack lines in the bestCombo() search in assets/js/lf-wbpw-public.js. Passed to the storefront as lf_wbpw_front_params.combo_max_items. Raise when many small packs are needed to approximate a target weight.

Pack marketing badges (Fixed Packs)

Slugs are defined in LF_WBPW_Pack_Badges::get_slugs() (e.g. recommended, best_seller). Stored in _weight_based_pack_badges. Admin saves pack rows as JSON triples [weight, stock, badge_slug] in the hidden lf_wbpw_weights field. Storefront: .pkg-marketing-badge next to the pack name; admin pack pills show optional small labels.

Admin vs storefront: pack stock colouring

Product editor pack “pills” use CSS classes lf_wbpw_weight_tag--stock-ok|stock-low|stock-out against the global low-stock threshold. Row-level inventory tint on the storefront was intentionally not tied to this (stock text badges remain).

Stock consistency note

Stock reduce/restore uses order item key _wbpw_display_unit_at_order to prevent unit-switch drift after an order is created.

Compatibility notes

Product editor (Fixed Packs)