首页 > 解决方案 > 在 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;
}

我想显示简单和可变产品的销售结束日期。

标签: wordpresswoocommerce

解决方案


您正在使用未定义的 $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;
}

推荐阅读