關於WooCommerce 中的可變產品,標準版面在產品標題下顯示價格範圍。對於想要了解所選產品的確切價格的客戶來說,這可能會造成混淆和誤導。
此程式碼解決了透過刪除價格範圍並僅顯示最低產品價格來解決問題。此外,它還會根據所選變體更新顯示的價格。
<br>add_action( 'woocommerce_before_single_product', 'move_variations_single_price', 1 );<br>函數move_variations_single_price(){<pre class="brush:php;toolbar:false">global $product, $post; if ( $product->is_type( 'variable' ) ) { // removing the variations price for variable products remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 ); // Change location and inserting back the variations price add_action( 'woocommerce_single_product_summary', 'replace_variation_single_price', 10 ); }
}
函數replace_variation_single_price(){
global $product; // Prices and calculations $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] ); $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 && $product->is_on_sale() ) { $price = '<del>' . $saleprice . $product->get_price_suffix() . '</del> <ins>' . $price . $product->get_price_suffix() . '</ins>'; } ?> <style> div.woocommerce-variation-price, div.woocommerce-variation-availability, div.hidden-variable-price { height: 0px !important; overflow:hidden; position:relative; line-height: 0px !important; font-size: 0% !important; } </style> <script> jQuery(document).ready(function($) { $('select').blur( function(){ if( '' != $('input.variation_id').val() ){ if($('p.availability')) $('p.availability').remove(); $('p.price').html($('div.woocommerce-variation-price > span.price').html()).append('<p>函數replace_variation_single_price(){
< ;h3>附加註意:
以上是如何在 WooCommerce 3 中將可變價格範圍替換為所選的變化價格?的詳細內容。更多資訊請關注PHP中文網其他相關文章!