function create_used_product(WP_REST_Request $request) { $base_sku = $request->get_param('sku'); $discount_percentage = $request->get_param('discount'); // Get discount percentage from request $base_product = wc_get_product_id_by_sku($base_sku); if (!$base_product) { return new WP_Error('no_product', 'Base product not found', array('status' => 404)); } $base_product_obj = wc_get_product($base_product); $used_sku_prefix = $base_sku . '-USED-'; // Collect all existing used SKUs for the base product $existing_skus = []; $args = [ 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 'meta_query' => [ [ 'key' => '_sku', 'value' => $used_sku_prefix, 'compare' => 'LIKE' ] ] ]; $query = new WP_Query($args); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $existing_skus[] = get_post_meta(get_the_ID(), '_sku', true); } } wp_reset_postdata(); // Reset post data // Find the next available SKU $i = 1; // Start from 1 for USED-0001 $used_sku = $used_sku_prefix . str_pad($i, 4, '0', STR_PAD_LEFT); while (in_array($used_sku, $existing_skus)) { $i++; $used_sku = $used_sku_prefix . str_pad($i, 4, '0', STR_PAD_LEFT); } // Validate discount percentage if (!is_numeric($discount_percentage) || $discount_percentage <= 0 || $discount_percentage >= 100) { return new WP_Error('invalid_discount', 'Discount percentage must be a number between 0 and 100', array('status' => 400)); } // Calculate the used product price based on the discount percentage $regular_price = $base_product_obj->get_regular_price(); if (empty($regular_price) || !is_numeric($regular_price)) { return new WP_Error('invalid_price', 'Base product price is not set or invalid', array('status' => 400)); } $regular_price = floatval($regular_price); // Ensure it's a float $used_price = $regular_price * (1 - ($discount_percentage / 100)); // Apply discount // Create the new product $used_product = array( 'post_title' => 'Used ' . $base_product_obj->get_name(), 'post_status' => 'publish', 'post_type' => 'product', 'post_excerpt' => $base_product_obj->get_description(), 'post_content' => $base_product_obj->get_description(), // Copy description 'post_excerpt' => $base_product_obj->get_short_description(), // Copy short description ); // Insert the product into the database $product_id = wp_insert_post($used_product); if (!$product_id) { return new WP_Error('product_creation_failed', 'Failed to create product', array('status' => 500)); } // Set the SKU and price update_post_meta($product_id, '_sku', $used_sku); update_post_meta($product_id, '_price', $used_price); update_post_meta($product_id, '_regular_price', $used_price); update_post_meta($product_id, '_discount_percentage', $discount_percentage); // Store discount percentage // Copy product categories $terms = wp_get_post_terms($base_product, 'product_cat', array("fields" => "ids")); if (!empty($terms)) { wp_set_object_terms($product_id, $terms, 'product_cat'); } // Copy stock status $stock_status = $base_product_obj->get_stock_status(); update_post_meta($product_id, '_stock_status', $stock_status); // Copy any additional meta data you need // Example: update_post_meta($product_id, 'custom_meta_key', get_post_meta($base_product, 'custom_meta_key', true)); return array('product_id' => $product_id, 'sku' => $used_sku); }