【问题标题】:Display lowest variation price and discounted percentage in WooCommerce在 WooCommerce 中显示最低变化价格和折扣百分比
【发布时间】:2020-11-02 04:31:54
【问题描述】:

根据这篇文章:

我一直在尝试在我的网站上随处显示可变和非可变产品的最低变化价格以及相关的折扣百分比。

这是我正在使用的当前代码:

add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format', 10, 2 );


function bbloomer_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( __( '%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( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    if ( $price !== $saleprice ) {
        $price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
    }
    return $price;
}

add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
    // Getting the clean numeric prices (without html and currency)
    $regular_price = floatval( strip_tags($regular_price) );
    $sale_price = floatval( strip_tags($sale_price) );

    // Percentage calculation and text
    $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
    $percentage_txt = __(' Save ', 'woocommerce' ).$percentage;

    return '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . $percentage_txt . '</ins>';
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'mmx_remove_select_text');

如果我只保留第二个功能,它适用于所有产品,但不适用于可变产品,后者在商店页面上显示价格范围。如果我添加第一个函数,它会显示最低产品变化的价格而不是价格范围,但旁边没有折扣百分比。

我可以将第二个函数的 % 代码添加到第一个函数中,但这很麻烦,有没有办法合并这些函数,以便我可以在任何地方显示折扣 %,并且在最低变化旁边显示可变产品?

【问题讨论】:

    标签: php wordpress woocommerce product price


    【解决方案1】:

    这可以通过稍微改变将替换两个挂钩函数的第一个函数来完成:

    add_filter( 'woocommerce_get_price_html', 'custom_price_format', 10, 2 );
    add_filter( 'woocommerce_variable_price_html', 'custom_price_format', 10, 2 );
    function custom_price_format( $price, $product ) {
    
        // Main Price
        $regular_price = $product->is_type('variable') ? $product->get_variation_regular_price( 'min', true ) : $product->get_regular_price();
        $sale_price = $product->is_type('variable') ? $product->get_variation_sale_price( 'min', true ) : $product->get_sale_price();
    
    
        if ( $regular_price !== $sale_price && $product->is_on_sale()) {
            // Percentage calculation and text
            $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
            $percentage_txt = __(' Save', 'woocommerce' ).' '.$percentage;
    
            $price = '<del>' . wc_price($regular_price) . '</del> <ins>' . wc_price($sale_price) . $percentage_txt . '</ins>';
        }
        return $price;
    }
    

    此代码位于活动子主题(或主题)的 function.php 文件中。

    经过测试并与您在之前的问题/答案中进行的自定义配合使用。

    【讨论】:

    • @malcom。您可以将帖子标记为已回答吗?这将有助于将来其他人可能会在该问题上寻求帮助。只是建议,不是命令! :-)
    • 验证答案存在延迟,现在标记为完成!
    猜你喜欢
    • 2018-07-06
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多