0 && $key && (int) get_transient( $key ) >= $per_hour ) { return new WP_Error( 'sofrah_board_limit', __( 'Too many orders have been sent from this connection in the last hour. Please phone the restaurant.', 'sofrah-order-board' ), array( 'status' => 429 ) ); } $per_phone = (int) sofrah_board_setting( 'phone_cap' ); $phone = trim( (string) $phone ); if ( $per_phone > 0 && '' !== $phone ) { $open = wc_get_orders( array( 'limit' => $per_phone, 'return' => 'ids', 'billing_phone' => $phone, 'date_created' => '>' . ( time() - DAY_IN_SECONDS ), 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery -- a small, bounded check at checkout. array( 'key' => '_sofrah_board_status', 'value' => SOFRAH_BOARD_OPEN, 'compare' => 'IN', ), ), ) ); if ( count( $open ) >= $per_phone ) { return new WP_Error( 'sofrah_board_limit', __( 'This phone number already has orders waiting at the restaurant. Please wait for them, or phone the restaurant.', 'sofrah-order-board' ), array( 'status' => 429 ) ); } } return true; } // Block checkout: checked inside WooCommerce's checkout step, whatever route the request came in on. add_action( 'woocommerce_store_api_checkout_update_order_from_request', function ( $order, $request ) { if ( ! sofrah_board_is_pickup_method( (string) $request->get_param( 'payment_method' ) ) ) { return; } $ok = sofrah_board_check_limits( $order->get_billing_phone() ); if ( is_wp_error( $ok ) ) { throw new \Automattic\WooCommerce\StoreApi\Exceptions\RouteException( 'sofrah_board_limit', esc_html( $ok->get_error_message() ), 429 ); } }, 11, 2 ); // Classic checkout. add_action( 'woocommerce_after_checkout_validation', function ( $data, $errors ) { if ( ! sofrah_board_is_pickup_method( $data['payment_method'] ?? '' ) ) { return; } $ok = sofrah_board_check_limits( $data['billing_phone'] ?? '' ); if ( is_wp_error( $ok ) ) { $errors->add( 'sofrah_board_limit', $ok->get_error_message() ); } }, 11, 2 ); // Each pickup order placed counts against its address for an hour. add_action( 'sofrah_board_order_placed', function () { $key = sofrah_board_address_key(); if ( $key && ! current_user_can( 'edit_shop_orders' ) ) { set_transient( $key, (int) get_transient( $key ) + 1, HOUR_IN_SECONDS ); } } );