2016-11-21 141 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

相關問題