admin管理员组

文章数量:1122846

i am finding how to change the value from _stock_status on single product. I have display the product stock status by that code:

<?php echo get_post_meta( get_the_ID(), '_stock_status', true ); ?>

but '_stock_status' return the value: "outofstock". I want to change this value to other name

Please help me to solve this. Thank you in advance

i am finding how to change the value from _stock_status on single product. I have display the product stock status by that code:

<?php echo get_post_meta( get_the_ID(), '_stock_status', true ); ?>

but '_stock_status' return the value: "outofstock". I want to change this value to other name

Please help me to solve this. Thank you in advance

Share Improve this question asked Jan 19, 2019 at 8:01 mrleezoomrleezoo 111 silver badge2 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You can use the code update_post_meta( get_the_ID(), '_stock_status',$value) to change the value of the meta. Please place the above line in the code where you want to change the status value and remember $value is the variable/array/object you can place any string/array/object in it.

IMO, It'd be best to create a helper function which returns your changed/translated text.

    function wpse326063_stock_texts( $product_id ) {
        
        $status = get_post_meta( $product_id, '_stock_status', true );
        
        //Out of Stock Message
        if ( $status == 'outofstock' ) {
            $status = 'This Product is Out of Stock';
        }
        
        //You can add your conditionals to change other texts.
        
        return $status;
    }

Now you can use it like this..

    <?php echo wpse326063_stock_texts( get_the_ID() );?>

本文标签: woocommerce offtopichow to change value return by stockstatus