AI Verified

Name

Inventory Status Change

About

I have a client who sells logs - has less than 10 products and doesn't use the stock management tools in WooCommerce. They wanted to control the Inventory on the basis of 'In Stock' or 'Out of Stock'. They're tree surgeons not particularly IT people. Yes you can go into Products > Inventory and choose in/out of stock - but I wanted a simply admin dashboard widget. As I couldn't find one I asked ChatGPT to write one for me. This widget may be quick and dirty, but it does the job admirably. Now this won't be entirely suitable for all, those with lots of products & variations.

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

Add the code snippet and activate on the admin only - hey presto you'll have it on the Admin Dashboard. Radio buttons for In / Out of stock and a button to update / save the info. I have this working all OK on WP 6.4.1 :)

Codevault

woldsnet_snippets

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

23/11/2023

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Potential vulnerability found : Cross Site Scripting
Found on line : 44
Code : echo($stock_status)
Vulnerable line : 41
Code : $stock_status = get_post_meta($product_id, '_stock_status', true));

Potential vulnerability found : Cross Site Scripting
Found on line : 47
Code : echo($product_id)
Vulnerable line : 71
Code : $inventory_status = isset($_POST['inventory_status']) ? $_POST['inventory_status'] : array());

Potential vulnerability found : Cross Site Scripting
Found on line : 47
Code : echo($product_id)
Vulnerable line : 71
Code : $inventory_status = isset($_POST['inventory_status']) ? $_POST['inventory_status'] : array());

Found 3 vulnerabilities

Inventory Status Change

 
                    
1/**
2 * Plugin Name: Inventory Status Change Dashboard Widget
3 * Description: Custom dashboard widget to display and update the inventory status of products.
4 * Version: 1.0
5 * Author: Phil Davies / CHATGPT - OpenAI
6 */
7 
8// Add the dashboard widget
9function inventory_status_change_dashboard_widget() {
10 wp_add_dashboard_widget(
11 'inventory_status_change_dashboard_widget',
12 'Inventory Status Change',
13 'inventory_status_change_dashboard_widget_content'
14 );
15}
16add_action('wp_dashboard_setup', 'inventory_status_change_dashboard_widget');
17 
18// Dashboard widget content
19function inventory_status_change_dashboard_widget_content() {
20 $products_args = array(
21 'post_type' => 'product',
22 'posts_per_page' => -1,
23 );
24 
25 $products_query = new WP_Query($products_args);
26 
27 if ($products_query->have_posts()) :
28 ?>
29 <div class="inventory-status-change-widget">
30 <h3>Inventory Status Change</h3>
31 <form method="post" action="">
32 <table>
33 <tr>
34 <th>Product</th>
35 <th>Current Status</th>
36 <th>Change Status</th>
37 </tr>
38 <?php while ($products_query->have_posts()) : $products_query->the_post(); ?>
39 <?php
40 $product_id = get_the_ID();
41 $stock_status = get_post_meta($product_id, '_stock_status', true);
42 ?>
43 <tr>
44 <td><?php the_title(); ?></td>
45 <td><?php echo $stock_status; ?></td>
46 <td>
47 <label>
48 <input type="radio" name="inventory_status[<?php echo $product_id; ?>]" value="instock" <?php checked($stock_status, 'instock'); ?>>
49 In Stock
50 </label>
51 <label>
52 <input type="radio" name="inventory_status[<?php echo $product_id; ?>]" value="outofstock" <?php checked($stock_status, 'outofstock'); ?>>
53 Out of Stock
54 </label>
55 </td>
56 </tr>
57 <?php endwhile; ?>
58 </table>
59 <input type="submit" name="update_inventory_status" value="Update Inventory Status">
60 </form>
61 </div>
62 <?php
63 else :
64 echo '<p>No products found.</p>';
65 endif;
66 
67 wp_reset_postdata();
68 
69 // Handle form submission
70 if (isset($_POST['update_inventory_status'])) {
71 $inventory_status = isset($_POST['inventory_status']) ? $_POST['inventory_status'] : array();
72 
73 foreach ($inventory_status as $product_id => $status) {
74 // Use WooCommerce function to update stock status
75 update_post_meta($product_id, '_stock_status', $status);
76 }
77 
78 echo '<p>Inventory status updated successfully!</p>';
79 }
80}

0

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

0

Como Medir Talles

Added: 8 months ago

Last Updated: 8 months ago

Genera un tab donde se muestra como medir los talles de las remeras y chombas

WooCommerce

AI Verified

0

WooCommerce Free Shipping Before A Certain Time

Added: 1 year ago

Last Updated: 1 year ago

A lot of online stores allow free delivery before a certain time of day depending on the courier used. This guide shows you how you can enable the shipping method, free shipping between midnight and 2...

WooCommerce

AI Verified

0

WooCommerce How To Add Content Under The Proceed To Checkout Button On The Cart Page

Added: 1 year ago

Last Updated: 1 year ago

How many times have you bought something online and seen trust symbols near the proceed to checkout button on the cart page? “30 Day Money Back Guarantee” or “Secure Checkout” are popular examples. By...

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WooCommerce

AI Verified

0

Inventory Status Change

Added: 7 months ago

Last Updated: 7 months ago

I have a client who sells logs - has less than 10 products and doesn't use the stock management tools in WooCommerce. They wanted to control the Inventory on the basis of 'In Stock' or 'Out of Stock'...

WooCommerce

AI Verified

0

Call Us / Add to Basket - Woo Button swap

Added: 1 year ago

Last Updated: 7 months ago

Enable 'Add to Basket' for Products that are In Stock and 'Call Us' when items are out of stock