Home > Article > Backend Development > How to Add Custom Stock Statuses to WooCommerce Products in WooCommerce 4 ?
Custom Stock Status for WooCommerce Products in WooCommerce 4
Adding custom stock statuses to products in WooCommerce 4 is a relatively straightforward process. However, it requires modifying specific functions to ensure that the statuses are correctly displayed in the frontend and backend.
Adding Custom Stock Statuses
To add custom stock statuses, add the following code to your functions.php file:
<code class="php">function filter_woocommerce_product_stock_status_options( $status ) { // Add new statuses $status['pre_order'] = __('Pre Order', 'woocommerce'); $status['contact_us'] = __('Contact us', 'woocommerce'); return $status; } add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );</code>
This code adds two new statuses: "Pre Order" and "Contact Us".
Displaying Custom Stock Availability
To ensure the custom statuses display correctly in the frontend, apply the following changes:
<code class="php">// Availability text function filter_woocommerce_get_availability_text( $availability, $product ) { // Get stock status switch( $product->get_stock_status() ) { case 'pre_order': $availability = __( 'Pre Order', 'woocommerce' ); break; case 'contact_us': $availability = __( 'Contact us', 'woocommerce' ); break; } return $availability; } add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 ); // Availability CSS class function filter_woocommerce_get_availability_class( $class, $product ) { // Get stock status switch( $product->get_stock_status() ) { case 'pre_order': $class = 'pre-order'; break; case 'contact_us': $class = 'contact-us'; break; } return $class; } add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );</code>
Displaying Stock Status in Admin Products List
To display custom stock statuses in the admin products list table, modify the following function:
<code class="php">// Admin stock html function filter_woocommerce_admin_stock_html( $stock_html, $product ) { // Simple if ( $product->is_type( 'simple' ) ) { // Get stock status $product_stock_status = $product->get_stock_status(); // Variable } elseif ( $product->is_type( 'variable' ) ) { foreach( $product->get_visible_children() as $variation_id ) { // Get product $variation = wc_get_product( $variation_id ); // Get stock status $product_stock_status = $variation->get_stock_status(); } } // Stock status switch( $product_stock_status ) { case 'pre_order': $stock_html = '<mark class="pre-order" style="background:transparent none;color:#33ccff;font-weight:700;line-height:1;">' . __( 'Pre order', 'woocommerce' ) . '</mark>'; break; case 'contact_us': $stock_html = '<mark class="contact-us" style="background:transparent none;color:#cc33ff;font-weight:700;line-height:1;">' . __( 'Contact us', 'woocommerce' ) . '</mark>'; break; } return $stock_html; } add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );</code>
Optional: Using Custom Stock Status in Hooks
You can use custom stock statuses in hooks when you have access to the $product object or can use global $product.
Note:
The above is the detailed content of How to Add Custom Stock Statuses to WooCommerce Products in WooCommerce 4 ?. For more information, please follow other related articles on the PHP Chinese website!