显示销售日期和销售日期至

时间:2016-11-21 02:30:12

标签: wordpress woocommerce

我从这里得到了这段代码(Display woocommerce sale end date

add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product )
{
    global $post;
    $sales_price_to = get_post_meta($post->ID, '_sale_price_dates_to', true);
    if(is_single() && $sales_price_to != "")
    {
        $sales_price_date_to = date("j M y", $sales_price_to);
        return str_replace( '</ins>', ' </ins> <b>(Offer till '.$sales_price_date_to.')</b>', $price );
    }
    else
    {
        return apply_filters( 'woocommerce_get_price', $price );
    }
}

这仅显示销售日期。如何显示从销售日期到销售日期的销售日期?

我尝试编辑这一行:

return str_replace( '</ins>', ' </ins> <b>(Offer till '.$sales_price_date_to.')</b>', $price );

return str_replace( '</ins>', ' </ins> <b>(Offer from '.$sales_price_date_from.' till '.$sales_price_date_to.')</b>', $price );

但它不会显示销售日期。

有人可以告诉我如何显示销售日期和销售日期吗?

2 个答案:

答案 0 :(得分:1)

我在我的网站上试过它,它对我有用。

add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product ) {

    $sales_price_from = get_post_meta( $product->id, '_sale_price_dates_from', true );
    $sales_price_to   = get_post_meta( $product->id, '_sale_price_dates_to', true );

    if ( is_single() && $product->is_on_sale() && $sales_price_to != "" ) {

        $sales_price_date_from = date( "j M y", $sales_price_from );
        $sales_price_date_to   = date( "j M y", $sales_price_to );

        $price = str_replace( '</ins>', ' </ins> <b>(Offer from ' . $sales_price_date_from . ' till ' . $sales_price_date_to . ')</b>', $price );
    }

    return apply_filters( 'woocommerce_get_price', $price );
}

答案 1 :(得分:1)

我为此做了一个插件(100%免费,没有广告,没有隐藏功能)。

与商店中其他商品的主要区别在于,它也可以与变体一起使用(即使每个变体具有不同的日期)。

https://wordpress.org/plugins/woo-on-sale-information/

此外,它的开发考虑到了简单性,并为开发人员提供了筛选器,使其能够进行调整而不会出现问题(代码也有据可查)。

看看;)