2017-09-01 49 views
1

在函數文件中,我添加了一個過濾器鉤子以在變體產品「min」價格之前添加自定義標籤。WooCommerce變量產品:使用自定義標籤保留「最小」價格

我該如何獲得與價格相同的標籤?

見我的代碼和下面的截圖:

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) { 
    $min_price = $product->get_variation_price('min', true); 
    $price = sprintf(__('From%1$s', 'woocommerce'), wc_price($min_price)); 
    return $price; 
} 

回答

1

由於WooCommerce 3,woocommerce_variable_sale_price_html鉤已被棄用,不再適用。如果你不關心「分鐘」售價(當最小价格有售),您可以使用此:

add_filter('woocommerce_variable_price_html', 'custom_min_max_variable_price_html', 10, 2); 
function custom_min_max_variable_price_html($price, $product) { 
    $prices = $product->get_variation_prices(true); 
    $min_price = current($prices['price']); 
    $max_price = end($prices['price']); 

    $min_price_html = wc_price($min_price) . $product->get_price_suffix(); 
    $price = sprintf(__('From %1$s', 'woocommerce'), $min_price_html); 

    return $price; 
} 

代碼放在您的活動子主題的function.php文件(或主題)或任何插件文件。

經過測試並在WooCommerce 3+上工作。你會得到這樣的事情:

enter image description here

如果你關心「分鐘」售價(當最小价格有售),並且要同時顯示的價格,你應該使用這個代碼而不是:

add_filter('woocommerce_variable_price_html', 'custom_min_max_variable_price_html', 10, 2); 
function custom_min_max_variable_price_html($price, $product) { 
    $prices = $product->get_variation_prices(true); 
    $min_price = current($prices['price']); 
    $max_price = end($prices['price']); 

    $min_keys = current(array_keys($prices['price'])); 
    $min_price_regular = $prices['regular_price'][$min_keys]; 
    $min_price_html = wc_price($min_price) . $product->get_price_suffix(); 

    if($min_price_regular != $min_price){ // When min price is on sale (Can be removed) 
     $min_price_regular_html = '<del>' . wc_price($min_price_regular) . $product->get_price_suffix() . '</del>'; 
     $min_price_html = $min_price_regular_html .'<ins>' . $min_price_html . '</ins>'; 
    } 
    $price = sprintf(__('From %1$s', 'woocommerce'), $min_price_html); 

    return $price; 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

經過測試並在WooCommerce 3+上工作。你會得到這樣的事情:

enter image description here

+0

它就像一個魅力。謝謝! – mobis

相關問題