首页  >  问答  >  正文

“在 Woocommerce 上显示销售徽章而不显示销售价格”

我正在 WordPress 上开发 woocommerce,我想制作一些销售徽章,但忽略销售价格(只有常规价格)。

我已经尝试过几次,但只有当我将数字放在产品的促销价格上时,“促销徽章”才会出现

我使用下面的代码

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

P粉590929392P粉590929392260 天前544

全部回复(1)我来回复

  • P粉713866425

    P粉7138664252024-01-08 13:15:16

    过滤器本身仅在产品促销时应用。

    您需要覆盖在检查产品是否在促销之前发生的闪购操作。

    首先,删除核心的闪购挂钩。

    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而不是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 'one';
        } elseif (has_term('two', 'product_cat', $product->ID)) {
            echo 'two';
        } elseif (has_term('three', 'product_cat', $product->ID) || empty($sale_price)) {
            echo 'three';
        }
        echo 'Sale';
    }

    回复
    0
  • 取消回复