Build a single-file WordPress plugin that enhances WooCommerce checkout with an express one-click purchase flow and shipping autofill, while leveraging existing payment gateways for Apple Pay / Google Pay.
Context: Site has WooCommerce and WooPayments active. WooPayments provides Apple Pay/Google Pay via Payment Request Buttons (PRB). We should not implement payment processing ourselves; instead integrate with WooPayments PRB where available.
Core features:
- Express / One-click purchase button on single product pages:
- Add a button labeled "Buy Now" near the add-to-cart form (hook into woocommerce_after_add_to_cart_button).
- On click, perform an add-to-cart for the currently selected variation (if variable product) and then redirect to checkout with a URL parameter like ?p0_express=1.
- Use a small inline JS (enqueued) to submit the add-to-cart form via normal WooCommerce mechanism, but then force redirect to checkout. The simplest robust approach:
- Add hidden input in the product form when express mode is requested.
- Add a separate submit button with name="p0_express_buy_now" so server can detect.
- On submit, let WooCommerce handle add-to-cart. Use filter/redirect after add to cart to send user to checkout if this express submit was used.
- Implement: use filter 'woocommerce_add_to_cart_redirect' to redirect to wc_get_checkout_url() with a query var preserving express flag.
- Ensure it works for simple and variable products. For variable products, rely on standard variation selection; submission posts variation_id etc.
- Only show for purchasable, in stock products.
- Apple Pay / Google Pay integration:
- Since WooPayments is active, ensure Payment Request Buttons appear prominently.
- Provide admin setting to optionally reposition WooPayments PRB (if possible) or just encourage enabling.
- Implement a compatibility layer:
- Detect if WooPayments gateway class exists (WooPayments) and PRB hooks exist; if present, add a notice-free integration.
- Provide a product-page hook that prints a small wrapper container before our Buy Now button where PRB can render (some gateways hook automatically). To avoid breaking, we should not call WooPayments internal methods.
- Add a settings page under WooCommerce > Settings > Payments (or a small submenu under WooCommerce) titled "Express Checkout" with:
- Enable/disable Buy Now button.
- Button label.
- Option "Show helper notice if WooPayments PRB not enabled".
- Frontend: if WooPayments is not active, optionally show an admin-only notice in wp-admin about needing WooPayments for Apple Pay/Google Pay.
- Auto-filled shipping details:
- On checkout page load, if user is logged in and has a shipping address saved in user meta, prefill checkout fields.
- Use 'woocommerce_checkout_get_value' filter to supply defaults for shipping_* and billing_* fields when empty.
- Also support WooCommerce customer session values.
- Include a setting "Autofill shipping for logged-in customers".
- Respect user-entered values: only autofill when the field is empty.
- Express checkout optimization:
- If express flag present (?p0_express=1), optionally hide cart page redirection and encourage direct checkout.
- Add filter to skip cart page if any plugin tries to redirect.
- Security and best practices:
- Use nonce on express submit? Since it’s a normal add-to-cart post, WooCommerce already uses nonce for some forms; still add our own nonce field and verify on redirect logic.
- Sanitize and escape.
- Use options stored in wp_options with defaults.
- Provide i18n-ready strings.
- UX:
- Minimal CSS for button styling matching WooCommerce button classes.
- Ensure button uses class 'button alt' and is accessible.
- Logging:
- Add optional debug logging with p0_debug when redirecting in express flow (only if WP_DEBUG true or a plugin option 'debug'). But per requirements, p0_debug can be used without checks; still guard with option.
Deliverable: single PHP plugin file with:
- Plugin header.
- Constants.
- Activation hook setting defaults.
- Admin settings page.
- Frontend hooks for button, redirect, checkout autofill.
- Small enqueued JS/CSS.
Do not include any external dependencies.