提问者:小点点

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个答案

匿名用户

请将以下代码添加到当前主题的函数中。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;
}

匿名用户

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