0 ) { $tiers[] = array( 'id' => 'free_shipping', 'threshold' => $threshold, 'type' => 'free_shipping', 'amount' => 0, // The zone's minimum is read already in the customer's currency. 'converted' => ! $is_manual, // The method's own rules, so the bar agrees with the rates at checkout. 'ignore_discounts' => (bool) ( $method['ignore_discounts'] ?? false ), 'requires' => (string) ( $method['requires'] ?? 'min_amount' ), ); } } /** * Filters the reward tiers. * * @since 1.0.0 * * @param array $tiers Tiers (id, threshold, type, amount, label). */ $tiers = (array) apply_filters( 'aiolos_reward_tiers', $tiers ); $tiers = array_values( array_filter( $tiers, static function ( $tier ) { return is_array( $tier ) && isset( $tier['threshold'], $tier['type'] ) && (float) $tier['threshold'] > 0; } ) ); // Amounts are set in the shop's base currency; the cart is in the customer's. foreach ( $tiers as $i => $tier ) { $tiers[ $i ]['base_amount'] = (float) ( $tier['base_amount'] ?? ( $tier['amount'] ?? 0 ) ); if ( empty( $tier['converted'] ) ) { $tiers[ $i ]['threshold'] = Currency::convert( (float) $tier['threshold'] ); if ( 'fixed' === $tier['type'] ) { $tiers[ $i ]['amount'] = Currency::convert( (float) ( $tier['amount'] ?? 0 ) ); } $tiers[ $i ]['converted'] = true; } } usort( $tiers, static function ( $a, $b ) { return (float) $a['threshold'] <=> (float) $b['threshold']; } ); return $tiers; } /** * The minimum amount of the "Free shipping" method in the zone of the cart's first package. */ public static function zone_free_shipping_threshold(): float { $method = self::zone_free_shipping(); return (float) ( $method['min'] ?? 0 ); } /** * The "Free shipping" method of the customer's zone that has the lowest minimum amount: * [ min (customer's currency), ignore_discounts, requires ], or null. */ public static function zone_free_shipping(): ?array { static $busy = false; if ( $busy || ! function_exists( 'WC' ) || ! WC()->cart || ! class_exists( '\WC_Shipping_Zones' ) ) { return null; } $busy = true; // The destination alone decides the zone. Built here rather than with // WC_Cart::get_shipping_packages(), whose filters may ask for the rewards again. $customer = WC()->customer; $country = $customer ? (string) $customer->get_shipping_country() : ''; $package = array( 'destination' => array( 'country' => '' !== $country ? $country : WC()->countries->get_base_country(), 'state' => $customer ? (string) $customer->get_shipping_state() : '', 'postcode' => $customer ? (string) $customer->get_shipping_postcode() : '', ), ); $zone = \WC_Shipping_Zones::get_zone_matching_package( $package ); $busy = false; $found = null; foreach ( $zone->get_shipping_methods( true ) as $method ) { if ( 'free_shipping' !== $method->id ) { continue; } // As WC_Shipping_Free_Shipping::is_available(): "coupon" alone has no amount to reach. $requires = (string) $method->get_option( 'requires' ); $amount = Currency::free_shipping_minimum( $method ); if ( $amount > 0 && in_array( $requires, array( 'min_amount', 'either', 'both' ), true ) && ( null === $found || $amount < $found['min'] ) ) { $found = array( 'min' => $amount, 'ignore_discounts' => 'yes' === $method->get_option( 'ignore_discounts' ), 'requires' => $requires, ); } } return $found; } /** * Whether a coupon in the cart grants free shipping. */ public static function has_free_shipping_coupon(): bool { foreach ( WC()->cart ? WC()->cart->get_coupons() : array() as $coupon ) { if ( $coupon->get_free_shipping() ) { return true; } } return false; } /** * The amount that counts towards the rewards: what WooCommerce's free shipping compares * (displayed subtotal after coupons), not counting the reward discount itself. * * @param bool $ignore_discounts Compare before coupons (the free shipping method's own setting). */ public static function qualifying_subtotal( bool $ignore_discounts = false ): float { $cart = WC()->cart; if ( ! $cart ) { return 0.0; } // The same amount WC_Shipping_Free_Shipping::is_available() compares. $total = (float) $cart->get_displayed_subtotal(); if ( ! $ignore_discounts ) { if ( $cart->display_prices_including_tax() ) { $total -= (float) $cart->get_discount_total() + (float) $cart->get_discount_tax(); } else { $total -= (float) $cart->get_discount_total(); } } $total -= self::excluded_amount( $ignore_discounts ); /** * Filters the amount that counts towards the rewards. * * @since 1.0.0 * * @param float $total Amount. * @param \WC_Cart $cart Cart. */ return max( 0.0, (float) apply_filters( 'aiolos_rewards_subtotal', round( $total, wc_get_price_decimals() ), $cart ) ); } /** * The shipping classes whose products do not count towards the bar. * * @return int[] */ public static function excluded_classes(): array { $ids = Settings::get( 'free_shipping_excluded_classes', array() ); return array_values( array_filter( array_map( 'absint', is_array( $ids ) ? $ids : array() ) ) ); } /** * What the products of the excluded shipping classes add to the compared amount (with or * without tax as prices are shown, before or after coupons as the comparison is). * * @param bool $ignore_discounts Before coupons. */ public static function excluded_amount( bool $ignore_discounts = false ): float { $classes = self::excluded_classes(); $cart = WC()->cart; if ( ! $classes || ! $cart ) { return 0.0; } $with_tax = $cart->display_prices_including_tax(); $amount = 0.0; foreach ( $cart->get_cart() as $item ) { $product = $item['data'] ?? null; if ( ! $product instanceof \WC_Product || ! in_array( (int) $product->get_shipping_class_id(), $classes, true ) ) { continue; } if ( $ignore_discounts ) { $amount += (float) ( $item['line_subtotal'] ?? 0 ) + ( $with_tax ? (float) ( $item['line_subtotal_tax'] ?? 0 ) : 0 ); } else { $amount += (float) ( $item['line_total'] ?? 0 ) + ( $with_tax ? (float) ( $item['line_tax'] ?? 0 ) : 0 ); } } return $amount; } /** * Every "Free shipping" method with a minimum amount, in every zone: * [ zone (name), min (shop currency), requires ]. * * @return array */ public static function free_shipping_methods(): array { if ( ! class_exists( '\WC_Shipping_Zones' ) ) { return array(); } $zones = array_map( static function ( $zone ) { return new \WC_Shipping_Zone( (int) $zone['zone_id'] ); }, \WC_Shipping_Zones::get_zones() ); $zones[] = new \WC_Shipping_Zone( 0 ); $found = array(); foreach ( $zones as $zone ) { foreach ( $zone->get_shipping_methods( true ) as $method ) { if ( 'free_shipping' !== $method->id ) { continue; } $requires = (string) $method->get_option( 'requires' ); $min = (float) wc_format_decimal( (string) $method->get_option( 'min_amount', 0 ) ); if ( $min > 0 && in_array( $requires, array( 'min_amount', 'either', 'both' ), true ) ) { $found[] = array( 'zone' => (string) $zone->get_zone_name(), 'min' => $min, 'requires' => $requires, ); } } } return $found; } /** * A warning for the settings screen when the bar and WooCommerce's free shipping disagree, * or ''. */ public static function threshold_warning(): string { static $warning = null; if ( null !== $warning ) { return $warning; } $warning = ''; if ( ! Settings::enabled( 'free_shipping_bar' ) ) { return $warning; } $methods = self::free_shipping_methods(); $manual = Settings::get( 'free_shipping_threshold' ); $fixed = is_numeric( $manual ) && (float) $manual > 0 ? (float) $manual : 0.0; $plain = static function ( float $amount ): string { return html_entity_decode( wp_strip_all_tags( wc_price( $amount ) ), ENT_QUOTES, 'UTF-8' ); }; if ( ! $methods ) { $warning = $fixed > 0 /* translators: %s: the bar's threshold */ ? sprintf( __( 'Warning: none of your shipping zones has a "Free shipping" method with a minimum order amount, so shipping does not become free at %s as the bar says. Add one in WooCommerce → Settings → Shipping.', 'aiolos-checkout-for-woocommerce' ), $plain( $fixed ) ) : __( 'Warning: none of your shipping zones has a "Free shipping" method with a minimum order amount, so the bar has nothing to count towards and stays hidden. Add one in WooCommerce → Settings → Shipping, or enter a threshold.', 'aiolos-checkout-for-woocommerce' ); return $warning; } if ( $fixed <= 0 ) { return $warning; } $differ = array(); foreach ( $methods as $method ) { if ( abs( $method['min'] - $fixed ) >= 0.01 ) { $differ[] = $method['zone'] . ': ' . $plain( $method['min'] ); } } if ( count( $differ ) === count( $methods ) ) { $warning = sprintf( /* translators: 1: the bar's threshold, 2: list of zones and their minimum amounts */ __( 'Warning: the bar\'s threshold (%1$s) is not the minimum of WooCommerce\'s "Free shipping" method (%2$s). Customers could be promised free shipping that checkout does not give. Leave the threshold empty to follow WooCommerce.', 'aiolos-checkout-for-woocommerce' ), $plain( $fixed ), implode( '; ', $differ ) ); } return $warning; } /** * Progress of the current cart. * * @return array|null Null when there are no tiers. Keys: tiers (each with unlocked, and the * title and inline labels), subtotal, remaining, next (tier or null), * percent, max. */ public static function progress(): ?array { if ( ! function_exists( 'WC' ) || ! WC()->cart ) { return null; } $key = WC()->cart->get_cart_hash() . '|' . Currency::current(); if ( array_key_exists( $key, self::$cache ) ) { return self::$cache[ $key ]; } $tiers = self::tiers(); if ( ! $tiers ) { self::$cache[ $key ] = null; return null; } // A zone's free shipping that counts before discounts is compared before discounts too. $ignore = false; foreach ( $tiers as $tier ) { $ignore = $ignore || ! empty( $tier['ignore_discounts'] ); } $subtotal = self::qualifying_subtotal( $ignore ); $coupon = self::has_free_shipping_coupon(); $next = null; foreach ( $tiers as $i => $tier ) { $met = $subtotal >= (float) $tier['threshold']; switch ( $tier['requires'] ?? 'min_amount' ) { case 'both': $unlocked = $met && $coupon; break; case 'either': $unlocked = $met || $coupon; break; default: $unlocked = $met; } $tiers[ $i ]['unlocked'] = $unlocked; $tiers[ $i ]['title'] = self::reward_label( $tier ); $tiers[ $i ]['inline'] = self::reward_label( $tier, true ); if ( null === $next && ! $tiers[ $i ]['unlocked'] ) { $next = $tiers[ $i ]; } } $max = (float) end( $tiers )['threshold']; $progress = array( 'tiers' => $tiers, 'subtotal' => $subtotal, 'next' => $next, 'remaining' => $next ? max( 0.0, (float) $next['threshold'] - $subtotal ) : 0.0, 'percent' => $max > 0 ? min( 100.0, round( $subtotal / $max * 100, 1 ) ) : 100.0, 'max' => $max, ); /** * Filters the reward progress of the cart. * * @since 1.0.0 * * @param array $progress Progress. */ self::$cache[ $key ] = (array) apply_filters( 'aiolos_reward_progress', $progress ); return self::$cache[ $key ]; } /** * What a tier gives, as words ("free shipping", "5,00€ off", "10% off"). * * @param array $tier Tier. * @param bool $inline For use inside a sentence ("You're 5€ away from free shipping"). */ public static function reward_label( array $tier, bool $inline = false ): string { if ( ! empty( $tier['label'] ) ) { return (string) $tier['label']; } /** * Filters the words for a reward (for tier types added by the Pro add-on, such as a gift). * * @since 1.1.0 * * @param string|null $label Words, or null for the default. * @param array $tier Tier. * @param bool $inline For use inside a sentence. */ $label = apply_filters( 'aiolos_reward_label', null, $tier, $inline ); if ( is_string( $label ) && '' !== $label ) { return $label; } if ( $inline && 'free_shipping' === $tier['type'] ) { return __( 'free shipping', 'aiolos-checkout-for-woocommerce' ); } switch ( $tier['type'] ) { case 'fixed': /* translators: %s: amount, e.g. 5,00€ */ return sprintf( __( '%s off', 'aiolos-checkout-for-woocommerce' ), self::short_price( (float) $tier['amount'] ) ); case 'percent': /* translators: %s: percentage, e.g. 10 */ return sprintf( __( '%s%% off', 'aiolos-checkout-for-woocommerce' ), wc_format_localized_decimal( (string) (float) $tier['amount'] ) ); default: return __( 'Free shipping', 'aiolos-checkout-for-woocommerce' ); } } /** * A price as plain text, without decimals when it is a whole amount: 5€, 7.50€. * * @param float $amount Amount. */ public static function short_price( float $amount ): string { $decimals = abs( $amount - round( $amount ) ) < 0.005 ? 0 : wc_get_price_decimals(); return html_entity_decode( wp_strip_all_tags( wc_price( $amount, array( 'decimals' => $decimals ) ) ), ENT_QUOTES, 'UTF-8' ); } /** * Whether a free shipping tier is unlocked. */ public static function free_shipping_unlocked(): bool { $progress = self::progress(); if ( ! $progress ) { return false; } foreach ( $progress['tiers'] as $tier ) { if ( 'free_shipping' === $tier['type'] && $tier['unlocked'] ) { return true; } } return false; } }