WC_Shipping_Flat_Rate::sanitize_cost
Sanitize the cost field.
Method of the class: WC_Shipping_Flat_Rate{}
No Hooks.
Returns
string.
Usage
$WC_Shipping_Flat_Rate = new WC_Shipping_Flat_Rate(); $WC_Shipping_Flat_Rate->sanitize_cost( $value );
- $value(string) (required)
- Unsanitized value.
Changelog
| Since 3.4.0 | Introduced. |
WC_Shipping_Flat_Rate::sanitize_cost() WC Shipping Flat Rate::sanitize cost code WC 11.3.0
public function sanitize_cost( $value ) {
$value = is_null( $value ) ? '' : $value;
$value = wp_kses_post( trim( wp_unslash( $value ) ) );
$this->get_weight_placeholder()->validate( $value );
$value = str_replace( array( get_woocommerce_currency_symbol(), html_entity_decode( get_woocommerce_currency_symbol() ) ), '', $value );
$contains_shortcodes = false !== strpos( $value, '[' ) || false !== strpos( $value, ']' );
if ( ! $contains_shortcodes && ! $this->is_math_expression( $value ) ) {
$value = \Automattic\WooCommerce\Utilities\NumberUtil::sanitize_cost_in_current_locale( $value );
}
// Thrown an error on the front end if the evaluate_cost will fail.
$dummy_cost = $this->evaluate_cost(
$value,
array(
'cost' => 1,
'qty' => 1,
'weight' => 1,
)
);
if ( false === $dummy_cost ) {
throw new Exception( WC_Eval_Math::$last_error );
}
// A package weighs nothing when none of its items have a weight set, which is a common configuration.
// Reject costs that can't be evaluated in that case, rather than silently offering the rate for free.
$zero_weight_cost = $this->evaluate_cost(
$value,
array(
'cost' => 1,
'qty' => 1,
'weight' => 0,
)
);
if ( false === $zero_weight_cost ) {
throw new Exception( esc_html__( 'This cost can\'t be calculated when no item in the cart has a weight. Use [weight min="1"] to set a minimum billable weight.', 'woocommerce' ) );
}
return $value;
}