Merge pull request #25946 from woocommerce/fix/25928

Send dummy parameters so that cost is evaluated correctly.
This commit is contained in:
Peter Fabian 2020-03-17 16:51:36 +01:00 committed by GitHub
commit 9431b34f0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View File

@ -318,7 +318,7 @@ if ( ! class_exists( 'WC_Eval_Math', false ) ) {
*/
private static function trigger( $msg ) {
self::$last_error = $msg;
if ( Constants::is_true( 'WP_DEBUG' ) ) {
if ( ! Constants::is_true( 'DOING_AJAX' ) && Constants::is_true( 'WP_DEBUG' ) ) {
echo "\nError found in:";
self::debugPrintCallingFunction();
trigger_error( $msg, E_USER_WARNING );

View File

@ -55,10 +55,15 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
* Evaluate a cost from a sum/string.
*
* @param string $sum Sum of shipping.
* @param array $args Args.
* @param array $args Args, must contain `cost` and `qty` keys. Having `array()` as default is for back compat reasons.
* @return string
*/
protected function evaluate_cost( $sum, $args = array() ) {
// Add warning for subclasses.
if ( ! is_array( $args ) || ! array_key_exists( 'qty', $args ) || ! array_key_exists( 'cost', $args ) ) {
wc_doing_it_wrong( __FUNCTION__, '$args must contain `cost` and `qty` keys.', '4.0.1' );
}
include_once WC()->plugin_path() . '/includes/libraries/class-wc-eval-math.php';
// Allow 3rd parties to process shipping cost arguments.
@ -264,7 +269,14 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
$value = wp_kses_post( trim( wp_unslash( $value ) ) );
$value = str_replace( array( get_woocommerce_currency_symbol(), html_entity_decode( get_woocommerce_currency_symbol() ) ), '', $value );
// Thrown an error on the front end if the evaluate_cost will fail.
if ( false === $this->evaluate_cost( $value ) ) {
$dummy_cost = $this->evaluate_cost(
$value,
array(
'cost' => 1,
'qty' => 1,
)
);
if ( false === $dummy_cost ) {
throw new Exception( WC_Eval_Math::$last_error );
}
return $value;