显示woocommerce可变产品价格

时间:2017-06-21 11:46:45

标签: php woocommerce variation

如何在单个产品页面上显示当前有效变化的woocommerce变量价格? 我使用代码:

                <?php 
                global $product;
                if ($product->is_type( 'simple' )) { ?>
                    <p class="price"><?php echo $product->get_price_html(); ?></p>
                <?php } ?>
                <?php 
                if($product->product_type=='variable') {
                    $available_variations = $product->get_available_variations();
                    $variation_id=$available_variations[0]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.
                    $variable_product1= new WC_Product_Variation( $variation_id );
                    $regular_price = $variable_product1 ->regular_price;
                    $sales_price = $variable_product1 ->sale_price;
                    echo $regular_price+$sales_price;
                    }
                ?>

但它只显示最低的可变价格而不是当前选择的变量价格。 有关如何显示当前活动变化价格的想法吗?

4 个答案:

答案 0 :(得分:5)

<?php 
                global $product;
                if ($product->is_type( 'simple' )) { ?>
                    <p class="price"><?php echo $product->get_price_html(); ?></p>
                <?php } ?>
                <?php 
                if($product->product_type=='variable') {
                    $available_variations = $product->get_available_variations();
$count = count($available_variations)-1;
                    $variation_id=$available_variations[$count]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.
                    $variable_product1= new WC_Product_Variation( $variation_id );
                    $regular_price = $variable_product1 ->regular_price;
                    $sales_price = $variable_product1 ->sale_price;
                    echo $regular_price+$sales_price;
                    }
                ?>

试试这个。这可能会对你有帮助。

答案 1 :(得分:1)

<?php 
                global $product;
                if ($product->is_type( 'simple' )) { ?>
                    <p class="price"><?php echo $product->get_price_html(); ?></p>
                <?php } ?>
                <?php 
                if($product->product_type=='variable') {
                    $available_variations = $product->get_available_variations();
foreach($available_variations as $key=>$val){ 
     if(trim($val['variation_id'])==**"your selected variant id"**){
                    $variation_id=$available_variations[$key]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.
     }
}
                    $variable_product1= new WC_Product_Variation( $variation_id );
                    $regular_price = $variable_product1 ->regular_price;
                    $sales_price = $variable_product1 ->sale_price;
                    echo $regular_price+$sales_price;
                    }
                ?>

答案 2 :(得分:1)

因此,您只需修改\ your-theme \ woocommerce \ single-product \ sale-flash.php文件

或者,您也可以使用过滤器。 顺便说一下,有一个更简单的解决方案:

if ($product->is_type( 'simple' )) {
        $sale_price     =  $product->get_sale_price();
        $regular_price  =  $product->get_regular_price();
    }
    elseif($product->is_type('variable')){
        $sale_price     =  $product->get_variation_sale_price( 'min', true );
        $regular_price  =  $product->get_variation_regular_price( 'max', true );
    }


    $discount = round (($sale_price / $regular_price -1 ) * 100);
}

或者您可以复制要点https://gist.github.com/sholex/4064fc2b656c0857a78228cf5324b370

答案 3 :(得分:0)

    add_action( 'woocommerce_before_single_product', 'check_if_variable_first' );
function check_if_variable_first(){
    if ( is_product() ) {
        global $post;
        $product = wc_get_product( $post->ID );
        if ( $product->is_type( 'variable' ) ) {
            // removing the price of variable products
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );

// Change location of
add_action( 'woocommerce_single_product_summary', 'custom_wc_template_single_price', 10 );
function custom_wc_template_single_price(){
    global $product;

// Variable product only
if($product->is_type('variable')):

    // 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 && $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() ){
                $('p.price').html($('div.woocommerce-variation-price > span.price').html()).append('<p class="availability">'+$('div.woocommerce-variation-availability').html()+'</p>');
                console.log($('input.variation_id').val());
            } else {
                $('p.price').html($('div.hidden-variable-price').html());
                if($('p.availability'))
                    $('p.availability').remove();
                console.log('NULL');
            }
        });
    });
    </script>
    <?php

    echo '<p class="price">'.$price.'</p>
    <div class="hidden-variable-price" >'.$price.'</div>';

endif;
}

        }
    }
}