Support » Plugin: WooCommerce » Custom Managed Stock Status In Woo Commerce

  • Resolved Stu

    (@stuartiwoodheadgmailcom)


    I have a shop that exports from USA to UK. Currently as we buy stock we list it on the website once we buy it (zero stock) and ship it to the UK. Once safely in the UK the stock level is updated and the goods are available to purchase. This gives people the chance to put a watch on items as they are in transit from the US to the UK. Down side is that as we load the stock on (at zero) everything shows out of stock that is in transit. Users have no way of knowing if an item is actually out of stock or on the way.

    I’d like to have a status for ‘in transit’ that we set as we load items up. then when the stock gets updated in the UK they can switch to ‘in stock’

    Found plenty of code that works as Lon as managed stock is disabled but this allows users to enter any number of items into their carts – I want ‘In Transit’ zero stock, then ‘in stock’ (1) then ‘out of stock’ (0). That way we can differentiate the various status of stock.

    ———- code that works if managed stock is off ———————–

    // Add new stock status options
    function filter_woocommerce_product_stock_status_options( $status ) {
        // Add new statuses
        $status['in_transit'] = __( 'In Transit', 'woocommerce' );
    
        return $status;
    }
    add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );
    
    // Availability text
    function filter_woocommerce_get_availability_text( $availability, $product ) {
        switch( $product->get_stock_status() ) {
            case 'in_transit':
                $availability = __( 'In Transit', 'woocommerce' );
            break;
            
        }
    
        return $availability; 
    }
    add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );
    
    // Availability class
    function filter_woocommerce_get_availability_class( $class, $product ) {
        switch( $product->get_stock_status() ) {
            case 'in_transit':
                $class = 'in-transit'; 
            break;
            
        }
    
        return $class;
    }
    add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );
    // admin stock html
    function filter_woocommerce_admin_stock_html( $stock_html, $product ) {
        switch( $product->get_stock_status() ) {
            case 'in_transit':
                $stock_html = '<mark class="in-transit" style="background:transparent none;color:#33ccff;font-weight:700;line-height:1;">' . __( 'In Transit', 'woocommerce' ) . '</mark>'; 
            break;
        }   
    
        return $stock_html;
    }
    add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.