简体   繁体   English

Woocommerce 店铺页面价格后加“库存”

[英]Woocommerce adding "stock" after price in shop page

Hey I need to put "in stock" text after price at my shop page.嘿,我需要在我的商店页面上的价格后添加“有货”文本。 Screen屏幕

I found this code我找到了这段代码

add_filter( 'woocommerce_get_price_html', 'prepend_append_icon_to_price', 10, 2 );
function prepend_append_icon_to_price( $price, $product ) {

    if( has_term( 'fast-shipping', 'product_cat', $product->get_id() ) && ! is_product() ){
        $price .= '<span style="float:right"><i class="fas fa-shipping-fast"></i></span> ';
    }
    return $price;
}

And I have this code我有这段代码

function envy_stock_catalog() {
    global $product;
    if ( $product->is_in_stock() ) {
        echo '<div class="stock" >' . $product->get_stock_quantity() . __( ' in stock', 'envy' ) . '</div>';
    } else {
        echo '<div class="out-of-stock" >' . __( 'out of stock', 'envy' ) . '</div>';
    }
}
add_action( 'woocommerce_after_shop_loop_item_title', 'envy_stock_catalog' );

Now i need to combine these codes, can you help me?现在我需要结合这些代码,你能帮我吗? Please:)请:)

You can do such type of things to fulfill your requirement.你可以做这样的事情来满足你的要求。 You can place this code in your functions.php.您可以将此代码放在您的函数中。php。

if(is_shop){
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 ); // remove previous price from shop
}
add_action('woocommerce_after_shop_loop_item_title','faastechies_solution');
function faastechies_solution(){
if(is_shop){    
global $product;
// Add your template here according to your further requirement. Just add classes and ids and use this css in your custom css or custom css file. 
 ?>
<span style="color:black; font-size: 16px font-weight: bold"> Price: <p 
style="display: inline; font-size: 16px; color: red; font-weight: 600"> 
    <? echo $product->get_regular_price();?>
    </p> Status: <p style="display: inline; font-size: 16px; color: red; font-weight: 
    600">
     <? echo $product->get_stock_status();;?>
     </p></span>
     <? 
     }}

Ok, thanks @fasstechies i edited the code and now it's working:) thx.好的,谢谢@fasstechies 我编辑了代码,现在它可以工作了:)谢谢。

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
add_action('woocommerce_after_shop_loop_item_title','faastechies_solution');
function faastechies_solution(){
if (is_shop()){
global $product;
// Add your template here according to your further requirement. Just add classes and ids and use this css in your custom css or custom css file. 
 ?>
<span style="color:black; font-size: 16px font-weight: bold"> Price: <p 
style="display: inline; font-size: 16px; color: red; font-weight: 600"> 
    <? echo $product->get_regular_price();?>
    </p> Status: <p style="display: inline; font-size: 16px; color: red; font-weight: 
    600">
     <? echo $product->get_stock_status();;?>
     </p></span>
     <? 
     }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM