Показывать дату окончания продажи продукта на странице продукта Woocommerce - PullRequest
1 голос
/ 29 мая 2020

У меня есть этот код, но он кажется устаревшим

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

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

$sales_price_from = $product->get_date_on_sale_from();
$sales_price_to   = $product->get_date_on_sale_to();

if( ! empty($sales_price_from) || ! empty($sales_price_to) ){
    $sales_price_date_to   = $sales_price_from->date( "j.m.Y");
    $sales_price_date_from = $sales_price_to->date( "j.m.Y");
    $sales_date = '<p class="offer_date">Angebot vom '.$sales_price_date_from.' bis '.$sales_price_date_to.'</p>';
} else {
    $sales_date = $sales_price_from = $sales_price_to = '';
}

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

return $price;
}

Я хочу отображать дату окончания распродажи для простых и переменных товаров.

1 Ответ

1 голос
/ 30 мая 2020

Вы используете переменную $ sales_price_to, если она не определена. Пожалуйста, используйте приведенный ниже код, он протестирован и отлично работает.

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

if ( is_single() && $product->is_on_sale() ) {
$sales_price_from = $product->get_date_on_sale_from();
$sales_price_to   = $product->get_date_on_sale_to();
if( ! empty($sales_price_from) || ! empty($sales_price_to) ){
    $sales_price_date_from   = $sales_price_from->date( "j.m.Y");
    $sales_price_date_to = $sales_price_to->date( "j.m.Y");
    $sales_date = '<p class="offer_date">Angebot vom '.$sales_price_date_from.' bis '.$sales_price_date_to.'</p>';
} else {
    $sales_date = $sales_price_from = $sales_price_to = '';
}

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

return $price;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...