Code Snippets (WPCode Lite) > + Add Snippet > Add Your Custom Code * Tipo: PHP Snippet | Ubicación: Run Everywhere * IMPORTANTE: Al pegar en WPCode Lite, OMITIR la primera línea " Settings > Workspace Settings > API) $beehiiv_pub_id = 'pub_e56ac054-eedd-45c1-baf4-916a1bfbe416'; $beehiiv_api_key = 'tFhn2UFf0rqMhRG2SH1wrBqAvxwAefAea02FwA0URXyEnVGyPnYLsPZv34Wttc8I'; $order = wc_get_order( $order_id ); if ( ! $order ) { return; } // Evitar duplicados si el pedido ya fue sincronizado if ( 'yes' === $order->get_meta( '_ocx_beehiiv_synced' ) ) { return; } // Si aún no se pega la API Key, no hace nada y no rompe nada if ( empty( $beehiiv_api_key ) || strpos( $beehiiv_api_key, 'PEGAR_AQUI' ) !== false ) { return; } $email = sanitize_email( $order->get_billing_email() ); if ( empty( $email ) || ! is_email( $email ) ) { return; } // 2. EXTRAER DATOS DEL COMPRADOR Y DETECTAR PLAN DE LA TRIBU $nombre_completo = trim( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() ); if ( empty( $nombre_completo ) ) { $nombre_completo = 'Miembro La Tribu'; } $total_cop = (float) $order->get_total(); $nombres_item = array(); foreach ( $order->get_items() as $item ) { $nombres_item[] = strtolower( $item->get_name() ); } $texto_items = implode( ' ', $nombres_item ); $plan_nombre = 'Aporte Libre'; $plan_tag = ''; if ( strpos( $texto_items, 'escarabaja' ) !== false || strpos( $texto_items, 'padilla' ) !== false || strpos( $texto_items, 'libro' ) !== false || $total_cop >= 115000 ) { $plan_nombre = 'Soy Escarabaja'; $plan_tag = 'plan-escarabaja'; } elseif ( strpos( $texto_items, 'chig' ) !== false || ( $total_cop >= 80000 && $total_cop < 115000 ) ) { $plan_nombre = 'Soy Chigüiro'; $plan_tag = 'plan-chiguiro'; } elseif ( strpos( $texto_items, 'jaguar' ) !== false || ( $total_cop >= 45000 && $total_cop < 80000 ) ) { $plan_nombre = 'Soy Jaguar'; $plan_tag = 'plan-jaguar'; } // Fechas de compra y vencimiento anual (+365 días) en formato ISO YYYY-MM-DD $fecha_compra = current_time( 'Y-m-d' ); $fecha_vencimiento = gmdate( 'Y-m-d', strtotime( $fecha_compra . ' +365 days' ) ); $pedido_ref = '#' . $order->get_order_number(); $headers = array( 'Authorization' => 'Bearer ' . $beehiiv_api_key, 'Content-Type' => 'application/json', 'Accept' => 'application/json', ); // 3. CREAR O ACTUALIZAR SUSCRIPTOR + CUSTOM FIELDS EN BEEHIIV $sub_url = 'https://api.beehiiv.com/v2/publications/' . $beehiiv_pub_id . '/subscriptions'; $sub_body = array( 'email' => $email, 'reactivate_existing'=> true, 'send_welcome_email' => false, 'utm_source' => 'woocommerce', 'utm_medium' => 'checkout', 'utm_campaign' => 'la_tribu_auto', 'custom_fields' => array( array( 'name' => 'nombre_completo', 'value' => $nombre_completo ), array( 'name' => 'plan_tribu', 'value' => $plan_nombre ), array( 'name' => 'estado_membresia', 'value' => 'Activa' ), array( 'name' => 'fecha_ultima_compra', 'value' => $fecha_compra ), array( 'name' => 'fecha_vencimiento', 'value' => $fecha_vencimiento ), array( 'name' => 'ultimo_pedido_wc', 'value' => $pedido_ref ), ), ); $response = wp_remote_post( $sub_url, array( 'headers' => $headers, 'body' => wp_json_encode( $sub_body ), 'timeout' => 5, ) ); if ( is_wp_error( $response ) ) { $order->add_order_note( '[OCX → Beehiiv] Aviso: no se pudo conectar con Beehiiv (' . $response->get_error_message() . '). El pedido no se vio afectado.' ); return; } $code = wp_remote_retrieve_response_code( $response ); $data = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $code < 200 || $code >= 300 || empty( $data['data']['id'] ) ) { $order->add_order_note( '[OCX → Beehiiv] Aviso HTTP ' . $code . ': revisar API Key de Beehiiv. El pedido no se vio afectado.' ); return; } $subscriber_id = sanitize_text_field( $data['data']['id'] ); // 4. ASIGNAR ETIQUETAS (TAGS) AUTOMÁTICAS PARA EL SEGMENTO DINÁMICO $tags_to_add = array( 'la-tribu-activo' ); if ( ! empty( $plan_tag ) ) { $tags_to_add[] = $plan_tag; } $tags_url = 'https://api.beehiiv.com/v2/publications/' . $beehiiv_pub_id . '/subscriptions/' . $subscriber_id . '/tags'; wp_remote_post( $tags_url, array( 'headers' => $headers, 'body' => wp_json_encode( array( 'tags' => $tags_to_add ) ), 'timeout' => 5, ) ); // 5. MARCAR COMO SINCRONIZADO Y DEJAR CONSTANCIA VISIBLE PARA PAOLA $order->update_meta_data( '_ocx_beehiiv_synced', 'yes' ); $order->update_meta_data( '_ocx_beehiiv_sub_id', $subscriber_id ); $order->save(); $order->add_order_note( sprintf( '[OCX → Beehiiv] Sincronizado OK (%s) | Plan: %s | Vence: %s | Etiquetas: %s', $email, $plan_nombre, $fecha_vencimiento, implode( ', ', $tags_to_add ) ) ); } catch ( Throwable $e ) { // Silencioso: jamás interrumpe el flujo de compra en WooCommerce if ( function_exists( 'error_log' ) ) { error_log( '[OCX Beehiiv Sync] ' . $e->getMessage() ); } } } add_action( 'woocommerce_payment_complete', 'ocx_cp_sync_order_to_beehiiv', 20, 1 ); add_action( 'woocommerce_order_status_processing', 'ocx_cp_sync_order_to_beehiiv', 20, 1 ); add_action( 'woocommerce_order_status_completed', 'ocx_cp_sync_order_to_beehiiv', 20, 1 ); } Escríbenos por WhatsApp