Woocommerce变化价格得到

时间:2016-04-19 09:41:59

标签: wordpress woocommerce

我想获得实际的变动价格。

代码默认woocommerce(/plugins/woocommerce/templates/single-product/add-to-cart/variable.php):

<div class="woocommerce-variation-price">
 {{{ data.variation.price_html }}}  </div>

我如何获取“data.variation.price_html”?

我试过了:

$pricenow =  $product->get_variation_price();
<? echo $pricenow; ?>

但是,只显示最低价格......

我如何做到这一点,以便您的首选价格范围?

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

请将以下代码添加到您当前主题的function.php文件中,并查看正面的更改(即变量产品详细信息页面)

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
    global $wpdb, $product;

    if ( empty( $term ) ) return $term;
    if ( empty( $product->id ) ) return $term;

    $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );

    $term_slug = ( !empty( $result ) ) ? $result[0] : $term;

    $query = "SELECT postmeta.post_id AS product_id
                FROM {$wpdb->prefix}postmeta AS postmeta
                    LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
                WHERE postmeta.meta_key LIKE 'attribute_%'
                    AND postmeta.meta_value = '$term_slug'
                    AND products.post_parent = $product->id";

    $variation_id = $wpdb->get_col( $query );

    $parent = wp_get_post_parent_id( $variation_id[0] );

    if ( $parent > 0 ) {
         $_product = new WC_Product_Variation( $variation_id[0] );
         return $term . ' (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')';
    }
    return $term;
}

答案 1 :(得分:0)

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