在Woocommerce产品页面上显示产品销售结束日期

时间:2020-05-29 15:10:45

标签: wordpress woocommerce

我有此代码,但它似乎已经过时了

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 个答案:

答案 0 :(得分:1)

未定义$ 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;
}