php - 在 Woocommerce 3 上显示默认的变化价格和储蓄金额

标签 php woocommerce product price variations

我需要在我的 上显示默认变化价格和正常价格和储蓄金额Woocommerce 主页和分类页面

我在此链接答案中找到了以下代码
Display lowest variation price and discounted percentage in WooCommerce

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;
}

现在这段代码在我的网站上完美运行,但这段代码显示了“最低”变化的价格和节省的百分比,我需要显示“默认”变化的价格和实际节省金额。

先感谢您。

最佳答案

尝试以下操作,我们首先搜索可变产品,即默认的变化价格。

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 ) {

    // 1. Variable products
    if( $product->is_type('variable') ){

        // Searching for the default variation
        $default_attributes = $product->get_default_attributes();
        // Loop through available variations
        foreach($product->get_available_variations() as $variation){
            $found = true; // Initializing
            // Loop through variation attributes
            foreach( $variation['attributes'] as $key => $value ){
                $taxonomy = str_replace( 'attribute_', '', $key );
                // Searching for a matching variation as default
                if( isset($default_attributes[$taxonomy]) && $default_attributes[$taxonomy] != $value ){
                    $found = false;
                    break;
                }
            }
            // When it's found we set it and we stop the main loop
            if( $found ) {
                $default_variaton = $variation;
                break;
            } // If not we continue
            else {
                continue;
            }
        }
        // Get the default variation prices or if not set the variable product min prices
        $regular_price = isset($default_variaton) ? $default_variaton['display_price']: $product->get_variation_regular_price( 'min', true );
        $sale_price = isset($default_variaton) ? $default_variaton['display_regular_price']: $product->get_variation_sale_price( 'min', true );
    }
    // 2. Other products types
    else {
        $regular_price = $product->get_regular_price();
        $sale_price    = $product->get_sale_price();
    }

    // Formatting the 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 文件中。测试和工作。

Now this code will never get the selected variation price as it's a live event on client browser that is much more complicated to handle (Php and Jquery will be needed in this case).



相关链接答案:Display lowest variation price and discounted percentage in WooCommerce

关于php - 在 Woocommerce 3 上显示默认的变化价格和储蓄金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50234781/

相关文章:

php - 一对一关系/唯一 key 对

php - 安装另一个 php 版本

php - Magento:在可配置产品上显示关联产品的图像

javascript - 不需要的 "Expand"链接不会消失。无法追查来源。显示:none; not working.(Wordpress)

wordpress - Woocommerce REST API 扩展订单响应

php - 如何在表格中显示多个类别的产品

css - Woocommerce 按钮,底部的灰色区域并填满整个屏幕

php - PDO 在一条语句中绑定(bind)多个值

php - 从 stdClass 对象中删除 stdClass 对象和 count(post_id) ( [count(post_id)] => 1 ) 仅需要结果 1

php - wordpress/woocommerce : Remove price if zero