Formula extends Base_Route implements Route
REST controller for the server-side formula calculation.
This route is invoked by the client side when the formula is calculated. Formulas are generally calculated in Javascript but when the "Prices entered with tax" is not aligned with the "Display prices in the shop" setting (a configuration that WooCommerce doesn't recommend for rounding reasons), the formula calculation is done server-side to ensure the correct price is calculated.
Tags
Interfaces, Classes and Traits
- Route
Table of Contents
- $rest_base : mixed
- calculate() : WP_REST_Response|WP_Error
- Calculate formulas.
- convert_price() : mixed
- Convert the price depending on the WooCommerce tax settings.
- get_price_excluding_tax() : float
- Calculate the price excluding tax, depending on the WooCommerce settings.
- get_price_including_tax() : float
- Calculate the price including tax, depending on the WooCommerce settings.
- permission_callback() : bool
- Permission callback.
- register_routes() : mixed
- Register the REST routes.
- remove_tax() : float
- Convert the price depending on the WooCommerce tax settings.
Properties
$rest_base
protected
mixed
$rest_base
= 'formula'
Tags
Methods
calculate()
Calculate formulas.
public
calculate(WP_REST_Request $request) : WP_REST_Response|WP_Error
Parameters
- $request : WP_REST_Request
Tags
Return values
WP_REST_Response|WP_Error —convert_price()
Convert the price depending on the WooCommerce tax settings.
public
convert_price(mixed $price, mixed $product) : mixed
Parameters
- $price : mixed
- $product : mixed
Tags
Return values
mixed —get_price_excluding_tax()
Calculate the price excluding tax, depending on the WooCommerce settings.
public
get_price_excluding_tax(float $price, WC_Product $product[, bool $force = false ]) : float
This method is borrowed from WooCommerce and modified to allow negative price calculations for example when a discount is applied to a product via a formula.
Parameters
- $price : float
-
The price tax is being removed from.
- $product : WC_Product
-
The current product object.
- $force : bool = false
-
Whether to force the tax removal.
Tags
Return values
float —The resulting price.
get_price_including_tax()
Calculate the price including tax, depending on the WooCommerce settings.
public
get_price_including_tax(float $price, WC_Product $product) : float
This method is borrowed from WooCommerce and modified to allow negative price calculations for example when a discount is applied to a product via a formula.
Parameters
- $price : float
-
The price being calculated.
- $product : WC_Product
-
The current product object.
Tags
Return values
float —The result of the tax calculation.
permission_callback()
Permission callback.
public
permission_callback() : bool
Tags
Return values
bool —register_routes()
Register the REST routes.
public
register_routes() : mixed
Tags
Return values
mixed —remove_tax()
Convert the price depending on the WooCommerce tax settings.
public
remove_tax(float $price, WC_Product $product) : float
Parameters
- $price : float
-
The price tax is being removed from.
- $product : WC_Product
-
The current product object.
Tags
Return values
float —The price with the tax removed.