Woocommerce在商店页面上添加价格后的“库存”

时间:2020-08-31 17:06:10

标签: php wordpress woocommerce product woocommerce-theming

嘿,我需要在商店页面上标出价格后的“现货”文本。 Screen

我找到了这段代码

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;
}

我有这个代码

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' );

现在我需要结合这些代码,您能帮我吗?请:)

2 个答案:

答案 0 :(得分:0)

您可以执行此类操作来满足您的要求。您可以将此代码放在您的functions.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>
     <? 
     }}

答案 1 :(得分:0)

好的,谢谢@fasstechies我编辑了代码,现在可以正常工作了:) thx。

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>
     <? 
     }}
相关问题