在单个产品页面上,我试图显示每克产品的价格。因此,价格除以重量。
我可以不带货币符号的价格回声:
<?php global $post; $product = new WC_Product($post->ID); echo wc_price($product->get_price_including_tax(1,$product->get_price())); ?>
我也可以在不带“ g”的情况下回应重量:
<?php global $product; $attributes = $product->get_attributes(); if ( $product->has_weight() ) { echo $product->get_weight();} ?>
我知道如何创建一个简单的计算:
<?php $first_number = 10; $second_number = 20; $sum_total = $second_number / $first_number; print ($sum_total); ?>
但是不确定如何将它们放在一起!
您的代码中有一些错误。由于get_price_including_tax()
不赞成使用WooCommerce 3方法,而由wc_get_price_including_tax()
函数代替。另外,您需要使用非格式化价格进行计算。
请尝试以下方法:
<?php
global $product;
if ( ! is_a( $product, 'WC_Product' ) ) {
$product = wc_get_product( get_the_ID() );
}
$price = wc_get_price_including_tax( $product );
$weight = $product->get_weight();
if( $product->has_weight() ) {
// echo __("Weight") . ': ' . wc_format_weight( $weight ) . '<br>'; // Uncomment if needed
echo wc_price( $price / $weight ) . ' ' . __("per gram");
}
?>
它应该工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句