/*
Plugin Name: Quick Product Updater
Description: A simple plugin to update WooCommerce product details quickly using barcode/SKU search.
Version: 1.0
Author: Your Name
*/
if (!defined('ABSPATH')) exit; // Exit if accessed directly
class Quick_Product_Updater {
public function __construct() {
add_action('admin_menu', [$this, 'add_plugin_menu']);
add_action('admin_enqueue_scripts', [$this, 'enqueue_assets']);
add_action('wp_ajax_qpu_search_product', [$this, 'search_product']);
add_action('wp_ajax_qpu_update_product', [$this, 'update_product']);
}
public function add_plugin_menu() {
add_menu_page('Quick Product Updater', 'Product Updater', 'manage_woocommerce', 'quick-product-updater', [$this, 'render_plugin_page'], 'dashicons-edit', 58);
}
public function enqueue_assets($hook) {
if ($hook !== 'toplevel_page_quick-product-updater') return;
wp_enqueue_style('qpu-style', plugin_dir_url(__FILE__) . 'style.css');
wp_enqueue_script('qpu-script', plugin_dir_url(__FILE__) . 'script.js', ['jquery'], false, true);
wp_localize_script('qpu-script', 'qpu_ajax', [
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('qpu_nonce')
]);
}
public function render_plugin_page() {
echo '
';
echo '
Quick Product Updater
';
echo '
';
echo '
';
echo '
';
echo '
';
}
public function search_product() {
check_ajax_referer('qpu_nonce');
$search_term = sanitize_text_field($_POST['term'] ?? '');
$args = [
'post_type' => 'product',
'posts_per_page' => 10,
's' => $search_term
];
$query = new WP_Query($args);
$products = [];
foreach ($query->posts as $product_post) {
$product = wc_get_product($product_post->ID);
$products[] = [
'id' => $product->get_id(),
'name' => $product->get_name(),
'sku' => $product->get_sku(),
'stock' => $product->get_stock_quantity(),
'regular_price' => $product->get_regular_price(),
'sale_price' => $product->get_sale_price(),
'weight' => $product->get_weight(),
'image' => wp_get_attachment_image_url($product->get_image_id(), 'thumbnail')
];
}
wp_send_json_success($products);
}
public function update_product() {
check_ajax_referer('qpu_nonce');
$id = intval($_POST['id']);
$product = wc_get_product($id);
if (!$product) wp_send_json_error('Product not found');
$product->set_stock_quantity(intval($_POST['stock']));
$product->set_regular_price(sanitize_text_field($_POST['regular_price']));
$product->set_sale_price(sanitize_text_field($_POST['sale_price']));
$product->set_weight(sanitize_text_field($_POST['weight']));
if (!empty($_POST['image_id'])) {
$product->set_image_id(intval($_POST['image_id']));
}
$product->save();
wp_send_json_success('Product updated successfully');
}
}
new Quick_Product_Updater();
Shop - AXD Gorilla