cart || ! WC()->session ) { return new \WP_Error( 'wacc_session', 'A browser checkout session could not be started.' ); } if ( ! Storage::claim( $cart ) ) { return new \WP_Error( 'wacc_conflict', 'The cart or checkout link changed. Ask the agent for a new link.' ); } $original = WC()->cart->get_cart(); $coupons = WC()->cart->get_applied_coupons(); $removed = WC()->cart->get_removed_cart_contents(); $notices = wc_get_notices(); try { // Customer explicitly consented to replacement. Preserve persistent cart until successful recalculation. WC()->cart->empty_cart( false ); foreach ( $items as $item ) { $product = wc_get_product( $item['product_id'] ); $variation_id = $product->is_type( 'variation' ) ? $product->get_id() : 0; $product_id = $variation_id ? $product->get_parent_id() : $product->get_id(); $attributes = $variation_id ? $product->get_variation_attributes() : array(); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Existing WooCommerce hook; preserve validation by installed product extensions. $valid = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $item['quantity'], $variation_id, $attributes, array() ); if ( ! $valid || is_wp_error( $valid ) || ! WC()->cart->add_to_cart( $product_id, $item['quantity'], $variation_id, $attributes ) ) { throw new \RuntimeException( 'Cart validation failed.' ); } } WC()->cart->calculate_totals(); WC()->session->set_customer_session_cookie( true ); // Calculate_totals persists through WooCommerce's cart session hooks. WC()->session->save_data(); Storage::finish( $cart['id'], true ); return true; } catch ( \Throwable $error ) { WC()->cart->set_cart_contents( $original ); WC()->cart->set_applied_coupons( $coupons ); WC()->cart->set_removed_cart_contents( $removed ); try { WC()->cart->calculate_totals(); WC()->session->save_data(); } finally { wc_set_notices( $notices ); Storage::finish( $cart['id'], false ); } return new \WP_Error( 'wacc_checkout_validation', 'The store could not accept these items. Your previous cart was restored. Use the normal product pages or ask the agent to update the cart and issue a new link.' ); } } public function page( $token ) { nocache_headers(); header( 'Cache-Control: private, no-store, max-age=0' ); header( 'Referrer-Policy: no-referrer' ); header( 'X-Robots-Tag: noindex, nofollow' ); $available = Api::available(); $cart = ! is_wp_error( $available ) ? Storage::for_handoff( $token ) : null; if ( ! $cart ) { wp_die( esc_html( is_wp_error( $available ) ? $available->get_error_message() : 'Checkout link invalid, expired or already used. Ask the agent for a new link.' ), 'Checkout link unavailable', array( 'response' => 410 ) ); } // Guest WordPress nonces are shared. Bind confirmation to a browser-only // HttpOnly secret so another shopper cannot forge a cross-site cart swap. $secure = wp_parse_url( home_url( '/' ), PHP_URL_SCHEME ) === 'https'; $cookie_name = $secure ? '__Host-wacc_review' : 'wacc_review'; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Strict hexadecimal validation below rejects malformed credentials without modifying them. $browser_secret = isset( $_COOKIE[ $cookie_name ] ) && is_string( $_COOKIE[ $cookie_name ] ) ? wp_unslash( $_COOKIE[ $cookie_name ] ) : ''; $browser_secret = preg_match( '/^[a-f0-9]{64}$/D', $browser_secret ) ? $browser_secret : ''; $method = isset( $_SERVER['REQUEST_METHOD'] ) && is_string( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) : 'GET'; if ( ! in_array( $method, array( 'GET', 'POST' ), true ) ) { wp_die( 'GET or POST required.', '', array( 'response' => 405 ) ); } if ( $method === 'GET' && $browser_secret === '' ) { $browser_secret = Storage::token(); setcookie( $cookie_name, $browser_secret, array( 'expires' => time() + Storage::HANDOFF_TTL, 'path' => '/', 'secure' => $secure, 'httponly' => true, 'samesite' => 'Lax' ) ); } $nonce_action = 'wacc_confirm_' . $token . '_' . $browser_secret; $error = null; if ( $method === 'POST' ) { $nonce = isset( $_POST['_wpnonce'] ) && is_string( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : ''; if ( $browser_secret === '' || ! wp_verify_nonce( $nonce, $nonce_action ) ) { wp_die( 'Confirmation expired. Reload the checkout link.', '', array( 'response' => 403 ) ); } $result = self::transfer( $token ); if ( $result === true ) { wp_safe_redirect( wc_get_checkout_url(), 303 ); exit; } $error = $result; } Frontend::head( 'Review your agent cart' ); echo '

Review your agent cart

Check these items before continuing. Your browser cart will be replaced with this selection. Existing cart items and coupons will be removed.

'; if ( $error ) { echo '

' . esc_html( $error->get_error_message() ) . '

'; } else { echo '

Stock and prices will be checked again. Shipping, tax, discounts and the final total are confirmed at checkout. No payment has been taken.

'; if ( ! empty( Settings::get()['paused'] ) ) { echo '

Agent checkout handoffs are currently paused.

'; } else { echo '
'; wp_nonce_field( $nonce_action, '_wpnonce', false ); echo '
'; } } echo '

View my current store cart

'; exit; } }