get_shipping_methods( true ) as $method ) { if ( ! $method instanceof \WC_Shipping_Free_Shipping ) { continue; } $requires = (string) $method->get_option( 'requires', '' ); $amount = (float) wc_format_decimal( (string) $method->get_option( 'min_amount', '0' ) ); if ( ! in_array( $requires, self::AMOUNT_RULES, true ) || $amount <= 0 ) { continue; } $found[] = array( 'amount' => $amount, 'requires' => $requires, 'ignore_discounts' => 'yes' === $method->get_option( 'ignore_discounts', 'no' ), 'title' => wp_strip_all_tags( (string) $method->get_title() ), ); } usort( $found, static function ( $a, $b ) { return $a['amount'] <=> $b['amount']; } ); return $found; } /** * Every shipping zone with the free shipping amount found in it (for the settings screen). * * @return array[] Each: name (string), amount (float|null), requires (string), ignore_discounts (bool). */ public static function zones_summary() { $zones = array(); foreach ( \WC_Shipping_Zones::get_zones() as $data ) { $zones[] = new \WC_Shipping_Zone( (int) $data['zone_id'] ); } $zones[] = new \WC_Shipping_Zone( 0 ); $list = array(); foreach ( $zones as $zone ) { $methods = self::zone_methods( $zone ); $first = $methods ? $methods[0] : null; // The "rest of the world" zone without any method is not worth a line. if ( 0 === (int) $zone->get_id() && ! $first && ! $zone->get_shipping_methods( true ) ) { continue; } $list[] = array( 'name' => 0 === (int) $zone->get_id() ? __( 'Rest of the world', 'cartmeter' ) : wp_strip_all_tags( (string) $zone->get_zone_name() ), 'amount' => $first ? $first['amount'] : null, 'requires' => $first ? $first['requires'] : '', 'ignore_discounts' => $first ? $first['ignore_discounts'] : false, ); } return $list; } /** * Where the shopper's order ships to (the store's country when not known yet). * * @return array Package destination. */ public static function destination() { $customer = function_exists( 'WC' ) ? WC()->customer : null; $destination = array( 'country' => $customer ? (string) $customer->get_shipping_country() : '', 'state' => $customer ? (string) $customer->get_shipping_state() : '', 'postcode' => $customer ? (string) $customer->get_shipping_postcode() : '', 'city' => $customer ? (string) $customer->get_shipping_city() : '', 'address' => $customer ? (string) $customer->get_shipping_address() : '', 'address_2' => $customer ? (string) $customer->get_shipping_address_2() : '', ); if ( '' === $destination['country'] ) { $base = wc_get_base_location(); $destination['country'] = isset( $base['country'] ) ? (string) $base['country'] : ''; $destination['state'] = isset( $base['state'] ) ? (string) $base['state'] : ''; } return $destination; } /** * Whether a valid free-shipping coupon is in the cart. * * @return bool */ public static function has_free_shipping_coupon() { if ( ! function_exists( 'WC' ) || ! WC()->cart ) { return false; } foreach ( WC()->cart->get_coupons() as $coupon ) { if ( $coupon->is_valid() && $coupon->get_free_shipping() ) { return true; } } return false; } /** * The free shipping goal for this shopper. * * @return array|null amount (float), ignore_discounts (bool), requires (string), source ('woocommerce' or 'manual'). */ public static function shipping_goal() { $s = Settings::get(); if ( 'manual' === $s['goal_mode'] ) { $amount = (float) $s['goal_amount']; $goal = $amount > 0 ? array( 'amount' => $amount, 'ignore_discounts' => false, 'requires' => 'min_amount', 'source' => 'manual', ) : null; } else { $goal = null; $zone = \WC_Shipping_Zones::get_zone_matching_package( array( 'destination' => self::destination() ) ); foreach ( self::zone_methods( $zone ) as $method ) { // "Amount AND coupon" only counts once the coupon is in the cart. if ( 'both' === $method['requires'] && ! self::has_free_shipping_coupon() ) { continue; } $goal = array( 'amount' => $method['amount'], 'ignore_discounts' => $method['ignore_discounts'], 'requires' => $method['requires'], 'source' => 'woocommerce', ); break; } } /** * Filters the free shipping goal for the current shopper. * * @param array|null $goal amount, ignore_discounts, requires, source. Null = no free shipping for this shopper. */ $goal = apply_filters( 'fspbar_shipping_goal', $goal ); return is_array( $goal ) && isset( $goal['amount'] ) && (float) $goal['amount'] > 0 ? $goal : null; } /** * The cart amount that counts towards the goal, counted the way WooCommerce's Free shipping method counts it. * * @param bool $ignore_discounts True = before coupon discounts. * @return float */ public static function cart_total( $ignore_discounts ) { $cart = WC()->cart; $total = (float) $cart->get_displayed_subtotal(); if ( ! $ignore_discounts ) { $total -= (float) $cart->get_discount_total(); if ( $cart->display_prices_including_tax() ) { $total -= (float) $cart->get_discount_tax(); } } return round( $total, wc_get_price_decimals() ); } /** * Goals on the bar, smallest first. The free plugin has one: free shipping. Add-ons add more. * * @param array|null $shipping Shipping goal from shipping_goal(). * @return array[] Each: id, type, amount (float), and optional texts. */ private static function goals( $shipping ) { $goals = array(); if ( $shipping ) { $goals[] = array( 'id' => 'shipping', 'type' => 'shipping', 'amount' => (float) $shipping['amount'], // "Amount OR coupon": a free-shipping coupon gives free shipping right away. 'coupon' => 'either' === $shipping['requires'] && self::has_free_shipping_coupon(), ); } /** * Filters the goals on the bar. * * @param array[] $goals Goals: id, type, amount, coupon (reached by a coupon), text_progress, text_done. * @param array|null $shipping The free shipping goal, if any. */ $goals = (array) apply_filters( 'fspbar_goals', $goals, $shipping ); $goals = array_values( array_filter( $goals, static function ( $goal ) { return is_array( $goal ) && isset( $goal['amount'] ) && (float) $goal['amount'] > 0; } ) ); usort( $goals, static function ( $a, $b ) { return (float) $a['amount'] <=> (float) $b['amount']; } ); return $goals; } /** * Where the shopper is now. * * @return array state ('none', 'empty', 'progress' or 'done'), goal (amount the text talks about), total, * remaining, percent (0–100, bar fill), goals (each with reached and position 0–100), next (index or null). */ public static function state() { if ( null !== self::$state ) { return self::$state; } $state = array( 'state' => 'none', 'goal' => 0.0, 'total' => 0.0, 'remaining' => 0.0, 'percent' => 0, 'goals' => array(), 'next' => null, ); if ( function_exists( 'WC' ) && WC()->cart && wc_shipping_enabled() ) { $cart = WC()->cart; $shipping = self::shipping_goal(); // Only digital items: shipping is not needed, so the free shipping goal does not apply. if ( $shipping && ! $cart->is_empty() && ! $cart->needs_shipping() ) { $shipping = null; } $goals = self::goals( $shipping ); if ( $goals ) { $ignore = $shipping ? (bool) $shipping['ignore_discounts'] : false; $total = $cart->is_empty() ? 0.0 : self::cart_total( $ignore ); $max = (float) end( $goals )['amount']; $next = null; foreach ( $goals as $i => $goal ) { $goals[ $i ]['reached'] = ! $cart->is_empty() && ( $total >= (float) $goal['amount'] || ! empty( $goal['coupon'] ) ); $goals[ $i ]['position'] = (int) round( (float) $goal['amount'] / $max * 100 ); if ( null === $next && ! $goals[ $i ]['reached'] ) { $next = $i; } } $state['goals'] = $goals; $state['total'] = $total; if ( $cart->is_empty() ) { $state['state'] = 'empty'; $state['goal'] = (float) $goals[0]['amount']; } elseif ( null === $next ) { $state['state'] = 'done'; $state['goal'] = $max; $state['percent'] = 100; } else { $state['state'] = 'progress'; $state['next'] = $next; $state['goal'] = (float) $goals[ $next ]['amount']; $state['remaining'] = max( 0.0, round( $state['goal'] - $total, wc_get_price_decimals() ) ); $state['percent'] = (int) max( 0, min( 99, floor( $total / $max * 100 ) ) ); } } } /** * Filters where the shopper is now. * * @param array $state See Goal::state(). */ self::$state = (array) apply_filters( 'fspbar_state', $state ); return self::$state; } }