This site is running Voxel Theme Version: 1.5.5 | Elementor Plugin Version: 3.28.1

Join the Voxel Guide Community!

Get Involved

You are now eligible to list your profile publicly!

Remodified version of stock availability

Description/Instructions

Stock > 10 → Shows “Available” in green.
Stock between 1-10 → Shows “Almost Sold Out” in orange.
Stock = 0 or not set → Shows “Sold Out” in red.

Now, when you use [product_stock] in your WordPress post/page, it will display the appropriate message based on stock levels.

Instructions

Go to appearance->theme edit->voxel-child->function.php 

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 stock information exists
if (isset($product_data['stock']['quantity'])) {
$stock_quantity = (int) $product_data['stock']['quantity'];

// Define stock levels
if ($stock_quantity > 10) {
return "<span style='color: green; font-weight: bold;'>Available ($stock_quantity in stock)</span>";
} elseif ($stock_quantity > 0 && $stock_quantity <= 10) {
return "<span style='color: orange; font-weight: bold;'>Almost Sold Out ($stock_quantity left)</span>";
} else {
return "<span style='color: red; font-weight: bold;'>Sold Out</span>";
}
} else {
return "<span style='color: red; font-weight: bold;'>Sold Out</span>";
}
}

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

  • PHP
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