在 woodmart 迷你購物車小部件中添加簡單和可變產品的總重量
P粉447495069
P粉447495069 2023-09-05 00:18:25
0
1
574

我正在使用 Woodmart 主題和迷你購物車小部件,我想顯示簡單和可變產品的總重量和總價格。 所以我修改了程式碼,但它不起作用並且有以下問題:

<強>1。 (總重量):當簡單或可變產品添加到購物車時,總重量顯示為產品重量的一半。例如,如果產品重量設定為 0.5 ,當加入購物車時,迷你購物車上的總重量顯示為 0.25。

<強>2。 (總價):當簡單或可變產品添加到購物車時,總價顯示為產品價格的一半。例如,如果基於重量 (0.5) 的產品價格為 7500,當添加到購物車時,迷你購物車上的總價顯示為 3750。

感謝任何幫助。非常感謝。 這是我的程式碼:

/* Display the total weight in the mini cart shopping cart widget footer*/ function display_mini_cart_total_weight() { if ( ! WC()->cart->is_empty() ) { $total_weight = 0; foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $product = $cart_item['data']; $variation_id = $cart_item['variation_id']; $weight = 0; if ( $variation_id ) { // Get the selected variation $variation = wc_get_product( $variation_id ); if ( $variation ) { // Get the weight of the variation $weight = $variation->get_weight(); } } else { // Get the weight of the product $weight = $product->get_weight(); } $quantity = $cart_item['quantity']; // Calculate the weight for the current product $product_weight = $weight * $quantity; // Add the product's weight to the total weight $total_weight = $product_weight; } // Output the total weight in the mini cart shopping cart widget footer $total_weight_display = $total_weight . ' Kg'; // Append ' Kg' to the total weight echo '  

' . __('Total Weight:', 'chahar-4-rahewordpress') . '

' . $total_weight_display . '

'; } } add_action( 'woocommerce_widget_shopping_cart_before_buttons', 'display_mini_cart_total_weight' );

P粉447495069
P粉447495069

全部回覆 (1)
P粉681400307

您可以檢查數量是否小於 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 '  

' . __('Total Weight:', 'chahar-4-rahewordpress') . '

' . $total_weight_display . '

'; echo '

' . __('Total Price 1:', 'chahar-4-rahewordpress') . '

' . $total_price_display . '

'; } } add_action( 'woocommerce_widget_shopping_cart_before_buttons', 'display_mini_cart_total_weight' );
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板
    關於我們 免責聲明 Sitemap
    PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!