ask_consent_enabled()) { add_action( 'init', array( $this, 'register_block_checkout_field' ), 20 ); add_action( 'woocommerce_admin_order_data_after_billing_address', array( $this, 'show_consent_in_admin' ) ); } } /** * @return void */ public function register_block_checkout_field(): void { if ( ! function_exists( 'woocommerce_register_additional_checkout_field' ) ) { return; } try { woocommerce_register_additional_checkout_field( array( 'id' => self::BLOCK_FIELD_ID, 'label' => $this->get_consent_text(), 'location' => 'order', 'type' => 'checkbox', 'required' => false, ) ); } catch ( \Throwable $e ) { return; } } /** * Order meta key used by WooCommerce for `location => order` additional fields. * * @return string */ public static function order_meta_key(): string { return '_wc_other/' . self::BLOCK_FIELD_ID; } /** * @return string */ public function get_consent_text(): string { $text = __( 'I agree to receive order updates and notifications via WhatsApp and SMS', 'afroit-messaging-workflows' ); return apply_filters( 'afromewo_checkout_consent_text', $text ); } /** * @param \WC_Order $order Order. * @return void */ public function show_consent_in_admin( $order ): void { if ( ! $order instanceof \WC_Order ) { return; } $text = self::order_has_consent( $order->get_id() ) ? __( 'Yes', 'afroit-messaging-workflows' ) : __( 'No', 'afroit-messaging-workflows' ); echo '
' . esc_html__( 'Messaging consent', 'afroit-messaging-workflows' ) . ': ' . esc_html( $text ) . '
'; } /** * Whether the customer opted in (checkbox stored as `1` / `0` on the order). * * @param int $order_id Order ID. * @return bool */ public static function order_has_consent( $order_id ): bool { if (empty($order_id)) { return true; } $order = wc_get_order( $order_id ); if ( ! $order instanceof \WC_Order ) { return false; } $value = $order->get_meta( self::order_meta_key(), true ); Log::info( 'Checkout consent value', [ 'order_id' => $order_id, 'value' => $value ] ); return ( '1' === (string) $value || true === $value || 1 === $value ); } }