has_credentials() ) { return; } $product = wc_get_product( $product_id ); if ( ! $product ) { return; } $this->push( 'product', $this->build_product_payload( $product ) ); } /** * Sync an order by ID. * * @param int $order_id Order ID. */ public function sync_order( $order_id ) { if ( ! $this->has_credentials() ) { return; } $order = wc_get_order( $order_id ); if ( ! $order ) { return; } $this->push( 'order', array( 'id' => $order->get_id(), 'status' => $order->get_status(), 'total' => $order->get_total(), 'email' => $order->get_billing_email(), ) ); } /** * Sync a customer by ID. * * @param int $customer_id Customer user ID. */ public function sync_customer( $customer_id ) { if ( ! $this->has_credentials() ) { return; } $customer = new WC_Customer( $customer_id ); $this->push( 'customer', array( 'id' => $customer->get_id(), 'email' => $customer->get_email(), 'name' => trim( $customer->get_first_name() . ' ' . $customer->get_last_name() ), ) ); } /** * POST payload to AANYA wc-sync endpoint. * * @param string $type Endpoint segment: product|order|customer. * @param array $data Payload body. * @return true|WP_Error */ public function push( $type, $data ) { $allowed = array( 'product', 'order', 'customer' ); if ( ! in_array( $type, $allowed, true ) ) { return new WP_Error( 'aanya_invalid_type', 'Invalid sync type.' ); } $creds = Aanya_Connection::get_credentials(); $chatbot_id = $creds['chatbot_id']; $embed_token = $creds['embed_token']; if ( '' === $chatbot_id || '' === $embed_token ) { return new WP_Error( 'aanya_missing_credentials', 'Missing credentials.' ); } $body = array_merge( $data, array( 'chatbot_id' => $chatbot_id, 'embed_token' => $embed_token, 'site_url' => home_url( '/' ), ) ); $response = wp_remote_post( trailingslashit( AANYA_WC_SYNC_BASE ) . $type, array( 'timeout' => 8, 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', ), 'body' => wp_json_encode( $body ), ) ); if ( is_wp_error( $response ) ) { return $response; } $code = (int) wp_remote_retrieve_response_code( $response ); if ( $code < 200 || $code >= 300 ) { return new WP_Error( 'aanya_sync_http', sprintf( 'AANYA sync HTTP %d', $code ) ); } return true; } /** * AJAX: batch-push existing products (backfill). */ public function ajax_sync_products() { if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Forbidden.', 'aanya-ai-chatbot' ) ), 403 ); } check_ajax_referer( 'aanya_admin', 'nonce' ); if ( ! class_exists( 'WooCommerce' ) ) { wp_send_json_error( array( 'message' => __( 'WooCommerce is not active.', 'aanya-ai-chatbot' ) ) ); } if ( '1' !== get_option( 'aanya_share_products', '0' ) ) { wp_send_json_error( array( 'message' => __( 'Enable “Share product catalog” and save first.', 'aanya-ai-chatbot' ) ) ); } if ( ! $this->has_credentials() ) { wp_send_json_error( array( 'message' => __( 'Authorize with AANYA first.', 'aanya-ai-chatbot' ) ) ); } $page = isset( $_POST['page'] ) ? max( 1, absint( wp_unslash( $_POST['page'] ) ) ) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified above. $limit = 50; $synced = 0; $failed = 0; $products = wc_get_products( array( 'limit' => $limit, 'page' => $page, 'status' => array( 'publish', 'private' ), 'return' => 'ids', ) ); foreach ( $products as $product_id ) { $result = $this->push_product_by_id( (int) $product_id ); if ( true === $result ) { ++$synced; } else { ++$failed; } } $has_more = count( $products ) === $limit; wp_send_json_success( array( 'page' => $page, 'synced' => $synced, 'failed' => $failed, 'has_more' => $has_more, 'next' => $has_more ? $page + 1 : null, 'message' => sprintf( /* translators: 1: synced count, 2: page number */ __( 'Synced %1$d products on page %2$d.', 'aanya-ai-chatbot' ), $synced, $page ), ) ); } /** * Build product payload and push (used by AJAX backfill). * * @param int $product_id Product ID. * @return true|WP_Error */ private function push_product_by_id( $product_id ) { $product = wc_get_product( $product_id ); if ( ! $product ) { return new WP_Error( 'aanya_missing_product', 'Product not found.' ); } return $this->push( 'product', $this->build_product_payload( $product ) ); } /** * Rich product payload for smart catalog search (variants, attributes, images, stock). * * @param WC_Product $product Product. * @return array */ private function build_product_payload( $product ) { $product_id = $product->get_id(); $images = array(); $image_id = $product->get_image_id(); if ( $image_id ) { $src = wp_get_attachment_image_url( $image_id, 'full' ); if ( $src ) { $images[] = array( 'id' => (int) $image_id, 'src' => $src, ); } } foreach ( $product->get_gallery_image_ids() as $gallery_id ) { $src = wp_get_attachment_image_url( $gallery_id, 'full' ); if ( $src ) { $images[] = array( 'id' => (int) $gallery_id, 'src' => $src, ); } } $categories = array(); foreach ( wc_get_product_term_ids( $product_id, 'product_cat' ) as $term_id ) { $term = get_term( $term_id, 'product_cat' ); if ( $term && ! is_wp_error( $term ) ) { $categories[] = array( 'id' => (int) $term->term_id, 'name' => (string) $term->name, 'slug' => (string) $term->slug, ); } } $attributes = array(); foreach ( $product->get_attributes() as $attribute ) { if ( is_a( $attribute, 'WC_Product_Attribute' ) ) { $name = $attribute->get_name(); $options = $attribute->get_options(); if ( $attribute->is_taxonomy() ) { $labels = array(); foreach ( $options as $option_id ) { $term = get_term( $option_id ); if ( $term && ! is_wp_error( $term ) ) { $labels[] = $term->name; } } $options = $labels; $label = wc_attribute_label( $name ); } else { $label = $name; } $attributes[] = array( 'name' => (string) $label, 'slug' => (string) $name, 'options' => array_values( array_map( 'strval', $options ) ), 'variation' => (bool) $attribute->get_variation(), ); } } $variations = array(); if ( $product->is_type( 'variable' ) ) { foreach ( $product->get_children() as $variation_id ) { $variation = wc_get_product( $variation_id ); if ( ! $variation ) { continue; } $attrs = array(); foreach ( $variation->get_attributes() as $attr_key => $attr_value ) { $label = wc_attribute_label( str_replace( 'attribute_', '', (string) $attr_key ) ); $attrs[ $label ] = (string) $attr_value; } $variations[] = array( 'id' => (int) $variation->get_id(), 'title' => (string) $variation->get_name(), 'price' => (string) $variation->get_price(), 'stock_status' => (string) $variation->get_stock_status(), 'available' => $variation->is_in_stock(), 'sku' => (string) $variation->get_sku(), 'attributes' => $attrs, 'url' => get_permalink( $variation->get_id() ), ); } } return array( 'id' => $product_id, 'name' => $product->get_name(), 'type' => $product->get_type(), 'sku' => $product->get_sku(), 'price' => $product->get_price(), 'regular_price' => $product->get_regular_price(), 'sale_price' => $product->get_sale_price(), 'stock_status' => $product->get_stock_status(), 'stock_quantity' => $product->get_stock_quantity(), 'description' => wp_strip_all_tags( $product->get_short_description() ?: $product->get_description() ), 'short_description' => wp_strip_all_tags( $product->get_short_description() ), 'url' => get_permalink( $product_id ), 'permalink' => get_permalink( $product_id ), 'images' => $images, 'image' => $images[0]['src'] ?? '', 'categories' => $categories, 'attributes' => $attributes, 'variations' => $variations, 'tags' => wp_get_post_terms( $product_id, 'product_tag', array( 'fields' => 'names' ) ), ); } }