session ) { return; } parse_str( $post_data, $data ); if ( empty( $data['aiolos_ship_manual'] ) || '1' !== $data['aiolos_ship_manual'] ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- WooCommerce checked the nonce of this refresh before the action; wc_clean() sanitizes every value. $posted = isset( $_POST['shipping_method'] ) && is_array( $_POST['shipping_method'] ) ? wc_clean( wp_unslash( $_POST['shipping_method'] ) ) : array(); $manual = (array) WC()->session->get( self::MANUAL, array() ); foreach ( (array) $posted as $index => $rate ) { if ( is_scalar( $rate ) && '' !== (string) $rate ) { $manual[ (string) $index ] = (string) $rate; } } WC()->session->set( self::MANUAL, $manual ); } /** * After the totals: a package that now offers free shipping, which it did not before, gets it * chosen when its current rate costs something and the customer did not pick that rate. */ public static function choose_free_shipping(): void { if ( self::$running || ! Settings::enabled( 'auto_free_shipping' ) || ! WC()->session || ! WC()->cart || ! ( Checkout::active() || Checkout::from_our_form() ) ) { return; } $packages = WC()->shipping()->get_packages(); if ( ! $packages ) { return; } $seen = (array) WC()->session->get( self::SEEN, array() ); $manual = (array) WC()->session->get( self::MANUAL, array() ); $chosen = (array) WC()->session->get( 'chosen_shipping_methods', array() ); $changed = false; foreach ( $packages as $index => $package ) { $rates = (array) ( $package['rates'] ?? array() ); $free = self::free_rate( $rates ); $had = ! empty( $seen[ $index ] ); // Remembered by package index, as WooCommerce remembers the chosen rates. $seen[ $index ] = '' !== $free; $current = isset( $chosen[ $index ] ) && is_scalar( $chosen[ $index ] ) ? (string) $chosen[ $index ] : ''; if ( '' === $free || $had || $free === $current || ! isset( $rates[ $current ] ) ) { continue; } $by_hand = isset( $manual[ $index ] ) && (string) $manual[ $index ] === $current; if ( $by_hand || (float) $rates[ $current ]->get_cost() <= 0 ) { continue; } $chosen[ $index ] = $free; $changed = true; } // Only the packages that offer free shipping are remembered, and only when that changed: a page // view that changes nothing must not write the session (a write saves the whole session row, // cart included, and could put back a cart another request had just changed). $seen = array_filter( $seen ); $stored = array_filter( (array) WC()->session->get( self::SEEN, array() ) ); if ( $seen != $stored ) { // phpcs:ignore Universal.Operators.StrictComparisons -- same keys and flags, in any order. WC()->session->set( self::SEEN, $seen ? $seen : null ); } if ( $changed ) { WC()->session->set( 'chosen_shipping_methods', $chosen ); self::$running = true; WC()->cart->calculate_totals(); self::$running = false; } } /** * The free shipping rate of a package ("Free shipping" methods), or ''. * * @param array $rates Rate id => WC_Shipping_Rate. */ private static function free_rate( array $rates ): string { foreach ( $rates as $id => $rate ) { if ( ! $rate instanceof \WC_Shipping_Rate ) { continue; } /** * Filters whether a shipping rate counts as the free shipping chosen automatically * when it becomes available. * * @since 1.6.0 * * @param bool $free Whether it does (default: WooCommerce's "Free shipping" method). * @param \WC_Shipping_Rate $rate Rate. */ if ( apply_filters( 'aiolos_auto_free_shipping_rate', 'free_shipping' === $rate->get_method_id(), $rate ) ) { return (string) $id; } } return ''; } }