【问题标题】:Woocommerce adding "stock" after price in shop pageWoocommerce 在商店页面的价格后添加“库存”
【发布时间】:2020-08-31 17:06:10
【问题描述】:

嘿,我需要在我的商店页面的价格后面加上“有货”文字。 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' );

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

【问题讨论】:

    标签: php wordpress woocommerce product woocommerce-theming


    【解决方案1】:

    你可以做这样的事情来满足你的要求。您可以将此代码放在您的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>
         <? 
         }}
    

    【讨论】:

    • 好的,我将 if (is_shop){ 替换为 if (is_shop()){ 并且它现在可以工作但仍然有旧价格。也许这个 remove_action 是错误的?
    【解决方案2】:

    好的,谢谢@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>
         <? 
         }}
    

    【讨论】:

      猜你喜欢
      • 2016-09-24
      • 2018-09-01
      • 2018-07-01
      • 1970-01-01
      • 2021-12-29
      • 2019-08-01
      • 2022-01-15
      • 2021-03-05
      • 1970-01-01
      相关资源
      最近更新 更多