我需要显示产品的毛重和净重,并根据数量更改毛重和净重。在改变数量的同时,净重和毛重也应该改变。它在单个产品页面上运行良好,而分组产品无法实现这一点。
这是我的单一产品工作正常:https ://hamarafresh.com/product/salmon/
这是我的分组产品不起作用:https ://hamarafresh.com/product/lady-fish/
我从自定义字段中获取毛重和净重。
add_action( 'woocommerce_after_quantity_input_field', 'woocommerce_total_product_price', 31 );
function woocommerce_total_product_price() {
if ( is_product() ) {
global $woocommerce, $product;
$gross_weight = get_post_custom_values($key = 'gross_weight');
$net_weight = get_post_custom_values($key = 'net_weight');
get_post_meta( $post_id, $key, $single );
// let's setup our divs
?>
<?php $netweight5= esc_html( get_post_meta( get_the_ID(), 'net_weight', true ) ); ?>
<?php $grossweight5= esc_html( get_post_meta( get_the_ID(), 'gross_weight', true ) ); ?>
<?php
echo sprintf('<div id="net_weight_disp" style="margin-bottom:10px; margin-top:7px;">%s %s</div>',__('Net Weight:'),'<span class="price">'.$netweight5.'</span>');
echo sprintf('<div id="gross_weight_disp" style="margin-bottom:10px; margin-top:7px;">%s %s</div>',__('Gross Weight:'),'<span class="price">'.$grossweight5.'</span>');
echo sprintf('<div id="product_total_price" style="margin-bottom:20px; text-align: center;">%s %s</div>',__('Product Total:','woocommerce'),'<span class="price">'.$product->get_price().'</span>');
?>
<script>
jQuery(function($){
var price = <?php echo $product->get_price(); ?>,
currency = '<?php echo get_woocommerce_currency_symbol(); ?>';
<?php $net_weight = get_post_custom_values($key = 'net_weight'); ?>;
<?php $netweight5= esc_html( get_post_meta( get_the_ID(), 'net_weight', true ) ); ?>;
var net_weightt = <?php echo $netweight5; ?>;
<?php $gross_weight = get_post_custom_values($key = 'gross_weight'); ?>;
<?php $grossweight5= esc_html( get_post_meta( get_the_ID(), 'gross_weight', true ) ); ?>;
var gross_weightt = <?php echo $grossweight5 ?>;
$('[name=quantity]').change(function(){
if (!(this.value < 0.5)) {
var net_weighttt = (net_weightt* this.value);
var gross_weighttt = (gross_weightt* this.value);
var product_total = parseFloat(price * this.value);
$('#product_total_price .price').html( currency + product_total.toFixed(2));
$('#net_weight_disp .price').html( currency + net_weighttt.toFixed(2));
$('#gross_weight_disp .price').html( currency + gross_weighttt.toFixed(2));
}
});
});
</script>
<?php
}
}
