the date shows 1 day in advance in the sale schedule woocommerce

Issue

i use the following hook to display the start and end dates of the discount :

function customize_woocommerce_get_price_html( $price, $product ) {
    if ( is_product() && $product->is_type( 'simple' ) && ! empty( $product->get_date_on_sale_from() && $product->get_date_on_sale_to() ) ) {
        return str_replace( '</del>', '</p><div class="sale-schedule"><p>' . __( 'start discount', 'woocommerce' ) . '<span>' . date_i18n( 'j F Y', $product->get_date_on_sale_from()->getTimestamp() ) . '</span></p>' . '<p>' . __( 'end discount', 'woocommerce' ) . '<span>' . date_i18n( 'j F Y', $product->get_date_on_sale_to()->getTimestamp() ) . '</p></span></div>', $price );
    }
    if ( is_product() && $product->is_type( 'variation' ) && ! empty( $product->get_date_on_sale_from() && $product->get_date_on_sale_to() ) ) {
        return str_replace( '</del>', '</span><div class="sale-schedule"><p>' . __( 'start discount', 'woocommerce' ) . '<span>' . date_i18n( 'j F Y', $product->get_date_on_sale_from()->getTimestamp() ) . '</span></p>' . '<p>' . __( 'end discount', 'woocommerce' ) . '<span>' . date_i18n( 'j F Y', $product->get_date_on_sale_to()->getTimestamp() ) . '</p></span></div>', $price );
    }
    return $price;
} add_filter( 'woocommerce_get_price_html', 'customize_woocommerce_get_price_html', 100, 2 );

displays the start and end date of the discount in product editing correctly, but when displayed to the user, it shows the discount start date 1 day before the selected date, the end date of the discount is absolutely correct

Solution

i changed getTimestamp() to getOffsetTimestamp() and it worked !

function customize_woocommerce_get_price_html( $price, $product ) {
    if ( is_product() && $product->is_type( 'simple' ) && ! empty( $product->get_date_on_sale_from() && $product->get_date_on_sale_to() ) ) {
        return str_replace( '</del>', '</p><div class="sale-schedule"><p>' . __( 'start discount', 'woocommerce' ) . '<span>' . date_i18n( 'j F Y', $product->get_date_on_sale_from()->getOffsetTimestamp() ) . '</span></p>' . '<p>' . __( 'end discount', 'woocommerce' ) . '<span>' . date_i18n( 'j F Y', $product->get_date_on_sale_to()->getOffsetTimestamp() ) . '</p></span></div>', $price );
    }
    if ( is_product() && $product->is_type( 'variation' ) && ! empty( $product->get_date_on_sale_from() && $product->get_date_on_sale_to() ) ) {
        return str_replace( '</del>', '</span><div class="sale-schedule"><p>' . __( 'start discount', 'woocommerce' ) . '<span>' . date_i18n( 'j F Y', $product->get_date_on_sale_from()->getOffsetTimestamp() ) . '</span></p>' . '<p>' . __( 'end discount', 'woocommerce' ) . '<span>' . date_i18n( 'j F Y', $product->get_date_on_sale_to()->getOffsetTimestamp() ) . '</p></span></div>', $price );
    }
    return $price;
} add_filter( 'woocommerce_get_price_html', 'customize_woocommerce_get_price_html', 100, 2 );

Answered By – benyas

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published