2015-09-23 43 views
0

在2.4x之前,我使用此片段從目錄和產品頁面上的價格而不是最小 - 最大價格。Woocommerce 2.4x - 使用價格方法2.0的可變價格問題

add_filter('woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2); 
add_filter('woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2); 
function wc_wc20_variation_price_format($price, $product) { 
// Main Price 
$prices = array($product->get_variation_price('min', true), $product->get_variation_price('max', true)); 
$price = $prices[0] !== $prices[1] ? sprintf(__('From: %1$s', 'woocommerce'), wc_price($prices[0])) : wc_price($prices[0]); 
// Sale Price 
$prices = array($product->get_variation_regular_price('min', true), $product->get_variation_regular_price('max', true)); 
sort($prices); 
$saleprice = $prices[0] !== $prices[1] ? sprintf(__('From: %1$s', 'woocommerce'), wc_price($prices[0])) : wc_price($prices[0]); 
if ($price !== $saleprice) { 
$price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>'; 
} 
return $price; 
} 

但是,由於他們更新了整個變化邏輯,所以它似乎不再那麼好用了。我參考這個官方帖子:https://woocommerce.wordpress.com/2015/09/14/caching-and-dynamic-pricing-upcoming-changes-to-the-get_variation_prices-method/

任何提示如何解決這個問題?

參考: https://github.com/woothemes/woocommerce/blob/2.4.7/includes/class-wc-product-variable.php#L257

回答

0

看看WooCommerce如何generating the variable price html。這應該有很大的幫助。基於此,以下是我想出的:

add_filter('woocommerce_variable_price_html', 'so_32738056_filter_variable_price_html', 10, 2); 
add_filter('woocommerce_variable_sale_price_html', 'so_32738056_filter_variable_price_html', 10, 2); 

function so_32738056_filter_variable_price_html($price, $product){ 
    $prices = $product->get_variation_prices(true); 

    $min_price = current($prices['price']); 
    $max_price = end($prices['price']); 
    $price  = $min_price !== $max_price ? sprintf(_x('From %1$s', 'minimum price', 'woocommerce'), wc_price($min_price)) : wc_price($min_price); 
    if ($product->is_on_sale()) { 
     $min_regular_price = current($prices['regular_price']); 
     $max_regular_price = end($prices['regular_price']); 
     $regular_price  = $min_regular_price !== $max_regular_price ? sprintf(_x('From: %1$s', 'minimum price', 'woocommerce'), wc_price($min_regular_price), wc_price($max_regular_price)) : wc_price($min_regular_price); 
     $price    = '<del>' . $regular_price . '</del> <ins>' . $price . '</ins>'; 
    } else { 
     $price = $price . $product->get_price_suffix(); 
    } 
    return $price; 
}