JP Mall | Vendor Product Upload Panel
No products yet. Upload your first product above!
'Login required']);
}
$name = sanitize_text_field($_POST['name']);
$sku = sanitize_text_field($_POST['sku']);
$price = floatval($_POST['price']);
$mrp = floatval($_POST['mrp']);
$category = sanitize_text_field($_POST['category']);
$description = sanitize_textarea_field($_POST['description']);
$vendor_id = get_current_user_id();
if (empty($name) || empty($sku) || $price <= 0) {
wp_send_json_error(['message' => 'Please fill all required fields']);
}
// Handle images
$image_urls = [];
if (!empty($_FILES['images'])) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
foreach ($_FILES['images']['tmp_name'] as $key => $tmp_name) {
if ($_FILES['images']['error'][$key] === UPLOAD_ERR_OK) {
$file = [
'name' => $_FILES['images']['name'][$key],
'type' => $_FILES['images']['type'][$key],
'tmp_name' => $tmp_name,
'error' => 0,
'size' => $_FILES['images']['size'][$key]
];
$attachment_id = media_handle_sideload($file, 0);
if (!is_wp_error($attachment_id)) {
$image_urls[] = wp_get_attachment_url($attachment_id);
}
}
}
}
if (empty($image_urls)) {
wp_send_json_error(['message' => 'At least one product image is required']);
}
$products = get_option('jp_mall_vendor_products', []);
$new_id = 'JP-' . strtoupper(uniqid());
$products[] = [
'id' => $new_id,
'name' => $name,
'sku' => $sku,
'price' => $price,
'mrp' => $mrp,
'category' => $category,
'description' => $description,
'images' => $image_urls,
'vendor_id' => $vendor_id,
'date' => current_time('mysql')
];
update_option('jp_mall_vendor_products', $products);
wp_send_json_success(['message' => 'Product added successfully']);
});
// Get vendor products
add_action('wp_ajax_jp_mall_get_vendor_products', function() {
if (!is_user_logged_in()) {
wp_send_json_error(['message' => 'Login required']);
}
$vendor_id = get_current_user_id();
$all_products = get_option('jp_mall_vendor_products', []);
$my_products = array_values(array_filter($all_products, function($p) use ($vendor_id) {
return isset($p['vendor_id']) && $p['vendor_id'] == $vendor_id;
}));
$my_products = array_map(function($p) {
return [
'id' => $p['id'],
'name' => $p['name'],
'sku' => $p['sku'],
'price' => $p['price'],
'category' => $p['category'],
'image' => $p['images'][0] ?? ''
];
}, $my_products);
wp_send_json_success(['products' => $my_products]);
});
// Delete product
add_action('wp_ajax_jp_mall_delete_product', function() {
if (!is_user_logged_in()) {
wp_send_json_error(['message' => 'Login required']);
}
$product_id = sanitize_text_field($_POST['product_id']);
$vendor_id = get_current_user_id();
$products = get_option('jp_mall_vendor_products', []);
$found = false;
foreach ($products as $key => $p) {
if ($p['id'] === $product_id && $p['vendor_id'] == $vendor_id) {
unset($products[$key]);
$found = true;
break;
}
}
if ($found) {
update_option('jp_mall_vendor_products', array_values($products));
wp_send_json_success(['message' => 'Deleted']);
} else {
wp_send_json_error(['message' => 'Not found']);
}
});
}
add_action('init', 'jp_mall_vendor_ajax_handlers');
}
?>