【问题标题】:How to show the lowest price of a variable in woocommerce product by specifying the price如何通过指定价格在 woocommerce 产品中显示变量的最低价格
【发布时间】:2022-12-03 03:29:34
【问题描述】:

我有一个我正在使用的 WordPress woo-commerce 网站,它有 1 件装、5 件装、10 件装和 30 件装的可变产品。如果您选择 30 件装,则 1 件装的价格是最便宜的,这是我希望向访问该网站的人展示的价格。我试图在网站的后端有一个位置,可以指定和显示此价格,因为每种产品的计算方式各不相同。

我在这里修改了这段代码Display prefixed price and additional unit price in Woocommerce simple products

add_filter( 'woocommerce_get_price_html', 'unit_product_price_on_archives', 10, 2 );
function unit_product_price_on_archives( $price, $product ) {
if ( is_product() || is_product_category() || is_product_tag() ) {
    $unit_divider = 6;
    $group_suffix = ' '. __('pack', 'woocommerce');
    $unit_suffix = ' '. __('(per pack)', 'woocommerce');

    if( $product->is_on_sale() )
    {
        $regular_price_unit = $product->get_regular_price() / $unit_divider;
        $regular_price_unit = wc_get_price_to_display( $product, array( 'price' => $regular_price_unit ) );

        $regular_price_group = $product->get_regular_price();
        $regular_price_group = wc_get_price_to_display( $product, array( 'price' => $regular_price_group ) );
      
        $group_price_sale = $product->get_sale_price();
        $group_price_sale = wc_get_price_to_display( $product, array( 'price' => $group_price_sale ) );

        $group_price_sale = wc_format_sale_price( $regular_price_group, $group_price_sale ) . $group_suffix;


        $unit_price_sale = $product->get_sale_price() / $unit_divider;
        $unit_price_sale = wc_get_price_to_display( $product, array( 'price' => $unit_price_sale ) );
        $unit_price_sale = wc_format_sale_price( $regular_price_unit, $unit_price_sale ) . $unit_suffix;
     
        $price = $group_price_sale . '<br>' . $unit_price_sale;
    }
    else
    {
        $group_price = $price;
        $group_price = $group_price . $group_suffix;
        $unit_price = $product->get_price() / $unit_divider;
        $unit_price = wc_get_price_to_display( $product, array( 'price' => $unit_price ) );
        $unit_price = $price = wc_price($unit_price) . $unit_suffix;
        $price = $group_price . '<br>' . $unit_price;
    }
}
return $price; 
}

但是代码只是将原来的简单乘积除以 6。

【问题讨论】:

    标签: php wordpress woocommerce product price


    【解决方案1】:

    这应该可以达到您要寻找的结果:

    /** Variable Product Price Range: "From: $$$min_price" */
    add_filter( 'woocommerce_variable_price_html', ' 
    bbloomer_variation_price_format_min', 9999, 2 );
    function bbloomer_variation_price_format_min( $price, $product ) {
    $prices = $product->get_variation_prices( true );
    $min_price = current( $prices['price'] );
    $price = sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $min_price ) );
    return $price;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 2017-11-24
      • 2016-05-08
      • 2021-04-29
      • 2018-05-25
      • 1970-01-01
      相关资源
      最近更新 更多