getMessage() ); } } /** * The configured opt-in label (with a sane bilingual-safe default). * * @param array $settings Plugin settings. * * @return string */ private static function opt_in_label( array $settings ): string { $label = $settings['abandoned_cart']['opt_in_label'] ?? ''; $label = is_string( $label ) ? trim( $label ) : ''; return '' !== $label ? $label : __( 'Send me order updates and offers on WhatsApp', '6rsh-messaging-notifications' ); } /** * Render the WhatsApp opt-in checkbox on the checkout form. * * @return void */ public static function render_opt_in(): void { try { $settings = get_option( 'sixrsh_settings', array() ); $default = ! isset( $settings['abandoned_cart']['opt_in_default_checked'] ) || ! empty( $settings['abandoned_cart']['opt_in_default_checked'] ); woocommerce_form_field( 'sixrsh_wa_opt_in', array( 'type' => 'checkbox', 'class' => array( 'sixrsh-wa-opt-in form-row-wide' ), 'label' => self::opt_in_label( $settings ), 'required' => false, ), $default ? 1 : 0 ); } catch ( \Throwable $e ) { error_log( '[6rsh WhatsApp] Checkout::render_opt_in exception: ' . $e->getMessage() ); } } /** * Persist the opt-in choice as order meta when the order is created. * * @param \WC_Order $order The order being created. * @param array $data Posted checkout data. * * @return void */ public static function save_opt_in_to_order( $order, $data ): void { try { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- WooCommerce verifies its own checkout nonce before this runs. $opt_in = isset( $_POST['sixrsh_wa_opt_in'] ) ? '1' : '0'; $order->update_meta_data( '_sixrsh_consent', $opt_in ); $order->update_meta_data( '_sixrsh_wa_opt_in', $opt_in ); } catch ( \Throwable $e ) { error_log( '[6rsh WhatsApp] Checkout::save_opt_in_to_order exception: ' . $e->getMessage() ); } } /** * Register the opt-in / consent checkboxes for Block Checkout, which ignores * the classic woocommerce_review_order_before_submit and * woocommerce_checkout_after_customer_details hooks. Needs WC 8.9+; older * stores keep the classic checkout and its fields. * * @return void */ public static function register_block_fields(): void { try { if ( ! function_exists( 'woocommerce_register_additional_checkout_field' ) ) { return; } $settings = get_option( 'sixrsh_settings', array() ); if ( ! empty( $settings['abandoned_cart']['enabled'] ) ) { woocommerce_register_additional_checkout_field( array( 'id' => self::BLOCK_OPT_IN, 'label' => self::opt_in_label( $settings ), 'location' => 'order', 'type' => 'checkbox', ) ); } if ( ! empty( $settings['consent_required'] ) ) { woocommerce_register_additional_checkout_field( array( 'id' => self::BLOCK_CONSENT, 'label' => __( 'I agree to receive order updates via WhatsApp.', '6rsh-messaging-notifications' ), 'location' => 'order', 'type' => 'checkbox', ) ); } } catch ( \Throwable $e ) { error_log( '[6rsh WhatsApp] Checkout::register_block_fields exception: ' . $e->getMessage() ); } } /** * Mirror the Block Checkout checkbox values into the same order meta the * classic checkout writes, so consent checks downstream don't care which * checkout the order came from. * * @param string $key Field id. * @param mixed $value Field value. * @param string $group Field group. * @param mixed $wc_object Order or customer the value is being saved to. * * @return void */ public static function save_block_field( $key, $value, $group, $wc_object ): void { try { if ( ! $wc_object instanceof \WC_Order ) { return; } $flag = $value ? '1' : '0'; if ( self::BLOCK_OPT_IN === $key ) { $wc_object->update_meta_data( '_sixrsh_consent', $flag ); $wc_object->update_meta_data( '_sixrsh_wa_opt_in', $flag ); } elseif ( self::BLOCK_CONSENT === $key ) { $wc_object->update_meta_data( '_sixrsh_consent', $flag ); } } catch ( \Throwable $e ) { error_log( '[6rsh WhatsApp] Checkout::save_block_field exception: ' . $e->getMessage() ); } } /** * Enqueue the live-capture script on the checkout page only. * * @return void */ public static function enqueue(): void { try { if ( ! function_exists( 'is_checkout' ) || ! is_checkout() ) { return; } // Don't run on the order-received / thank-you endpoint. if ( function_exists( 'is_wc_endpoint_url' ) && is_wc_endpoint_url( 'order-received' ) ) { return; } $settings = get_option( 'sixrsh_settings', array() ); wp_enqueue_script( 'sixrsh-checkout-js', SIXRSH_URL . 'public/checkout.js', array(), SIXRSH_VERSION, true ); wp_localize_script( 'sixrsh-checkout-js', 'SixRshCheckout', array( 'endpoint' => esc_url_raw( rest_url( '6rsh/v1/capture-cart' ) ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'requireOptIn' => SixRsh_Cart_Tracker::opt_in_required( $settings ), ) ); } catch ( \Throwable $e ) { error_log( '[6rsh WhatsApp] Checkout::enqueue exception: ' . $e->getMessage() ); } } }