Join the Voxel Guide Community!

Get Involved

Voxel Theme Version: 1.5.4 | Elementor Plugin Version: 3.28.0

You are now eligible to list your profile publicly!

Show Quantity of Stock on Front End

Description/Instructions

Snippet will allow you to show how much stock the item has on the front end. Works for simple products, not tested with variable or booking.

If this snippet helped, feel free to buy me a taco :)

function display_product_stock() {
global $post;
// Retrieve the meta value for 'product'
$product_meta = get_post_meta($post->ID, 'product', true);

// Decode the JSON stored in the meta value
$product_data = json_decode($product_meta, true);

// Check if the product data has stock information and the stock quantity is greater than 0
if (isset($product_data['stock']['quantity']) && $product_data['stock']['quantity'] > 0) {
$stock_quantity = $product_data['stock']['quantity'];
// Display the quantity and 'in stock' text
return $stock_quantity . ' in stock';
} else {
// Handle no stock or stock not enabled
return 'Out of stock';
}
}

// Register the shortcode with WordPress
add_shortcode('product_stock', 'display_product_stock');

  • PHP
Copy Code

[product_stock]

  • HTML
Copy Code

Let's Chat About this Snippet

Chat Toggle
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Ask me anything about Voxel!
Send
Powered by AI24