Ajoutez le poids total des produits simples et variables dans le widget du mini panier Woodmart
P粉447495069
P粉447495069 2023-09-05 00:18:25
0
1
597
<p>J'utilise le thème Woodmart et le widget mini-panier et je souhaite afficher le poids total et le prix total des produits simples et variables. J'ai donc modifié le code mais il ne fonctionne pas et a le problème suivant : </p> <p><Qiang>1. (Poids total) : </strong> Lorsqu'un produit simple ou variable est ajouté au panier, le <strong>Poids total</strong> est affiché comme la moitié du poids du produit. Par exemple, si le poids du produit est défini sur 0,5, une fois ajouté au panier, le poids total sur le mini-chariot s'affiche à 0,25. </p> <p><Qiang>2. (Prix total) : </strong> Lorsqu'un produit simple ou variable est ajouté au panier, le <strong> Prix total</strong> est affiché comme étant la moitié du prix du produit. Par exemple, si le prix d'un produit basé sur le poids (0,5) est de 7 500, une fois ajouté au panier, le prix total sur le mini panier s'affiche comme 3 750. </p> <p>Merci pour toute aide. Merci beaucoup.这是我的代码:</p> <pre class="brush:php;toolbar:false;">/* Afficher le poids total dans le pied de page du widget du mini panier*/ fonction display_mini_cart_total_weight() { if ( ! WC()->cart->is_empty() ) { $poids_total = 0 ; foreach ( WC()->cart->get_cart() comme $cart_item_key => $cart_item ) { $produit = $cart_item['data']; $variation_id = $cart_item['variation_id']; $poids = 0 ; si ( $variation_id ) { // Récupère la variante sélectionnée $variation = wc_get_product( $variation_id ); si ( $variation ) { // Récupère le poids de la variation $poids = $variation->get_weight(); } } autre { // Récupère le poids du produit $poids = $produit->get_weight(); } $quantité = $cart_item['quantité']; // Calcule le poids du produit actuel $product_weight = $poids * $quantité ; // Ajoutez le poids du produit au poids total $total_weight += $product_weight ; } // Afficher le poids total dans le pied de page du widget de panier d'achat du mini panier $total_weight_display = $total_weight . ' Kg'; // Ajouter 'Kg' au poids total echo '<tr class="ligne-poids-total"> <td colspan="3" class="cellule-poids-total"> <p class="total-weight-label woocommerce-mini-cart__total">' . __('Poids total :', 'chahar-4-rahewordpress') . '≪/p> <p class="total-weight-value woocommerce-Prix-montant montant">' . $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

répondre à tous(1)
P粉681400307

Vous pouvez vérifier si la quantité est inférieure à 1, alors la quantité minimale doit être considérée comme 1. Veuillez vérifier le code suivant.

/* 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' );
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!