Saya menggunakan tema Woodmart dan widget troli mini dan saya mahu memaparkan jumlah berat dan jumlah harga produk ringkas dan berubah-ubah. Jadi saya mengubah suai kod tetapi ia tidak berfungsi dan mempunyai masalah berikut:
Terima kasih atas sebarang bantuan. Terima kasih banyak-banyak.这是我的代码:
/* Paparkan jumlah berat dalam pengaki widget troli beli-belah troli mini*/ function display_mini_cart_total_weight() { jika ( ! WC()->cart->is_kosong() ) { $total_weight = 0; foreach ( WC()->cart->get_cart() sebagai $cart_item_key => $cart_item ) { $produk = $cart_item['data']; $variation_id = $cart_item['variation_id']; $berat = 0; jika ($variation_id) { // Dapatkan variasi yang dipilih $variasi = wc_get_product( $variation_id ); jika ($variation) { // Dapatkan berat variasi $berat = $variasi->dapatkan_berat(); } } lain { // Dapatkan berat produk $weight = $product->get_weight(); } $kuantiti = $cart_item['kuantiti']; // Kira berat untuk produk semasa $berat_produk = $berat * $kuantiti; // Tambahkan berat produk kepada jumlah berat $total_weight += $product_weight; } // Keluarkan jumlah berat dalam pengaki widget troli beli-belah mini $total_weight_display = $total_weight . ' Kg'; // Tambah ' Kg' kepada jumlah berat echo ' ' . __('Jumlah Berat:', 'chahar-4-rahewordpress') . '
' . $total_weight_display . '
'; } } add_action( 'woocommerce_widget_shopping_cart_before_buttons', 'display_mini_cart_total_weight' );
Boleh semak sama ada kuantiti kurang dari 1, maka kuantiti minimum mesti dianggap sebagai 1. Sila semak kod berikut.
/* 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 ' '; echo ' '; } } add_action( 'woocommerce_widget_shopping_cart_before_buttons', 'display_mini_cart_total_weight' );' . __('Total Weight:', 'chahar-4-rahewordpress') . '
' . $total_weight_display . '
' . __('Total Price 1:', 'chahar-4-rahewordpress') . '
' . $total_price_display . '