is_type( array( 'variable', 'grouped' ) ); /** * Filters whether Strikewise records prices for a product. * * @param bool $trackable * @param \WC_Product $product */ return (bool) apply_filters( 'strikewise_is_trackable', $trackable, $product ); } /** * @return array{price: float, regular: float|null, reduced: bool}|null Null if the product has no price. */ public static function snapshot( \WC_Product $product ): ?array { $regular = $product->get_regular_price( 'edit' ); $sale = $product->get_sale_price( 'edit' ); $current = $product->get_price( 'edit' ); if ( '' === $regular && '' === $current ) { return null; } // is_on_sale() checks the sale price and the scheduled sale dates against "now". $reduced = '' !== $sale && $product->is_on_sale( 'edit' ); $effective = $reduced ? (float) $sale : (float) ( '' !== $regular ? $regular : $current ); return array( 'price' => self::to_gross( $product, $effective ), 'regular' => '' === $regular ? null : self::to_gross( $product, (float) $regular ), 'reduced' => $reduced, ); } /** * Price as entered in WooCommerce -> final price incl. VAT at the base location. */ public static function to_gross( \WC_Product $product, float $amount ): float { if ( ! wc_tax_enabled() || wc_prices_include_tax() || ! $product->is_taxable() ) { return round( $amount, 4 ); } $rates = \WC_Tax::get_base_tax_rates( $product->get_tax_class( 'unfiltered' ) ); $taxes = \WC_Tax::calc_tax( $amount, $rates, false ); return round( $amount + array_sum( $taxes ), 4 ); } /** * Final price incl. VAT -> the basis prices are entered in (inverse of to_gross()). */ public static function from_gross( \WC_Product $product, float $gross ): float { if ( ! wc_tax_enabled() || wc_prices_include_tax() || ! $product->is_taxable() ) { return $gross; } $rates = \WC_Tax::get_base_tax_rates( $product->get_tax_class( 'unfiltered' ) ); $taxes = \WC_Tax::calc_tax( $gross, $rates, true ); return $gross - array_sum( $taxes ); } /** * Stored gross price -> the way the shop displays prices (incl. or excl. VAT). */ public static function to_display( \WC_Product $product, float $gross ): float { if ( ! wc_tax_enabled() || ! $product->is_taxable() || 'excl' !== get_option( 'woocommerce_tax_display_shop' ) ) { return $gross; } $rates = \WC_Tax::get_base_tax_rates( $product->get_tax_class( 'unfiltered' ) ); $taxes = \WC_Tax::calc_tax( $gross, $rates, true ); return $gross - array_sum( $taxes ); } }