首页 > 解决方案 > Woocommerce 在产品页面上显示销售日期

问题描述

我正在尝试在我的产品页面上显示销售日期范围从-> 到。我的代码适用于简单的产品,但对于变量我无法使用。我已经尝试了一段时间,但没有成功,我想我快到了,我想念一些简单的东西。我希望任何人都可以帮助我,因为我真的需要让这个工作,实现它会很棒。

$alreadyran = false;
function flux_sale_end_date_to_product_price( $price_html, $product ) {
    $child = $product->get_children();
    if($alreadyran){
        return $price_html;
    }
    $alreadyran = true;
    $sale_price = $product->get_sale_price();
    $from = $product->get_date_on_sale_from();
    if (
        empty( $from ) ||
        ! $product->is_on_sale() ||
        ! is_single()
    ) {
        return $price_html;
    }
    

    $output_content = '';
    $date_format = apply_filters( 'flux_date_format', get_option( 'date_format' ) );
    $sale_from_text    = apply_filters( 'flux_sale_from_text', __( 'Promoção válida de', 'woocommerce-onsale-information' ) );
    $sale_ends_on_text = apply_filters( 'flux_sale_ends_on_text', __( 'até', 'woocommerce-onsale-information' ) );
    $wrapper_css_class   = apply_filters( 'flux_wrapper_css_class', 'flux_wrapper' );
    $sale_from_css_class = apply_filters( 'flux_sale_from_css_class', 'flux_sale_from_date' );
    $sale_end_css_class  = apply_filters( 'flux_sale_end_css_class', 'flux_sale_end_date' );

    if ( $product->get_date_on_sale_from() ) {
        $sale_from_date = date_i18n( $date_format, $product->get_date_on_sale_from()->getTimestamp() );
        $sale_from_html = '<div class="' . $sale_from_css_class . '">' . $sale_from_text . ' ' . $sale_from_date . ' </div>';

        $output_content .= $sale_from_html;
    }

    if ( $product->get_date_on_sale_to() ) {
        $sale_end_date = date_i18n( $date_format, $product->get_date_on_sale_to()->getTimestamp() );
        $sale_end_html = '<div class="' . $sale_end_css_class . '">' . $sale_ends_on_text . ' ' . $sale_end_date . ' </div>';

        $output_content .= $sale_end_html;
    }
    $output = '<div class="' . $wrapper_css_class . '">' . $output_content . '</div>';
    return apply_filters( 'flux_price_html', $price_html . $output );


}

add_filter( 'woocommerce_get_price_html', 'flux_sale_end_date_to_product_price', 100, 2 );

标签: wordpresswoocommerce

解决方案


推荐阅读