Last active
February 17, 2025 03:06
-
-
Save mommaroodles/7bb9c1aeafd4b85f6be56a2a45483ab8 to your computer and use it in GitHub Desktop.
Custom Stock Status - Discontinued
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Step 1: Add custom stock status to WooCommerce product stock statuses | |
add_filter('woocommerce_product_stock_status_options', 'custom_woocommerce_product_stock_status_options'); | |
function custom_woocommerce_product_stock_status_options($status_options) { | |
$status_options['discontinued'] = __('Discontinued', 'woocommerce'); | |
return $status_options; | |
} | |
// Step 2: Save custom stock status for products | |
add_action('woocommerce_process_product_meta', 'custom_save_product_meta'); | |
function custom_save_product_meta($post_id) { | |
$product = wc_get_product($post_id); | |
if (isset($_POST['_stock_status']) && $_POST['_stock_status'] === 'discontinued') { | |
$product->set_stock_status('discontinued'); | |
} | |
} | |
// Step 3: Display custom stock status on the product page | |
add_filter('woocommerce_get_availability_text', 'custom_get_availability_text', 10, 2); | |
function custom_get_availability_text($availability, $product) { | |
if ($product->get_stock_status() === 'discontinued') { | |
$availability = __('Discontinued', 'woocommerce'); | |
} | |
return $availability; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cnvr_o