Skip to content

Instantly share code, notes, and snippets.

@braddalton
Created July 9, 2024 14:49
Show Gist options
  • Save braddalton/674e09ff4943b6ffa7a0c18f06ac9bef to your computer and use it in GitHub Desktop.
Save braddalton/674e09ff4943b6ffa7a0c18f06ac9bef to your computer and use it in GitHub Desktop.
WooCommerce Online Stock and Instore Stock
// Step 1: Add a meta box for store stock field
function custom_store_stock_meta_box() {
add_meta_box(
'custom_store_stock_meta_box', // Meta box ID
'Store Stock', // Title
'render_custom_store_stock_meta_box', // Callback function to render the meta box content
'product', // Post type (product for WooCommerce products)
'side', // Context (side, normal, advanced)
'default' // Priority (high, core, default, low)
);
}
add_action('add_meta_boxes', 'custom_store_stock_meta_box');
// Step 2: Render meta box content
function render_custom_store_stock_meta_box($post) {
// Retrieve the current value of the store stock if it exists
$store_stock = get_post_meta($post->ID, '_store_stock', true);
?>
<label for="store_stock_field">Store Stock:</label>
<input type="text" id="store_stock_field" name="store_stock_field" value="<?php echo esc_attr($store_stock); ?>" />
<?php
wp_nonce_field('save_store_stock_meta', 'store_stock_meta_nonce');
}
// Step 3: Save meta box data
function save_custom_store_stock_meta_box($post_id) {
// Check if nonce is set and valid
if (!isset($_POST['store_stock_meta_nonce']) || !wp_verify_nonce($_POST['store_stock_meta_nonce'], 'save_store_stock_meta')) {
return;
}
// Check if this is not an autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Check if the current user has permission to edit the post
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Save store stock value
if (isset($_POST['store_stock_field'])) {
update_post_meta($post_id, '_store_stock', sanitize_text_field($_POST['store_stock_field']));
} else {
delete_post_meta($post_id, '_store_stock');
}
}
add_action('save_post', 'save_custom_store_stock_meta_box');
// Step 1: Add the following code to your theme's functions.php file or a custom plugin
// Display inventory and custom store stock on single product page
function display_inventory_and_custom_store_stock() {
global $product;
// Get WooCommerce managed stock quantity
$stock_quantity = $product->get_stock_quantity();
// Get custom store stock quantity if set
$store_stock = get_post_meta(get_the_ID(), '_store_stock', true);
// Output both stock levels
echo '<div class="stock-status">';
// Display WooCommerce managed stock
if ($product->managing_stock() && $stock_quantity !== null) {
echo '<p class="woocommerce-stock-quantity">Online Stock: ' . $stock_quantity . '</p>';
}
// Display custom store stock if set
if (!empty($store_stock)) {
echo '<p class="custom-store-stock">Store Stock: ' . $store_stock . '</p>';
}
echo '</div>';
}
add_action('woocommerce_single_product_summary', 'display_inventory_and_custom_store_stock', 11);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment