Menu Close

How to change stock status option in woocommerce

0
(0)

Code Snippet to change stock status option in woocommerce.

Here I change out of stock option text to Sold and you can also add the option using the woocommerce_add_custom_stock_status function like below.

Create custom stock status options

function woocommerce_add_custom_stock_status() {
    ?>
    <script type="text/javascript">
    jQuery(function(){
        jQuery('._stock_status_field').not('.custom-stock-status').remove();
    });
    </script>
    <?php   
    
    $stock_status = get_post_meta($_REQUEST['post'], '_custom_stock_status', true );
    update_post_meta($_REQUEST['post'], '_stock_status', wc_clean( $stock_status ));


    woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'custom-stock-status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(        
        'instock' => __( 'In stock', 'woocommerce' ),
        'onbackorder' => __( 'Back Order', 'woocommerce' ),
        'outofstock' => __( 'Sold', 'woocommerce' ),
    ), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );    
}
add_action('woocommerce_product_options_stock_status', 'woocommerce_add_custom_stock_status'); 

Save selected stock status option value in database

function woocommerce_save_custom_stock_status( $product_id ) {
    update_post_meta( $product_id, '_stock_status', wc_clean( $_POST['_stock_status'] ) );
    update_post_meta( $product_id, 'stock_status', wc_clean( $_POST['_stock_status'] ) );
    update_post_meta( $product_id, '_custom_stock_status', wc_clean( $_POST['_stock_status'] ) ); //save another custom meta since '_stock_status' will be overridden
}
add_action('woocommerce_process_product_meta', 'woocommerce_save_custom_stock_status',99,1); 

Fetch the stock status option value and display at front view

function get_custom_stock_status( $data, $product ) {
   $product_id = $product->id;
   $stock_status = get_post_meta($product_id, '_custom_stock_status', true );
    
    switch( $stock_status ) {
        case 'instock':
            $data = array( 'availability' => __( 'In stock', 'woocommerce' ), 'class' => 'in-stock' );
        break;
        case 'onbackorder':
            $data = array( 'availability' => __( 'Back Order', 'woocommerce' ), 'class' => 'onbackorder' );
        break;
        case 'outofstock':
            $data = array( 'availability' => __( 'Sold', 'woocommerce' ), 'class' => 'outofstock' );
        break;
        case '':
            $data = array( 'availability' => __( 'In stock', 'woocommerce' ), 'class' => 'in-stock' );
        break;
    }
    return $data;
}
add_action('woocommerce_get_availability', 'get_custom_stock_status', 10, 2); 

Change stock status once customer create order

function woocommerce_order_change_custom_stock_status( $order_id ){
    if( ! $order_id ) return;

    $order = wc_get_order( $order_id );
    $items = $order->get_items();
    foreach ( $items as $item ) {
        $product_id = $item->get_product_id();

        $real_stock_status = get_post_meta($product_id, '_stock_status', true );
        if($real_stock_status=="onbackorder") {
            $stock_status = get_post_meta($product_id, '_custom_stock_status', true ); //get status from custom meta
            update_post_meta($product_id, '_stock_status', wc_clean( $stock_status ));
        }
    }
}
add_action( 'woocommerce_thankyou', 'woocommerce_order_change_custom_stock_status', 10, 1 ); 

Check the blog for change the out of stock text to sold – https://codeamend.com/blog/how-to-change-out-of-stock-text-in-woocommerce/

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Share

Posted in PHP, WooCommerce, WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *

You can also read...