【问题标题】:WooCommerce default variation display priceWooCommerce 默认变化显示价格
【发布时间】:2022-01-03 03:42:49
【问题描述】:
我需要在此处更改一些内容以在我的商店页面上显示默认变化价格:
<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>
你能帮帮我吗?
【问题讨论】:
标签:
woocommerce
hook-woocommerce
【解决方案1】:
我只是像这样更改代码,并为我工作:
<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 中进行了这些更改
这是我的简单解决方案,效果很好。
【解决方案2】:
解决方案
第一步:
首先,您可以编辑可变产品并选择变体值。哪种变体
您希望在产品页面上显示 默认表单值 见截图Select Default value screenshot
步骤: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% 正常工作
谢谢