fail( 'MB_PRICE_UNRESOLVED' ); } $on_sale = false; $raw = ''; if ( $product->is_on_sale() ) { $sale = $product->get_sale_price( 'edit' ); if ( '' !== $sale && null !== $sale ) { $raw = (string) $sale; $on_sale = true; } } if ( '' === $raw ) { $regular = $product->get_regular_price( 'edit' ); if ( '' === $regular || null === $regular ) { // Purchasable product without price. return $this->fail( 'MB_PRICE_UNRESOLVED' ); } $raw = (string) $regular; } // Prefer Woo tax-exclusive helper when store prices include tax. if ( function_exists( 'wc_get_price_excluding_tax' ) && function_exists( 'get_option' ) && 'yes' === get_option( 'woocommerce_prices_include_tax' ) ) { try { $ex = wc_get_price_excluding_tax( $product, array( 'qty' => 1, 'price' => $raw, ) ); if ( ! is_numeric( $ex ) ) { return $this->fail( 'MB_TAX_BASIS_UNRESOLVED' ); } $raw = (string) $ex; } catch ( \Throwable $e ) { return $this->fail( 'MB_TAX_BASIS_UNRESOLVED' ); } } try { $amount = MoneyAmount::from_decimal_string( $raw, Constants::DEFAULT_MONEY_SCALE ); if ( null === $amount ) { return $this->fail( 'MB_PRICE_UNRESOLVED' ); } return array( 'amount' => $amount, 'basis' => $on_sale ? 'sale' : 'regular', 'reason_code' => null, 'on_sale' => $on_sale, ); } catch ( MoneyException $e ) { return $this->fail( $e->error_code() ); } } /** * @param string $code Reason. * @return array */ private function fail( $code ) { return array( 'amount' => null, 'basis' => 'unresolved', 'reason_code' => $code, 'on_sale' => false, ); } }