显示woocommerce可变产品价格 [英] Display woocommerce variable product price

查看:37
本文介绍了显示woocommerce可变产品价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to display woocommerce variable price for current active variation on single product page? I use the code:

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

但它只显示最低可变价格,而不是当前选择的变体价格.任何想法如何显示活动变化的当前价格?

But it shows only lowest variable price instead of currently selected variation's price. Any ideas how to display current price for active variation?

推荐答案

所以,你可以修改\your-theme\woocommerce\single-product\sale-flash.php文件

So, you can just modify \your-theme\woocommerce\single-product\sale-flash.php file

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

Or, your can also use filter. By the way there is more simple solution:

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/4064fc2b656c0828285c>

Or you can copy this gist https://gist.github.com/sholex/4064fc2b656c0857a78228cf5324b370

这篇关于显示woocommerce可变产品价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆