/* 在小车购物车小部件页脚中显示总重量*/ 函数display_mini_cart_total_weight() { if ( !WC()->购物车->is_empty() ) { $总权重 = 0; foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $产品 = $cart_item['数据']; $variation_id = $cart_item['variation_id']; $权重=0; 如果($variation_id){ // 获取选定的变体 $variation = wc_get_product( $variation_id ); 如果($变化){ // 获取变异的权重 $weight = $variation->get_weight(); } } 别的 { // 获取产品的重量 $weight = $product->get_weight(); } $数量 = $cart_item['数量']; // 计算当前产品的重量 $产品重量 = $重量 * $数量; // 将产品的重量添加到总重量中 $total_weight += $product_weight; } // 在迷你购物车购物车小部件页脚中输出总重量 $total_weight_display = $total_weight 。 ' 公斤'; // 将“Kg”添加到总重量中 echo '<td colspan=“3” class="total-weight-cell"> <p class="total-weight-label woocommerce-mini-cart__total">' 。 __('总重量:', 'chahar-4-rahewordpress') . '</p> <p class="total-weight-value woocommerce-Price-amount amount">' 。 $total_weight_display 。 '</p> </td> </tr>'; } } add_action( 'woocommerce_widget_shopping_cart_before_buttons', 'display_mini_cart_total_weight' );</pre></p> 全部回复(1)我来回复
P粉6814003072023-09-05 22:03:41
您可以检查数量是否小于 1,则必须将最小数量视为 1。请检查以下代码。
/* Display the total weight and price in the mini cart shopping cart widget footer */ function display_mini_cart_total_weight() { if ( ! WC()->cart->is_empty() ) { $total_weight = 0; $total_price = 0; foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $weight = 0; $price = 0; $product = $cart_item['data']; $variation_id = $cart_item['variation_id']; if ( $variation_id ) { // Get the selected variation $variation = wc_get_product( $variation_id ); if ( $variation ) { // Get the weight and price of the variation $weight = $variation->get_weight(); $price = $variation->get_price(); } } else { // Get the weight and price of the product $weight = $product->get_weight(); $price = $product->get_price(); } $quantity = $cart_item['quantity']; if( $quantity < 1 ){ $quantity = 1; } // Calculate the weight and price of the current product $product_weight = $weight * $quantity; $product_price = $price * $quantity; // Add the product's weight and price to the total weight and price $total_weight += $product_weight; $total_price += $product_price; } // Output the total weight and price in the mini cart shopping cart widget footer $total_weight_display = $total_weight . ' Kg'; // Append ' Kg' to the total weight $total_price_display = wc_price( $total_price ); // Format the total price as WooCommerce price echo '<tr class="total-weight-row"> <td colspan="3" class="total-weight-cell"> <p class="total-weight-label woocommerce-mini-cart__total">' . __('Total Weight:', 'chahar-4-rahewordpress') . '</p> <p class="total-weight-value woocommerce-Price-amount amount">' . $total_weight_display . '</p> </td> </tr>'; echo '<tr class="total-price-row"> <td colspan="3" class="total-price-cell"> <p class="total-price-label woocommerce-mini-cart__total">' . __('Total Price 1:', 'chahar-4-rahewordpress') . '</p> <p class="total-price-value woocommerce-Price-amount amount">' . $total_price_display . '</p> </td> </tr>'; } } add_action( 'woocommerce_widget_shopping_cart_before_buttons', 'display_mini_cart_total_weight' );回复0取消