2016-11-21 138 views
0

我得到这个代码从这里(Display woocommerce sale end date显示销售日期和销售日期到

add_filter('woocommerce_get_price_html', 'custom_price_html', 100, 2); 
function custom_price_html($price, $product) 
{ 
    global $post; 
    $sales_price_to = get_post_meta($post->ID, '_sale_price_dates_to', true); 
    if(is_single() && $sales_price_to != "") 
    { 
     $sales_price_date_to = date("j M y", $sales_price_to); 
     return str_replace('</ins>', ' </ins> <b>(Offer till '.$sales_price_date_to.')</b>', $price); 
    } 
    else 
    { 
     return apply_filters('woocommerce_get_price', $price); 
    } 
} 

这仅显示销售日期来。我如何让它显示从销售日期到销售日期的销售日期?

我试图编辑这一行:

return str_replace('</ins>', ' </ins> <b>(Offer till '.$sales_price_date_to.')</b>', $price); 

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

,但它不会显示从销售日期。

那么有人可以告诉我怎么显示销售日期和销售日期?

回答

1

我已经在我的网站上试过了,它适用于我。

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

    $sales_price_from = get_post_meta($product->id, '_sale_price_dates_from', true); 
    $sales_price_to = get_post_meta($product->id, '_sale_price_dates_to', true); 

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

     $sales_price_date_from = date("j M y", $sales_price_from); 
     $sales_price_date_to = date("j M y", $sales_price_to); 

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

    return apply_filters('woocommerce_get_price', $price); 
} 
+0

这不适合我,它会导致我的页面崩溃,我无法看到我的网站。你能粘贴我你尝试过的完整代码吗?我会尝试看看是否有效。 – gosi123

+0

你可以启用WP_DEBUG,然后在这里粘贴错误? – hoangkianh

+1

我再次测试了您的代码,我意识到我没有删除旧代码,因此发生了错误。你的代码完美无缺!谢谢。 – gosi123

相关问题