WooCommerce オプション商品に最低価格のみを表示する方法

Last Updated:2023年09月06日| | コメントを残す

WooCommerce ショッピングモールで商品をオプション商品として設定した場合、価格は最低価格と最高価格の範囲(例:10,000ウォン〜12,000ウォン)で表示されます。 オプション商品について最低価格のみ表示したい場合があります。 そのような場合 WooCommerce フックを使用して、最低価格のみを表示させることができます。

WooCommerce オプション商品に最低価格または最高価格のみを表示する方法

WordPress WooCommerceで商品をオプションに設定すると、商品リストページ(ショップページ)と商品カテゴリページ、および個々の商品ページで価格が最低価格と最高価格の範囲で表示されます。

WooCommerce オプション商品の最低価格のみを表示する

WooCommerce オプション商品に最低価格のみを表示する方法

最低価格と最高価格の両方を表示するのではなく、上の図の右図に示すように、「最低$ 100.00」などの最低価格のみを表示したい場合は、テーマ関数ファイルに次のコードを追加できます。 (チャイルドテーマを作成してチャイルドテーマ内の関数ファイルに追加しなければ、将来のテーマが更新されてもコードは消えません。)

// Display the lowest price for WooCommerce variable products
// WooCommerce 옵션 상품에 최저가를 표시합니다.

// Apply the filter for displaying the sale price of variable products
add_filter('woocommerce_variable_sale_price_html', 'display_lowest_variation_price', 10, 2);
// Apply the filter for displaying the regular price of variable products
add_filter('woocommerce_variable_price_html', 'display_lowest_variation_price', 10, 2);

function display_lowest_variation_price($price, $product) {

    // Get the lowest and highest regular prices among all variations
    $regular_prices = array(
        $product->get_variation_regular_price('min', true),
        $product->get_variation_regular_price('max', true)
    );
    sort($regular_prices);
    $lowest_regular_price = $regular_prices[0];

    // Get the lowest and highest sale prices among all variations
    $sale_prices = array(
        $product->get_variation_price('min', true),
        $product->get_variation_price('max', true)
    );
    sort($sale_prices);
    $lowest_sale_price = $sale_prices[0];

    // Check if the product is on sale
    if ($lowest_sale_price < $lowest_regular_price) {
        // If on sale, show both the sale price and the regular price
        $price = '<del>' . wc_price($lowest_regular_price) . $product->get_price_suffix() . '</del> <ins>' . sprintf(__('From %s', 'woocommerce'), wc_price($lowest_sale_price)) . $product->get_price_suffix() . '</ins>';
    } else {
        // If not on sale, just show the regular price with the "From" prefix
        $price = sprintf(__('From %s', 'woocommerce'), wc_price($lowest_regular_price));
    }

    return $price;
}

上記のコードで韓国語の場合、「From%s」部分を「最低%s」のように修正してください。

このような機能をするコードをこのブログの前の記事でも紹介したことがあります。 そして、グーグルすると同様のコードを見つけることができます。 上記のコードは最新バージョンでうまく機能し、古いコードで重複していた部分を排除することで効率化されました。

上記のコード Avada(Avada)テーマでうまく動作することを確認しました。 他のテーマで問題なく動作します。 エラーが発生した場合は、以下のコメントでエラーの症状とメッセージをお知らせください。コードを更新します。

WooCommerce オプション商品の最高価格のみを表示する

オプション商品の最高価格のみを表示したい場合は、次のコードを使用できます。

// Display the highest price for WooCommerce variable products
// WooCommerce 옵션 상품에 최고가를 표시합니다.

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

function improved_variation_price_format( $price, $product ) {

    // Main Price (max price among variations)
    $max_price = $product->get_variation_price( 'max', true );
    
    // If the product has varying prices, prefix with "Up To:", otherwise, just show the price
    $formatted_max_price = sprintf( __( 'Up To: %1$s', 'woocommerce' ), wc_price( $max_price ) );

    // Regular Price (max regular price among variations)
    $max_regular_price = $product->get_variation_regular_price( 'max', true );

    // Format the max regular price
    $formatted_max_regular_price = sprintf( __( 'Up To: %1$s', 'woocommerce' ), wc_price( $max_regular_price ) );
 
    // Determine final display based on whether the product is on sale
    if ( $formatted_max_price !== $formatted_max_regular_price ) {
        $price = '<del>' . $formatted_max_regular_price . $product->get_price_suffix() . '</del> <ins>' . $formatted_max_price . $product->get_price_suffix() . '</ins>';
    } else {
        $price = $formatted_max_price;
    }

    return $price;
}

韓国語版では、上記のコードの「Up To:...」部分を適切に修正します(例:「最高…」)。

店舗ページに最高値しか表示されていないと価格が高価に見えるので望ましくないようですが、もし最高価格のみを表示したい場合は上記のコードをご活用ください。

参照


コメントを残す

コメント