“销售徽章”没有销售价格

发布于 2025-02-13 14:04:17 字数 972 浏览 1 评论 0原文

我正在WordPress上工作,我想进行一些销售徽章,但忽略了销售价格(只有常规价格)。

产品上的销售价格上的数字上,才能出现“销售徽章”

我已经尝试过几次,但是只有在我使用下面使用代码的

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

I'm working on woocommerce on wordpress, I want to make some sale badges but ignore the sales price (there are only regular prices).

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淑女气质 2025-02-20 14:04:17

仅当产品开始销售时才应用过滤器本身。

您需要在检查产品是否出售之前覆盖发生的闪存销售动作。

首先,删除核心闪存销售挂钩。

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

然后,改用您的自定义销售功能。

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

然后使用echo而不是返回

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

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

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>';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文