Add total weight of simple and variable products in woodmart mini cart widget
P粉447495069
P粉447495069 2023-09-05 00:18:25
0
1
590
<p>I am using Woodmart theme and mini cart widget and I want to display total weight and total price of simple and variable products. So I modified the code but it doesn't work and has the following problem: </p> <p><Qiang>1. (Total Weight): </strong>When a simple or variable product is added to the cart, the <strong>Total Weight</strong> is displayed as half the product weight. For example, if the product weight is set to 0.5, when added to the cart, the total weight on the mini cart displays as 0.25. </p> <p><Qiang>2. (Total Price): </strong>When a simple or variable product is added to the cart, the <strong>Total Price</strong> is displayed as half the product price. For example, if the price of a product based on weight (0.5) is 7500, when added to the cart, the total price on the mini cart is displayed as 3750. </p> <p>Thanks for any help. Thank you so much. This is my code: </p> <pre class="brush:php;toolbar:false;">/* 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 '<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>'; } } add_action( 'woocommerce_widget_shopping_cart_before_buttons', 'display_mini_cart_total_weight' );</pre></p>
P粉447495069
P粉447495069

reply all(1)
P粉681400307

You can check if the quantity is less than 1, then the minimum quantity must be considered 1. Please check the following code.

/* 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' );
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!