简体   繁体   English

Woocommerce 上没有销售价格的“销售徽章”

[英]"Sales Badge" without sale price on Woocommerce

I'm working on woocommerce on wordpress, I want to make some sale badges but ignore the sales price (there are only regular prices).我正在 wordpress 上进行 woocommerce,我想制作一些销售徽章但忽略销售价格(只有正常价格)。

I've tried it several times but the "sale badge" can only appear when I put the number on the sale price on the product我已经尝试过几次,但“销售徽章”只有在我将数字放在产品的销售价格上时才会出现

I use the code below我使用下面的代码

  add_filter('woocommerce_sale_flash', 'woocommerce_custom_sale_text', 10, 3);
function woocommerce_custom_sale_text($text, $post, $_product)
{
    global $post,$product;
    if ( ! $product->is_in_stock() ) return;
    $sale_price = get_post_meta( $product->id, '_price', true);
    $regular_price = get_post_meta( $product->id, '_regular_price', true);
    if (has_term('one', 'product_cat', $product->ID)) {
        return '<span class="onsale">one</span>';
    } elseif (has_term('two', 'product_cat', $product->ID)) {
        return '<span class="onsale">two</span>';
    } elseif (has_term('three', 'product_cat', $product->ID) || empty($sale_price)) {
        return '<span class="onsale">three</span>';
    }
    return '<span class="onsale">Sale</span>';
}

The filter itself is applied only if the product is on sale.过滤器本身仅在产品打折时应用。

You need to overwrite the flash sale actions that happen before checking if the product is on sale.在检查产品是否正在销售之前,您需要覆盖发生的闪购操作。

first, remove the core flash sale hooks.首先,移除核心闪购挂钩。

remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );

then, add your custom sale function instead.然后,改为添加您的自定义销售功能。

add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_custom_sale_text', 10 );
add_action( 'woocommerce_before_single_product_summary', 'woocommerce_custom_sale_text', 10 );

then use echo instead of return然后使用echo而不是return

function woocommerce_custom_sale_text()
{
    global $post,$product;
    if ( ! $product->is_in_stock() ) return;
    $sale_price = get_post_meta( $product->id, '_price', true);
    $regular_price = get_post_meta( $product->id, '_regular_price', true);
    if (has_term('one', 'product_cat', $product->ID)) {
        echo '<span class="onsale">one</span>';
    } elseif (has_term('two', 'product_cat', $product->ID)) {
        echo '<span class="onsale">two</span>';
    } elseif (has_term('three', 'product_cat', $product->ID) || empty($sale_price)) {
        echo '<span class="onsale">three</span>';
    }
    echo '<span class="onsale">Sale</span>';
}

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

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