Home > Backend Development > PHP Tutorial > How to Add Custom Stock Statuses to WooCommerce Products in WooCommerce 4 ?

How to Add Custom Stock Statuses to WooCommerce Products in WooCommerce 4 ?

Susan Sarandon
Release: 2024-11-03 14:59:02
Original
680 people have browsed it

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>
Copy after login

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>
Copy after login

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=&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;
}
add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );</code>
Copy after login

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:

  • Use global $product when you don't have access to the $product object, such as in the woocommerce_shop_loop_item_title and woocommerce_single_product_summary hooks.
  • Access the $product object if passed by default to the callback function, as in the woocommerce_get_price_html hook.

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template