Custom stock statuses, such as "Preorder" and "Contact us," are missing from the product options in WooCommerce 4 .
Add the following code to your functions.php file:
// Add new stock status options add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 ); function filter_woocommerce_product_stock_status_options( $status ) { $status['pre_order'] = __( 'Pre order', 'woocommerce' ); $status['contact_us'] = __( 'Contact us', 'woocommerce' ); return $status; } // Availability text add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 ); function filter_woocommerce_get_availability_text( $availability, $product ) { switch( $product->get_stock_status() ) { case 'pre_order': $availability = __( 'Pre order', 'woocommerce' ); break; case 'contact_us': $availability = __( 'Contact us', 'woocommerce' ); break; } return $availability; } // Availability CSS class add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 ); function filter_woocommerce_get_availability_class( $class, $product ) { switch( $product->get_stock_status() ) { case 'pre_order': $class = 'pre-order'; break; case 'contact_us': $class = 'contact-us'; break; } return $class; } // Admin stock html add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 ); function filter_woocommerce_admin_stock_html( $stock_html, $product ) { switch( $product->get_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; }
Additional Notes:
The above is the detailed content of How to Add Custom Stock Statuses like 'Preorder” and 'Contact Us” to WooCommerce Products?. For more information, please follow other related articles on the PHP Chinese website!