'mode', 'type' => 'select', 'label' => __( 'While on holiday', 'admin-powerups-for-woocommerce' ), 'description' => fn() => $this->status_text(), 'options' => array( 'checkout' => __( 'Pause checkout: shoppers can still fill their cart', 'admin-powerups-for-woocommerce' ), 'purchases' => __( 'Pause purchases: browsing only, no add to cart', 'admin-powerups-for-woocommerce' ), ), 'default' => 'checkout', ), array( 'id' => 'start', 'type' => 'date', 'label' => __( 'From', 'admin-powerups-for-woocommerce' ), 'description' => __( 'Optional. Empty = starts as soon as the power-up is on.', 'admin-powerups-for-woocommerce' ), 'default' => '', ), array( 'id' => 'end', 'type' => 'date', 'label' => __( 'Until (last day closed)', 'admin-powerups-for-woocommerce' ), 'description' => __( 'Optional. Orders open again the next morning. Empty = until you switch it off.', 'admin-powerups-for-woocommerce' ), 'default' => '', ), array( 'id' => 'message', 'type' => 'textarea', 'prose' => true, 'label' => __( 'Message', 'admin-powerups-for-woocommerce' ), 'description' => __( 'Shown in the banner and when someone tries to order. {date} becomes “on” and the day you’re back, or “soon” without an Until date.', 'admin-powerups-for-woocommerce' ), 'default' => __( 'We’re on a short break and not taking orders right now. We’re back {date}.', 'admin-powerups-for-woocommerce' ), ), array( 'id' => 'notice', 'type' => 'toggle', 'label' => __( 'Show a banner on the store', 'admin-powerups-for-woocommerce' ), 'description' => __( 'Uses WooCommerce’s store notice while you’re away.', 'admin-powerups-for-woocommerce' ), 'default' => true, ), ); } /** * {@inheritDoc} */ public function boot() { if ( is_admin() && ! wp_doing_ajax() ) { return; // Staff screens: the store is never closed for them. } add_filter( 'woocommerce_is_purchasable', array( $this, 'purchasable' ), 50, 2 ); add_filter( 'woocommerce_variation_is_purchasable', array( $this, 'purchasable' ), 50, 2 ); add_action( 'woocommerce_load_cart_from_session', array( $this, 'cart_loading' ), 0 ); add_action( 'woocommerce_cart_loaded_from_session', array( $this, 'cart_loaded' ), 0 ); add_action( 'woocommerce_after_checkout_validation', array( $this, 'classic_checkout' ), 50, 2 ); add_action( 'woocommerce_store_api_checkout_update_order_from_request', array( $this, 'block_checkout' ), 50 ); add_action( 'woocommerce_before_pay_action', array( $this, 'pay_for_order' ), 50 ); add_action( 'woocommerce_before_checkout_form', array( $this, 'checkout_notice' ), 5 ); add_filter( 'pre_option_woocommerce_demo_store', array( $this, 'notice_on' ) ); add_filter( 'pre_option_woocommerce_demo_store_notice', array( $this, 'notice_text' ) ); } /** * Start of the first closed day, and start of the day orders open again (store time zone). * * @since 1.0.0 * @return array{0: ?int, 1: ?int} Unix times; null = open-ended. */ public function window() { $zone = wp_timezone(); $start = (string) $this->setting( 'start' ); $end = (string) $this->setting( 'end' ); $from = '' !== $start ? DateTimeImmutable::createFromFormat( '!Y-m-d', $start, $zone ) : false; $until = '' !== $end ? DateTimeImmutable::createFromFormat( '!Y-m-d', $end, $zone ) : false; return array( $from ? $from->getTimestamp() : null, $until ? $until->modify( '+1 day' )->getTimestamp() : null, ); } /** * Whether the store is closed right now. * * @since 1.0.0 * @param int|null $now Unix time (default now). * @return bool */ public function is_closed( $now = null ) { $now = null === $now ? time() : (int) $now; list( $from, $until ) = $this->window(); $closed = ( null === $from || $now >= $from ) && ( null === $until || $now < $until ); /** * Filter whether Holiday mode is closing the store right now. * * @since 1.0.0 * @param bool $closed Closed. * @param int $now Unix time checked. */ return (bool) apply_filters( 'apwc_holiday_mode_closed', $closed, $now ); } /** * The message, with {date} replaced. * * @since 1.0.0 * @return string */ public function message() { $message = trim( (string) $this->setting( 'message' ) ); $message = '' !== $message ? $message : (string) $this->defaults()['message']; $until = $this->window()[1]; $date = $until /* translators: %s: date the store opens again. */ ? sprintf( __( 'on %s', 'admin-powerups-for-woocommerce' ), wp_date( get_option( 'date_format' ), $until ) ) : __( 'soon', 'admin-powerups-for-woocommerce' ); // "back on {date}" (the earlier wording) must not read "back on on 28 December". return str_replace( array( ' on {date}', '{date}' ), array( ' ' . $date, $date ), $message ); } /** * Settings line: is it on now, and when does it start or end. * * @since 1.0.0 * @return string */ public function status_text() { if ( ! $this->is_enabled() ) { return __( 'The store stays open while this power-up is off.', 'admin-powerups-for-woocommerce' ); } list( $from, $until ) = $this->window(); $format = get_option( 'date_format' ); if ( $from && $until && $until <= $from ) { return __( 'The Until date is before From, so the store never closes. Fix the dates.', 'admin-powerups-for-woocommerce' ); } if ( $until && $until <= time() ) { /* translators: %s: date. */ return sprintf( __( 'Ended on %s. The store is open.', 'admin-powerups-for-woocommerce' ), wp_date( $format, $until ) ); } if ( $from && $from > time() ) { /* translators: %s: date. */ return sprintf( __( 'Starts on %s. The store is open until then.', 'admin-powerups-for-woocommerce' ), wp_date( $format, $from ) ); } return $until /* translators: %s: date. */ ? sprintf( __( 'On now: orders are paused until %s.', 'admin-powerups-for-woocommerce' ), wp_date( $format, $until ) ) : __( 'On now: orders are paused until you switch it off.', 'admin-powerups-for-woocommerce' ); } /** * Products can't be bought while closed ("Pause purchases" mode). * * @since 1.0.0 * @param bool $purchasable Purchasable. * @param \WC_Product|null $product Product. * @return bool */ public function purchasable( $purchasable, $product = null ) { if ( ! $purchasable || $this->loading_cart || 'purchases' !== $this->setting( 'mode' ) || ! $this->is_closed() ) { return $purchasable; } // Items already in the cart stay valid when WooCommerce checks the cart (classic and Store // API validate the cart item's own product object), so shoppers aren't told to remove them; // checkout itself is refused while closed. Anywhere else (product page, shop, add to cart) // works on a fresh product object, so there's no Add to cart button and no adding more. if ( $product instanceof \WC_Product && function_exists( 'WC' ) && WC()->cart ) { foreach ( WC()->cart->get_cart_contents() as $item ) { if ( ( $item['data'] ?? null ) === $product ) { return $purchasable; } } } return false; } /** * WooCommerce drops unpurchasable items when it restores a cart; keep them for later. * * @since 1.0.0 * @return void */ public function cart_loading() { $this->loading_cart = true; } /** * Cart restored. * * @since 1.0.0 * @return void */ public function cart_loaded() { $this->loading_cart = false; } /** * Classic checkout: refuse the order. * * @since 1.0.0 * @param array $data Posted data. * @param WP_Error $errors Errors. * @return void */ public function classic_checkout( $data, $errors ) { if ( $errors instanceof WP_Error && $this->is_closed() ) { $errors->add( 'apwc_holiday', $this->message() ); } } /** * Block checkout: refuse the order. * * @since 1.0.0 * @return void * @throws RouteException While closed. */ public function block_checkout() { if ( $this->is_closed() ) { throw new RouteException( 'apwc_holiday', esc_html( $this->message() ), 403 ); } } /** * Classic "pay for order" page: refuse payment too (the block pay route is covered by block_checkout()). * * @since 1.0.0 * @return void */ public function pay_for_order() { if ( $this->is_closed() ) { wc_add_notice( esc_html( $this->message() ), 'error' ); } } /** * Classic checkout page: say so before the form, unless the banner already does. * * @since 1.0.0 * @return void */ public function checkout_notice() { if ( $this->is_closed() && ! $this->setting( 'notice' ) && function_exists( 'wc_print_notice' ) ) { wc_print_notice( esc_html( $this->message() ), 'notice' ); } } /** * Turn on WooCommerce's store notice while closed. * * @since 1.0.0 * @param mixed $value Pre-option value. * @return mixed */ public function notice_on( $value ) { return $this->setting( 'notice' ) && $this->is_closed() ? 'yes' : $value; } /** * Our message in the store notice while closed. * * @since 1.0.0 * @param mixed $value Pre-option value. * @return mixed */ public function notice_text( $value ) { return $this->setting( 'notice' ) && $this->is_closed() ? $this->message() : $value; } }