Get Involved
No items added to cart
Home
Discover
Hire
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.
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 existsif (isset($product_data['stock']['quantity'])) {$stock_quantity = (int) $product_data['stock']['quantity']; // Define stock levelsif ($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 WordPressadd_shortcode('product_stock', 'display_product_stock');
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 existsif (isset($product_data['stock']['quantity'])) {$stock_quantity = (int) $product_data['stock']['quantity'];
// Define stock levelsif ($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 WordPressadd_shortcode('product_stock', 'display_product_stock');
Are you sure you want to exit? Your current conversation will be lost.