Home  >  Article  >  Backend Development  >  How to Add Custom Stock Statuses like “Preorder” and “Contact Us” to WooCommerce Products?

How to Add Custom Stock Statuses like “Preorder” and “Contact Us” to WooCommerce Products?

Linda Hamilton
Linda HamiltonOriginal
2024-11-02 22:32:291038browse

How to Add Custom Stock Statuses like “Preorder” and “Contact Us” to WooCommerce Products?

How to Add Custom Stock Status to Products in WooCommerce 4

The Problem

Custom stock statuses, such as "Preorder" and "Contact us," are missing from the product options in WooCommerce 4 .

The Solution

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=&quot;pre-order&quot; style=&quot;background:transparent none;color:#33ccff;font-weight:700;line-height:1;&quot;>' . __( 'Pre order', 'woocommerce' ) . '</mark>';
        break;
        case 'contact_us':
            $stock_html = '<mark class=&quot;contact-us&quot; style=&quot;background:transparent none;color:#cc33ff;font-weight:700;line-height:1;&quot;>' . __( 'Contact us', 'woocommerce' ) . '</mark>';
        break;
    }
    return $stock_html;
}

Additional Notes:

  • These changes do not have an effect on existing stock statuses.
  • The new stock statuses will appear on the product page, the single product page, and the admin products list table.
  • If desired, you can use the custom stock statuses in hooks where you already have access to the $product object.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn