Developer Reference
Functions, actions and filters Price Discounts provides, the WooCommerce hooks it fires when it writes a sale price, and the data it stores. Everything here is read from the add-on’s code.
Available since 0.1.0. Real sale prices, the purge action and the job filters are available since 0.4.0.
Functions
ssappd_discount_for( $product )
/**
* @param WC_Product $product A simple product or a variation.
* @return array|null { price: string, regular: string, rule: array }
*/
$discount = ssappd_discount_for( $product );
// array( 'price' => '4303.20', 'regular' => '4890', 'rule' => array( ... ) )
The discount a display-only rule applies to the product right now, or null. It also answers for a rule that changes sale prices until that rule has written the product’s price. Once a rule has written the price, the discount is the product’s own sale price, and this returns null: read the product’s sale price, and _ssappd_rule (below) to tell which rule set it. Returns null while the add-on is switched off. Variable parents have no price of their own; pass a variation.
ssappd_is_active()
true when discount rules are applied on this request: the add-on is active, Super Speedy Ajax 1.9.3 or later and WooCommerce are present, and Price Discounts is ticked under Advanced > Add-ons.
Actions
ssappd_purge_page_cache
do_action( 'ssappd_purge_page_cache', string $why );
Fires on every page cache purge, after the detected cache plugin’s own purge, and also when no supported cache plugin is found. Hook it to purge a CDN or a host cache. $why is a readable reason: discount rules saved, rule Black Friday jackets started, rule Black Friday jackets ended or sale prices changed. Does not fire when Purge the page cache when a sale starts or ends is unticked.
add_action( 'ssappd_purge_page_cache', function ( $why ) {
// Purge your CDN here.
} );
ssappd_real_before_apply
do_action( 'ssappd_real_before_apply', int[] $write_ids, int[] $revert_ids );
Fires during a sync, after the add-on has worked out which products and variations get a rule’s sale price ($write_ids) and which are put back ($revert_ids), and before anything is written. Every product about to be touched is re-read after this action, so a sale price changed here is treated as an edit by hand and left alone.
Filters
ssappd_rule_amount
apply_filters( 'ssappd_rule_amount', float $amount, array $rule, WC_Product $product );
Filters the amount of a Fixed amount off or Fixed price rule, in the store’s base currency, before it is applied to the price shown. With the Country Based Prices add-on active, $amount arrives converted for the visitor’s zone. It applies to display-only rules, and to a rule that changes sale prices until it has written the product’s price; the sale prices written into products are worked out from the rule’s amount as saved.
ssappd_real_job_size
apply_filters( 'ssappd_real_job_size', int $size );
Products per batch of save hooks. Default 250 when the hooks run in the browser, 1000 with Run sale price updates in the background ticked. Every variation of a variable product stays in the same batch, so a batch can be larger than $size. Minimum 1.
WooCommerce hooks fired when a rule writes a sale price
The prices are written in bulk first. Then, for each changed product or variation, a batch fires what WooCommerce fires on a save, in this order:
woocommerce_before_product_object_save(orwoocommerce_before_product_variation_object_save), with a product object holding the old values and the new ones set as changes, soget_changes()shows both;woocommerce_delete_product_transients;woocommerce_product_object_updated_props, withsale_price,price,date_on_sale_fromanddate_on_sale_to;save_post_product(orsave_post_product_variation),save_postandwp_insert_post, with$updatetrue;woocommerce_update_product(orwoocommerce_update_product_variation);woocommerce_after_product_object_save(orwoocommerce_after_product_variation_object_save), with the product reading its new price.
Each variable parent is then re-synced with WC_Product_Variable::sync( $parent_id, true ), which rebuilds its price rows and fires its own save hooks.
WooCommerce filters the add-on uses
Display-only rules answer these at priority 20, after Country Based Prices (priority 5) and ordinary priority-10 price filters, so a percentage comes off the price the visitor would otherwise see:
woocommerce_product_get_price,woocommerce_product_get_sale_price, and theproduct_variationequivalents;woocommerce_variation_prices_price,woocommerce_variation_prices_sale_price;woocommerce_product_is_on_sale;woocommerce_product_get_date_on_sale_toand the variation equivalent, for the sale countdown;woocommerce_get_variation_prices_hash, so WooCommerce’s cached variation prices rebuild when the rules in force change.
Only WooCommerce’s view context is filtered, so a product save never stores a display-only price. A price another plugin sets on a product object in memory, such as a gift plugin calling set_price( 0 ) on a basket item, is left as set.
Sale listings use pre_transient_wc_products_onsale to add rule-discounted products to wc_get_product_ids_on_sale().
Scheduled actions
All in the Action Scheduler group ssappd, visible under WooCommerce > Status > Scheduled Actions:
| Hook | When |
|---|---|
ssappd_real_sync | 30 seconds after a product is saved during a sale, and when Super Speedy Ajax switches between TEST and LIVE |
ssappd_real_boundary | At each enabled rule’s From time, and one second after its To time; args rule and edge (from or to) |
ssappd_real_sync_hourly | Every hour |
ssappd_real_work | Runs queued batches of save hooks, about 20 seconds at a time |
Deactivating the add-on unschedules all four.
Stored data
Options
| Option | Holds |
|---|---|
ssappd_rules | Every rule, in priority order, autoloaded |
ssappd_real_cron | yes when Run sale price updates in the background is ticked |
ssappd_purge_cache | no when Purge the page cache when a sale starts or ends is unticked |
ssappd_last_purge | The last purge: purged, cache, why, at |
ssappd_delete_data_on_uninstall | yes to delete the rules when the add-on is deleted |
A rule in ssappd_rules:
| Key | Values |
|---|---|
id | A unique key, for example r1f4af48bad |
title | The rule name |
enabled | yes or no |
from, to | Y-m-d or Y-m-d\TH:i in the site’s timezone, or empty |
target | all, terms or products |
groups | Conditions: [ { taxonomy, terms: [ term ids ] } ] |
product_ids | For products |
exclude_product_ids, exclude_groups | Exclusions |
on_sale | keep, lower (whichever is lower) or stack (off the sale price as well) |
type | percent, fixed_amount or fixed_price |
amount | A number; a percentage is capped at 100 |
mode | real (change each product’s sale price) or virtual (display only) |
on_end | restore or clear |
countdown | yes or no |
Rules written with update_option( 'ssappd_rules', $rules ) are applied as a save on the Discounts tab is: prices written or put back, start and end times scheduled, the page cache purged. Run them through SSAPPD_Rules::sanitize() first, because the settings sanitiser is registered only in wp-admin.
Post meta
| Key | On | Holds |
|---|---|---|
_ssappd_rule | A product or variation whose sale price a rule wrote | The rule’s id |
_ssappd_hands_off | A product edited by hand during a rule’s sale | The rule’s id, until that sale ends |
Tables
{prefix}ssappd_changes is the record of every sale price a rule wrote: rule_id, product_id, parent_id, the product’s own sale price and dates before the rule (prev_sale, prev_from, prev_to), what the rule wrote (written_sale, written_to), on_end, and status:
| Status | Meaning |
|---|---|
active | Written and in force |
reverted | The sale ended and the product was put back |
edited | Changed by hand during the sale, so left alone; listed under Edited since, not put back |
edited_seen | As edited, after Clear this list |
{prefix}ssappd_jobs holds batches of save hooks waiting to run, and is empty when they are done.
Transient
ssappd_rule_on_sale_ids caches the product IDs the rules add to WooCommerce’s sale listings. It is rebuilt when the rules in force change and deleted whenever WooCommerce deletes a product’s transients.
Super Speedy Ajax hooks the add-on uses
ssap_register_addons adds Price Discounts to Advanced > Add-ons; ssap_settings_tabs and ssap_settings_panels add the Discounts and Sale prices tabs; ssap_settings_uninstall_options adds the delete-on-uninstall checkbox. Creating Add-ons for Super Speedy Ajax documents them.
Further reading
ssappd_purge_page_cache firesWritten for Super Speedy Ajax – Price Discounts 0.4.3.
Comments