api = $api; add_action( 'woocommerce_order_status_completed', [ $this, 'on_order_completed' ] ); add_action( 'woocommerce_order_status_cancelled', [ $this, 'on_order_cancelled' ] ); add_action( 'user_register', [ $this, 'on_user_register' ] ); } /** * Sync billing email to phpList when an order is marked completed. * * @param int $order_id WooCommerce order ID. */ public function on_order_completed( int $order_id ): void { WooList_Logger::debug( 'Hook fired: order_completed order_id=' . $order_id ); if ( get_option( 'woolist_sync_completed' ) !== 'yes' ) { WooList_Logger::debug( 'Completed order sync disabled, skipping.' ); return; } $list_id = (int) get_option( 'woolist_completed_list_id', 0 ); if ( $list_id < 1 ) { WooList_Logger::error( 'Completed order sync enabled but no list ID configured order_id=' . $order_id ); return; } $order = wc_get_order( $order_id ); if ( ! $order ) { WooList_Logger::error( 'Could not load order order_id=' . $order_id ); return; } $email = $order->get_billing_email(); if ( ! is_email( $email ) ) { WooList_Logger::error( 'Order has no valid billing email order_id=' . $order_id ); return; } WooList_Logger::info( 'Syncing completed order order_id=' . $order_id . ' email=' . $email . ' list_id=' . $list_id ); $this->api->subscribe_email_to_list( $email, $list_id ); } /** * Sync billing email to phpList when an order is cancelled. * * @param int $order_id WooCommerce order ID. */ public function on_order_cancelled( int $order_id ): void { WooList_Logger::debug( 'Hook fired: order_cancelled order_id=' . $order_id ); if ( get_option( 'woolist_sync_cancelled' ) !== 'yes' ) { WooList_Logger::debug( 'Cancelled order sync disabled, skipping.' ); return; } $list_id = (int) get_option( 'woolist_cancelled_list_id', 0 ); if ( $list_id < 1 ) { WooList_Logger::error( 'Cancelled order sync enabled but no list ID configured order_id=' . $order_id ); return; } $order = wc_get_order( $order_id ); if ( ! $order ) { WooList_Logger::error( 'Could not load order order_id=' . $order_id ); return; } $email = $order->get_billing_email(); if ( ! is_email( $email ) ) { WooList_Logger::error( 'Order has no valid billing email order_id=' . $order_id ); return; } WooList_Logger::info( 'Syncing cancelled order order_id=' . $order_id . ' email=' . $email . ' list_id=' . $list_id ); $this->api->subscribe_email_to_list( $email, $list_id ); } /** * Sync new user email to phpList when a WordPress account is created. * * @param int $user_id Newly registered user ID. */ public function on_user_register( int $user_id ): void { WooList_Logger::debug( 'Hook fired: user_register user_id=' . $user_id ); if ( get_option( 'woolist_sync_signup' ) !== 'yes' ) { WooList_Logger::debug( 'Account signup sync disabled, skipping.' ); return; } $list_id = (int) get_option( 'woolist_signup_list_id', 0 ); if ( $list_id < 1 ) { WooList_Logger::error( 'Account signup sync enabled but no list ID configured user_id=' . $user_id ); return; } $user = get_userdata( $user_id ); if ( ! $user || ! is_email( $user->user_email ) ) { WooList_Logger::error( 'Could not load user or invalid email user_id=' . $user_id ); return; } WooList_Logger::info( 'Syncing new account user_id=' . $user_id . ' email=' . $user->user_email . ' list_id=' . $list_id ); $this->api->subscribe_email_to_list( $user->user_email, $list_id ); } }