Change stock status names in WooCommerce variable products selector?

﹥>﹥吖頭↗ 提交于 2019-11-28 12:08:16

问题


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:

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.


回答1:


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;

}


来源:https://stackoverflow.com/questions/45190106/change-stock-status-names-in-woocommerce-variable-products-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!