繁体   English   中英

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

[英]"Sales Badge" without sale price on 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>';
}

过滤器本身仅在产品打折时应用。

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

首先,移除核心闪购挂钩。

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