sync_service = $sync_service; } public function get_slug() { return self::SLUG; } public function get_label() { return __( 'WooCommerce', '404-emails' ); } public function is_available() { return class_exists( 'WooCommerce' ); } public function register_hooks() { add_action( 'woocommerce_init', array( $this, 'register_consent_field' ) ); add_action( 'woocommerce_payment_complete', array( $this, 'handle_order_synced' ), 10, 1 ); add_action( 'woocommerce_order_status_completed', array( $this, 'handle_order_synced' ), 10, 1 ); } /** * Registers the Checkout Block consent checkbox - a no-op (and safe on * any WooCommerce version) when the Checkout Block fields API isn't * present, or when the integration is disabled. `location => 'contact'` * and the `'other'` group parameter used to read the value back in * consent_given_for_order() are both taken directly from * WooCommerce's own developer tutorial * ("How to Add Additional Fields to the WooCommerce Checkout Block," * developer.woocommerce.com), not guessed from memory - see * docs/ecommerce-integrations.md for the full citation. */ public function register_consent_field() { if ( ! Settings::is_woocommerce_integration_enabled() ) { return; } if ( ! function_exists( 'woocommerce_register_additional_checkout_field' ) ) { // Checkout Blocks fields API not present on this WooCommerce // version - see the class doc comment's "Checkout Blocks only" // limitation. Orders still sync (without consent) via the // order hooks below. return; } woocommerce_register_additional_checkout_field( array( 'id' => self::CONSENT_FIELD_ID, 'label' => Settings::get_woo_consent_label(), 'location' => 'contact', 'type' => 'checkbox', 'required' => false, ) ); } /** @param int $order_id */ public function handle_order_synced( $order_id ) { if ( ! Settings::is_woocommerce_integration_enabled() ) { return; } if ( ! function_exists( 'wc_get_order' ) ) { return; } $order = wc_get_order( $order_id ); if ( ! $order || ! is_object( $order ) || ! method_exists( $order, 'get_id' ) ) { return; } $this->sync_order( $order ); } /** * Privacy-minimal by design (Part G, and the product's own "not * becoming a CRM" boundary): unlike Event Tickets (where a * contact-only sync has independent product value - the platform * already tracks event registrations), a WooCommerce customer with no * marketing consent has no analogous email-marketing purpose in this * product. If consent was not affirmatively given, or no destination * Form is configured, **nothing is sent to 404 Emails at all** - * no contact-only fallback, per Part G's own explicit "if no * marketing purpose exists without consent, it is acceptable to send * nothing." This is a deliberate divergence from * Event_Tickets_Provider's dispatch shape, not an oversight - see * docs/ecommerce-integrations.md#woocommerce-no-consent-behavior. */ private function sync_order( $order ) { if ( ! $this->consent_given_for_order( $order ) ) { return; } $destination_form = Settings::get_woo_selected_form_public_id(); if ( '' === trim( (string) $destination_form ) ) { // Consent given, but no destination Form configured - nothing // safe to do with consent alone (no contact-only fallback, // per this method's own doc comment). return; } $email = $order->get_billing_email(); if ( empty( $email ) || ! is_email( $email ) ) { return; } $dto = $this->build_dto( $order, $destination_form, $email ); $this->sync_service->send( $dto, $this->get_slug() ); } private function build_dto( $order, $destination_form, $email ) { $idempotency_key = Idempotency::build_key( $this->get_slug(), $order->get_id(), $destination_form ); return new Signup_Dto( $email, $order->get_billing_first_name(), $order->get_billing_last_name(), true, self::build_source_reference( $order->get_id() ), $destination_form, $idempotency_key ); } /** WordPress/WooCommerce order ID only - never a customer name or email (Part N). */ public static function build_source_reference( $order_id ) { return sprintf( 'woocommerce:order-%d', (int) $order_id ); } /** * Reads the Checkout Block consent checkbox's per-order saved value - * never inferred from placing an order, creating an account, entering * a checkout email, or accepting store terms (the critical product * boundary this whole slice exists to enforce). Defaults to false * whenever the Blocks fields service class isn't available (older * WooCommerce, or classic checkout was used - see the class doc * comment) or the field has no recorded value. */ private function consent_given_for_order( $order ) { if ( ! Settings::is_woocommerce_integration_enabled() ) { return false; } if ( ! class_exists( '\Automattic\WooCommerce\Blocks\Package' ) || ! class_exists( '\Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields' ) ) { return false; } try { $checkout_fields = \Automattic\WooCommerce\Blocks\Package::container()->get( \Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields::class ); $value = $checkout_fields->get_field_from_object( self::CONSENT_FIELD_ID, $order, 'other' ); } catch ( \Throwable $e ) { return false; } return self::is_truthy_checkbox_value( $value ); } /** Same defensive normalization as Event_Tickets_Provider::is_truthy_checkbox_value() - a checkbox field's stored value shape is not fully guaranteed across WooCommerce versions. */ private static function is_truthy_checkbox_value( $value ) { if ( is_array( $value ) ) { return ! empty( $value ); } $value = strtolower( trim( (string) $value ) ); return '' !== $value && ! in_array( $value, array( '0', 'no', 'false', 'off' ), true ); } }