Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

php - Change stock status names in WooCommerce variable products selector?

I have this code:

$stock_status = $variation_obj->get_stock_status();
$stock_qty = $variation_obj->get_stock_quantity();
if( $stock_qty>0 )
    return $term_name .= ' - ' . $stock_status . ' ('.$stock_qty.')';
else
    return $term_name .= ' - ' . $stock_status;

}

I use it to show the variation stock status like "In Stock" or "Out of Stock". Currently this part of my function works, but show the stock status is this way:

enter image description here

So I want to ask someone how to show "In Stock" instead of "instock" as is shown in image under.
I tryed to use some functions for custom stock status, but looks like dont have any effect.

Any help please?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do it simply using str_replace() PHP function this way:

$stock_status = $variation_obj->get_stock_status();

// Here we change both $stock_status names to human readable strings:
$stock_status = str_replace( array('instock','outofstock'), array('In Stock','Out of Stock'), $stock_status );

$stock_qty = $variation_obj->get_stock_quantity();

if( $stock_qty>0 )
    return $term_name .= ' - ' . $stock_status . ' ('.$stock_qty.')';
else
    return $term_name .= ' - ' . $stock_status;

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...