Developers

Developer Reference

Published

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:

  1. woocommerce_before_product_object_save (or woocommerce_before_product_variation_object_save), with a product object holding the old values and the new ones set as changes, so get_changes() shows both;
  2. woocommerce_delete_product_transients;
  3. woocommerce_product_object_updated_props, with sale_price, price, date_on_sale_from and date_on_sale_to;
  4. save_post_product (or save_post_product_variation), save_post and wp_insert_post, with $update true;
  5. woocommerce_update_product (or woocommerce_update_product_variation);
  6. woocommerce_after_product_object_save (or woocommerce_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 the product_variation equivalents;
  • woocommerce_variation_prices_price, woocommerce_variation_prices_sale_price;
  • woocommerce_product_is_on_sale;
  • woocommerce_product_get_date_on_sale_to and 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:

HookWhen
ssappd_real_sync30 seconds after a product is saved during a sale, and when Super Speedy Ajax switches between TEST and LIVE
ssappd_real_boundaryAt each enabled rule’s From time, and one second after its To time; args rule and edge (from or to)
ssappd_real_sync_hourlyEvery hour
ssappd_real_workRuns queued batches of save hooks, about 20 seconds at a time

Deactivating the add-on unschedules all four.

Stored data

Options

OptionHolds
ssappd_rulesEvery rule, in priority order, autoloaded
ssappd_real_cronyes when Run sale price updates in the background is ticked
ssappd_purge_cacheno when Purge the page cache when a sale starts or ends is unticked
ssappd_last_purgeThe last purge: purged, cache, why, at
ssappd_delete_data_on_uninstallyes to delete the rules when the add-on is deleted

A rule in ssappd_rules:

KeyValues
idA unique key, for example r1f4af48bad
titleThe rule name
enabledyes or no
from, toY-m-d or Y-m-d\TH:i in the site’s timezone, or empty
targetall, terms or products
groupsConditions: [ { taxonomy, terms: [ term ids ] } ]
product_idsFor products
exclude_product_ids, exclude_groupsExclusions
on_salekeep, lower (whichever is lower) or stack (off the sale price as well)
typepercent, fixed_amount or fixed_price
amountA number; a percentage is capped at 100
modereal (change each product’s sale price) or virtual (display only)
on_endrestore or clear
countdownyes 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

KeyOnHolds
_ssappd_ruleA product or variation whose sale price a rule wroteThe rule’s id
_ssappd_hands_offA product edited by hand during a rule’s saleThe 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:

StatusMeaning
activeWritten and in force
revertedThe sale ended and the product was put back
editedChanged by hand during the sale, so left alone; listed under Edited since, not put back
edited_seenAs 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

how the batches of save hooks run
when ssappd_purge_page_cache fires
the add-on hooks in Super Speedy Ajax

Written for Super Speedy Ajax – Price Discounts 0.4.3.

Comments