false, 'product_id' => null, 'message' => 'WooCommerce is not active.', 'edit_url' => null ); } // Sanitize status — only allow draft or publish. $status = in_array( $status, array( 'draft', 'publish' ), true ) ? $status : 'draft'; try { $product = new WC_Product_Simple(); // Core fields. $product->set_name( sanitize_text_field( $data['title'] ) ); $product->set_status( $status ); if ( ! empty( $data['description'] ) ) { $product->set_description( wp_kses_post( $data['description'] ) ); } if ( ! empty( $data['short_description'] ) ) { $product->set_short_description( wp_kses_post( $data['short_description'] ) ); } // SKU. if ( ! empty( $data['sku'] ) ) { $product->set_sku( sanitize_text_field( $data['sku'] ) ); } // Pricing. if ( isset( $data['regular_price'] ) && $data['regular_price'] !== '' ) { $product->set_regular_price( wc_format_decimal( $data['regular_price'] ) ); } if ( isset( $data['sale_price'] ) && $data['sale_price'] !== '' ) { $product->set_sale_price( wc_format_decimal( $data['sale_price'] ) ); } // Stock management. $manage_stock = isset( $data['manage_stock'] ) && in_array( strtolower( (string) $data['manage_stock'] ), array( 'yes', '1', 'true' ), true ); $product->set_manage_stock( $manage_stock ); if ( $manage_stock && isset( $data['stock'] ) && $data['stock'] !== '' ) { $product->set_stock_quantity( intval( $data['stock'] ) ); } if ( ! empty( $data['stock_status'] ) ) { $stock_status = in_array( $data['stock_status'], array( 'instock', 'outofstock', 'onbackorder' ), true ) ? $data['stock_status'] : 'instock'; $product->set_stock_status( $stock_status ); } // Weight. if ( isset( $data['weight'] ) && $data['weight'] !== '' ) { $product->set_weight( wc_format_decimal( $data['weight'] ) ); } // Categories — accepts comma-separated names or pipe-separated like the WC CSV format. if ( ! empty( $data['categories'] ) ) { $cat_ids = self::resolve_term_ids( $data['categories'], 'product_cat' ); if ( ! empty( $cat_ids ) ) { $product->set_category_ids( $cat_ids ); } } // Tags — same separator support. if ( ! empty( $data['tags'] ) ) { $tag_ids = self::resolve_term_ids( $data['tags'], 'product_tag' ); if ( ! empty( $tag_ids ) ) { $product->set_tag_ids( $tag_ids ); } } // Save to get an ID before attaching images. $product_id = $product->save(); if ( ! $product_id ) { return array( 'success' => false, 'product_id' => null, 'message' => 'Failed to save the product to the database.', 'edit_url' => null ); } // Images — comma-separated URLs; first becomes featured image, rest become gallery. if ( ! empty( $data['images'] ) ) { self::attach_product_images( $product_id, $data['images'] ); } self::log_debug( 'Product created', array( 'product_id' => $product_id, 'status' => $status, 'title' => $data['title'] ) ); return array( 'success' => true, 'product_id' => $product_id, 'message' => sprintf( 'Product %s successfully (ID: %d).', $status === 'publish' ? 'published' : 'saved as draft', $product_id ), 'edit_url' => get_edit_post_link( $product_id, 'raw' ), 'view_url' => get_permalink( $product_id ) ?: null, ); } catch ( Exception $e ) { return array( 'success' => false, 'product_id' => null, 'message' => 'Error: ' . $e->getMessage(), 'edit_url' => null ); } } /** * Publishes an existing product draft. * * @param int $product_id * @return array{ success: bool, product_id: int, message: string, edit_url: string|null } */ public static function publish_product( $product_id ) { $product_id = absint( $product_id ); $product = wc_get_product( $product_id ); if ( ! $product ) { return array( 'success' => false, 'product_id' => $product_id, 'message' => 'Product not found.', 'edit_url' => null ); } $product->set_status( 'publish' ); $product->save(); self::log_debug( 'Product published', array( 'product_id' => $product_id ) ); return array( 'success' => true, 'product_id' => $product_id, 'message' => sprintf( 'Product %d published successfully.', $product_id ), 'edit_url' => get_edit_post_link( $product_id, 'raw' ), ); } /** * Updates fields on an existing WooCommerce product. * * Accepts any subset of the same fields supported by create_product(). * Only fields explicitly passed in $data are updated. * * @param int $product_id * @param array $data Associative array of product fields to update. * @return array{ success: bool, product_id: int, message: string, edit_url: string|null } */ public static function update_product( $product_id, array $data ) { $product_id = absint( $product_id ); $product = wc_get_product( $product_id ); if ( ! $product ) { return array( 'success' => false, 'product_id' => $product_id, 'message' => 'Product not found.', 'edit_url' => null ); } try { if ( isset( $data['title'] ) && '' !== $data['title'] ) { $product->set_name( sanitize_text_field( $data['title'] ) ); } if ( isset( $data['description'] ) ) { $product->set_description( wp_kses_post( $data['description'] ) ); } if ( isset( $data['short_description'] ) ) { $product->set_short_description( wp_kses_post( $data['short_description'] ) ); } if ( isset( $data['sku'] ) && '' !== $data['sku'] ) { $product->set_sku( sanitize_text_field( $data['sku'] ) ); } if ( isset( $data['regular_price'] ) && '' !== $data['regular_price'] ) { $product->set_regular_price( wc_format_decimal( $data['regular_price'] ) ); } if ( isset( $data['sale_price'] ) ) { $product->set_sale_price( '' !== $data['sale_price'] ? wc_format_decimal( $data['sale_price'] ) : '' ); } if ( isset( $data['manage_stock'] ) ) { $manage_stock = in_array( strtolower( (string) $data['manage_stock'] ), array( 'yes', '1', 'true' ), true ); $product->set_manage_stock( $manage_stock ); } if ( isset( $data['stock'] ) && '' !== $data['stock'] ) { $product->set_stock_quantity( intval( $data['stock'] ) ); } if ( isset( $data['stock_status'] ) ) { $stock_status = in_array( $data['stock_status'], array( 'instock', 'outofstock', 'onbackorder' ), true ) ? $data['stock_status'] : 'instock'; $product->set_stock_status( $stock_status ); } if ( isset( $data['weight'] ) && '' !== $data['weight'] ) { $product->set_weight( wc_format_decimal( $data['weight'] ) ); } if ( isset( $data['status'] ) && in_array( $data['status'], array( 'draft', 'publish', 'pending', 'private' ), true ) ) { $product->set_status( $data['status'] ); } if ( ! empty( $data['categories'] ) ) { $cat_ids = self::resolve_term_ids( $data['categories'], 'product_cat' ); if ( ! empty( $cat_ids ) ) { $product->set_category_ids( $cat_ids ); } } if ( ! empty( $data['tags'] ) ) { $tag_ids = self::resolve_term_ids( $data['tags'], 'product_tag' ); if ( ! empty( $tag_ids ) ) { $product->set_tag_ids( $tag_ids ); } } if ( ! empty( $data['images'] ) ) { self::attach_product_images( $product_id, $data['images'] ); } $product->save(); self::log_debug( 'Product updated', array( 'product_id' => $product_id ) ); return array( 'success' => true, 'product_id' => $product_id, 'message' => sprintf( 'Product %d updated successfully.', $product_id ), 'edit_url' => get_edit_post_link( $product_id, 'raw' ), ); } catch ( Exception $e ) { return array( 'success' => false, 'product_id' => $product_id, 'message' => 'Error: ' . $e->getMessage(), 'edit_url' => null ); } } /** * Resolves term IDs from comma- or pipe-separated term names/IDs. * Creates terms when needed. * * @param string|int[] $values * @param string $taxonomy * @return int[] */ private static function resolve_term_ids( $values, $taxonomy ) { if ( empty( $values ) || ! taxonomy_exists( $taxonomy ) ) { return array(); } if ( ! is_array( $values ) ) { $values = preg_split( '/[|,]+/', $values ); } $term_ids = array(); foreach ( $values as $value ) { $value = trim( (string) $value ); if ( $value === '' ) { continue; } if ( ctype_digit( $value ) ) { $term_id = absint( $value ); if ( get_term( $term_id, $taxonomy ) ) { $term_ids[] = $term_id; continue; } } $term = term_exists( $value, $taxonomy ); if ( $term && ! is_wp_error( $term ) ) { $term_ids[] = is_array( $term ) ? absint( $term['term_id'] ) : absint( $term ); continue; } $inserted = wp_insert_term( sanitize_text_field( $value ), $taxonomy ); if ( ! is_wp_error( $inserted ) && isset( $inserted['term_id'] ) ) { $term_ids[] = absint( $inserted['term_id'] ); } } return array_values( array_unique( array_filter( $term_ids ) ) ); } /** * Attaches product images from URLs to the given product. * The first image becomes the featured image if the product has none. * * @param int $product_id * @param string|string[] $urls * @return void */ private static function attach_product_images( $product_id, $urls ) { if ( empty( $urls ) ) { return; } if ( ! is_array( $urls ) ) { $urls = preg_split( '/[|,\r\n]+/', $urls ); } $urls = array_filter( array_map( 'trim', $urls ) ); if ( empty( $urls ) ) { return; } require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; $product = wc_get_product( $product_id ); $gallery_ids = $product ? $product->get_gallery_image_ids() : array(); $has_featured = $product ? (bool) $product->get_image_id() : false; $first = true; foreach ( $urls as $url ) { $url = esc_url_raw( $url ); if ( empty( $url ) ) { continue; } $attachment_id = media_sideload_image( $url, $product_id, null, 'id' ); if ( is_wp_error( $attachment_id ) ) { continue; } if ( $first && ! $has_featured && $product ) { $product->set_image_id( $attachment_id ); $has_featured = true; } else { $gallery_ids[] = $attachment_id; } $first = false; } if ( $product ) { if ( ! empty( $gallery_ids ) ) { $product->set_gallery_image_ids( array_unique( $gallery_ids ) ); } $product->save(); } } /** * Deletes a WooCommerce product (moves to trash by default). * * @param int $product_id * @param bool $force_delete True to permanently delete; false to trash. * @return array{ success: bool, product_id: int, message: string } */ public static function delete_product( $product_id, $force_delete = false ) { $product_id = absint( $product_id ); $product = wc_get_product( $product_id ); if ( ! $product ) { return array( 'success' => false, 'product_id' => $product_id, 'message' => 'Product not found.' ); } $result = $force_delete ? wp_delete_post( $product_id, true ) : wp_trash_post( $product_id ); if ( ! $result ) { return array( 'success' => false, 'product_id' => $product_id, 'message' => 'Failed to delete product.' ); } self::log_debug( 'Product deleted', array( 'product_id' => $product_id, 'force' => $force_delete ) ); return array( 'success' => true, 'product_id' => $product_id, 'message' => sprintf( 'Product %d %s.', $product_id, $force_delete ? 'permanently deleted' : 'moved to trash' ), ); } /** * Searches for WooCommerce products by name/keyword. * Used by the chatbot search_product action to resolve names → IDs. * * @param array $input { search_term: string, limit?: int } * @return array List of [ id, name, type, sku, status, edit_url ] */ public static function search_product_id( array $input ) { $term = isset( $input['search_term'] ) ? sanitize_text_field( $input['search_term'] ) : ''; $limit = isset( $input['limit'] ) ? absint( $input['limit'] ) : 10; if ( empty( $term ) ) { return array(); } $products = wc_get_products( array( 's' => $term, 'limit' => $limit, 'status' => array( 'publish', 'draft', 'private' ), ) ); $result = array(); foreach ( $products as $product ) { $result[] = array( 'id' => $product->get_id(), 'name' => $product->get_name(), 'type' => $product->get_type(), 'sku' => $product->get_sku(), 'status' => $product->get_status(), 'edit_url' => get_edit_post_link( $product->get_id(), 'raw' ), ); } return $result; } }