YourPlugins

Change the calculated price

Part of Developer reference.

All docs

You can change the calculated price by using WordPress filters in your theme or custom plugin.

For new integrations, use the yps_calcsheet_* filters shown below. Legacy awspc_* filters are still supported for backward compatibility, but they should not be used in new code.

You can place these examples in your theme functions.php file or in a custom plugin.

Change the final calculated price

/*
* $price                     The final formatted-or-unformatted price value currently being returned.
* $params['errors']          Validation errors.
* $params['priceRaw']        Raw numeric price before formatting.
* $params['product']         Product data.
* $params['calculator']      Calculator object.
* $params['data']            Raw input values.
* $params['userData']        Transformed values used by the calculation engine.
* $params['outputResults']   Output field results.
* $params['formatPrice']     Whether the final price should be formatted.
*/
function custom_yps_calcsheet_filter_calculate_price($price, $params){
    return $price * 5;
}
add_filter('yps_calcsheet_filter_calculate_price', 'custom_yps_calcsheet_filter_calculate_price', 10, 2);

add_filter is a WordPress core function. See the official documentation for more details.

Change only the AJAX calculation response

/*
* $response['errorsCount']       Number of validation errors.
* $response['errors']            Validation errors.
* $response['price']             Formatted price returned to the frontend.
* $response['priceRaw']          Raw numeric price.
* $response['outputFields']      Output field values.
* $response['conditionalLogic']  Visibility map for calculator fields.
*
* $params['productId']           Product ID.
* $params['calculator']          Calculator object.
* $params['fields']              Calculator fields used at runtime.
* $params['postData']            Raw values sent by the frontend.
* $params['conditionalLogic']    Visibility map for calculator fields.
* $params['outputResults']       Output field results before serialization.
* $params['errors']              Validation errors.
* $params['price']               Formatted price returned to the frontend.
* $params['priceRaw']            Raw numeric price.
* $params['page']                Runtime context such as product or cart.
*/
function custom_yps_calcsheet_filter_calculate_price_ajax_response($response, $params){
    return $response;
}
add_filter('yps_calcsheet_filter_calculate_price_ajax_response', 'custom_yps_calcsheet_filter_calculate_price_ajax_response', 10, 2);

Change the calculator price in cart

If you want to adjust the product price in cart, for example to add a surcharge, discount, or custom tax logic, use the cart price filter below.

/*
* $productPrice            The current calculated product price in cart.
* $fieldsData              The current calculator field values.
* $outputFields            The current output field values.
*/
function custom_yps_calcsheet_filter_calculate_price_in_cart($productPrice, $fieldsData, $outputFields){
    return $productPrice + 100;
}
add_filter('yps_calcsheet_filter_calculate_price_in_cart', 'custom_yps_calcsheet_filter_calculate_price_in_cart', 10, 3);