WooCommerce默认变化显示价格



我需要在这里更改一些内容,以在我的商店页面上显示默认的变化价格:

<div class="deal-part2-details">
<?php
if($product->get_type() == "simple"){
?>
<h5><?php echo get_field('network_price_text',$p_id); ?> <?php echo $currency;  echo $product->regular_price; ?>pm</h5>
<h5 class="our-price"><?php echo get_field('our_price_text',$p_id); ?> : <?php echo $currency; echo $product->sale_price; ?>pm</h5>
<h6><?php echo get_field('upfront_cost',$p_id); ?> <?php echo get_field('upfront_cost_text',$p_id); ?></h6>
<?php
}else{
$product_variations = $product->get_available_variations();
$variation_product_id = $product_variations [0]['variation_id'];
$variation_product = new WC_Product_Variation( $variation_product_id );

?>
<h5 class="network-price"><?php echo get_field('network_price_text',$p_id); ?> <?php echo $currency;  echo $variation_product->regular_price; ?>pm</h5>
<h5 class="our-price"><?php echo get_field('our_price_text',$p_id); ?> : <?php echo $currency; echo $variation_product->sale_price; ?>pm</h5>

<!--<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo $price_html; ?></span>
<?php endif; ?> -->

<h6><?php echo get_field('upfront_cost',$p_id); ?><?php echo get_field('upfront_cost_text',$p_id); ?></h6>
<?php
}
?>
</div>

你能帮我吗?

我只是这样更改代码,并为我工作:

<div class="deal-part2-details">
<?php
if($product->get_type() == "simple"){
?>
<h5><?php echo get_field('network_price_text',$p_id); ?> <?php echo $currency;  echo $product->regular_price; ?>pm</h5>
<h5 class="our-price"><?php echo get_field('our_price_text',$p_id); ?> : <?php echo $currency; echo $product->sale_price; ?>pm</h5>
<h6><?php echo get_field('upfront_cost',$p_id); ?> <?php echo get_field('upfront_cost_text',$p_id); ?></h6>
<?php
}else{
$product_variations = $product->get_available_variations();
$variation_product_id = $product_variations [0]['variation_id'];
$variation_product = new WC_Product_Variation( $variation_product_id );
$price_regular_min = $product->get_variation_regular_price();
$price_sale_min = $product->get_variation_sale_price();
?>
<h5 class="network-price"><?php echo get_field('network_price_text',$p_id); ?> <?php echo $currency;  echo $price_regular_min; ?>pm</h5>
<h5 class="our-price"><?php echo get_field('our_price_text',$p_id); ?> : <?php echo $currency; echo $price_sale_min; ?>pm</h5>

<!--<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo $price_html; ?></span>
<?php endif; ?> -->

<h6><?php echo get_field('upfront_cost',$p_id); ?><?php echo get_field('upfront_cost_text',$p_id); ?></h6>
<?php
}
?>
</div>

对于这一点,我只添加了两个变量:

$price_regular_min = $product->get_variation_regular_price();
$price_sale_min = $product->get_variation_sale_price();

然后我在这里激活了它们:

<h5 class="network-price"><?php echo get_field('network_price_text',$p_id); ?> <?php echo $currency; echo $price_regular_min; ?>pm</h5>
<h5 class="our-price"><?php echo get_field('our_price_text',$p_id); ?> : <?php echo $currency; echo $price_sale_min; ?>pm</h5>

我在loop-start.php中进行了这些更改这是我的简单解决方案,效果很好。

解决方案

步骤1:首先您可以编辑可变产品&选择变化值。哪种变体您想显示在产品页面上默认表单值请参阅屏幕截图选择默认值屏幕截图

步骤:2然后转到代码下面的活动主题或子主题functions.php文件。

//woo_default_price_variation_price
add_filter('woocommerce_variable_price_html', 'woo_default_price_variation_price', 10, 2);
function woo_default_price_variation_price( $price, $product ) {
foreach($product->get_available_variations() as $pav){
$def=true;
foreach($product->get_variation_default_attributes() as $defkey=>$defval){
if($pav['attributes']['attribute_'.$defkey]!=$defval){
$def=false; 
}
}
if($def){
$price = $pav['display_price']; 
}
} 
return woocommerce_price($price);
}

它是100%工作的

感谢

最新更新