session ) { return; } WC()->session->set( self::FLAG, 1 ); } /** * The cart page is shown. */ public static function unflag(): void { if ( ! self::$running && function_exists( 'WC' ) && WC()->session ) { WC()->session->set( self::FLAG, null ); } } /** * Our checkout page is about to render. */ public static function on_checkout_page(): void { if ( Checkout::active() ) { // The checkout calculates the totals itself right after (Checkout::maybe_take_over()). self::maybe_run( false ); } } /** * Our checkout is updated (quantities changed in the summary). */ public static function on_checkout_update(): void { if ( Checkout::from_our_form() ) { // WooCommerce sets the posted address and shipping choice, then calculates, after this // hook: calculating here would use the old ones. self::maybe_run( false ); } } /** * Fires the cart page's hooks once if the cart changed since. * * @param mixed $recalculate Whether to calculate the totals afterwards (the mini cart's hook * passes nothing: yes). */ public static function maybe_run( $recalculate = true ): void { if ( self::$running || ( is_admin() && ! wp_doing_ajax() ) ) { return; } if ( ! function_exists( 'WC' ) || ! WC()->session || ! WC()->cart || ! WC()->session->get( self::FLAG ) || ! self::wanted() ) { return; } WC()->session->set( self::FLAG, null ); if ( WC()->cart->is_empty() ) { return; } self::$running = true; $notices = function_exists( 'wc_get_notices' ) ? wc_get_notices() : array(); $printer = has_action( 'woocommerce_before_cart', 'woocommerce_output_all_notices' ); if ( false !== $printer ) { remove_action( 'woocommerce_before_cart', 'woocommerce_output_all_notices', $printer ); } // phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment -- WooCommerce's own hooks. do_action( 'woocommerce_cart_updated' ); ob_start(); try { do_action( 'woocommerce_before_cart' ); } finally { ob_end_clean(); } // phpcs:enable if ( false !== $printer ) { add_action( 'woocommerce_before_cart', 'woocommerce_output_all_notices', $printer ); } // A plugin that printed (and so cleared) the notices: they come back for the page. if ( $notices && function_exists( 'wc_set_notices' ) && ! wc_get_notices() ) { wc_set_notices( $notices ); } if ( false !== $recalculate ) { WC()->cart->calculate_totals(); } self::$running = false; } }