在WoodMart迷你购物车小部件中为简单和变量产品添加总重量

huangapple go评论79阅读模式
英文:

Adding Total Weight for both simple and variable product In woodmart mini cart widget

问题

以下是您要翻译的代码部分:

/* 在小购物车小部件页脚中显示总重量 */
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 ) {
                // 获取所选的变种
                $variation = wc_get_product( $variation_id );

                if ( $variation ) {
                    // 获取变种的重量
                    $weight = $variation->get_weight();
                }
            } else {
                // 获取产品的重量
                $weight = $product->get_weight();
            }

            $quantity = $cart_item['quantity'];

            // 计算当前产品的重量
            $product_weight = $weight * $quantity;

            // 将产品的重量添加到总重量中
            $total_weight += $product_weight;
        }

        // 在小购物车小部件页脚中输出总重量
        $total_weight_display = $total_weight . '千克'; // 在总重量后添加 '千克'
        
        echo '<tr class="total-weight-row">
                <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' );

请注意,代码已经翻译成中文。

英文:

I am using Woodmart theme and on mini cart widget and I want to show total weight and total price of simple and variable product.
So I have modified the code but it is not working and has these problem below:

1. (Total weight): when a simple or variable product added to cart the total weight is shown half of the product weight. For example if the product weight set to 0.5 , when added to cart , on mini cart the total weight is shown 0.25.

2. (Total Price): when a simple or variable product added to cart the total price is shown half of the product price. For example if the product price based on weight (0.5) is 7500, when added to cart , on mini cart the total price is shown 3750.

在WoodMart迷你购物车小部件中为简单和变量产品添加总重量

Any help is appreciated. So many thanks.
Here is my code:

/* Display the total weight in the mini cart shopping cart widget footer*/
function display_mini_cart_total_weight() {
    if ( ! WC()-&gt;cart-&gt;is_empty() ) {
        $total_weight = 0;

        foreach ( WC()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $cart_item ) {
            $product = $cart_item[&#39;data&#39;];
            $variation_id = $cart_item[&#39;variation_id&#39;];
            $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-&gt;get_weight();
                }
            } else {
                // Get the weight of the product
                $weight = $product-&gt;get_weight();
            }

            $quantity = $cart_item[&#39;quantity&#39;];

            // Calculate the weight for the current product
            $product_weight = $weight * $quantity;

            // Add the product&#39;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 . &#39; Kg&#39;; // Append &#39; Kg&#39; to the total weight
        
        echo &#39;&lt;tr class=&quot;total-weight-row&quot;&gt;
                &lt;td colspan=&quot;3&quot; class=&quot;total-weight-cell&quot;&gt;
                    &lt;p class=&quot;total-weight-label woocommerce-mini-cart__total&quot;&gt;&#39; . __(&#39;Total Weight:&#39;, &#39;chahar-4-rahewordpress&#39;) . &#39;&lt;/p&gt;
                    &lt;p class=&quot;total-weight-value woocommerce-Price-amount amount&quot;&gt;&#39; . $total_weight_display . &#39;&lt;/p&gt;
                &lt;/td&gt;
            &lt;/tr&gt;&#39;;
    }
}

add_action( &#39;woocommerce_widget_shopping_cart_before_buttons&#39;, &#39;display_mini_cart_total_weight&#39; );

答案1

得分: 1

以下是您要求的代码的中文翻译部分:

/* 在小购物车挂件底部显示总重量和价格 */
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 ) {
                // 获取所选变种
                $variation = wc_get_product( $variation_id );

                if ( $variation ) {
                    // 获取变种的重量和价格
                    $weight = $variation->get_weight();
                    $price = $variation->get_price();
                }
            } else {
                // 获取产品的重量和价格
                $weight = $product->get_weight();
                $price = $product->get_price();
            }

            $quantity = $cart_item['quantity'];

            if( $quantity < 1 ){
                $quantity = 1;
            }

            // 计算当前产品的重量和价格
            $product_weight = $weight * $quantity;
            $product_price = $price * $quantity;

            // 将产品的重量和价格添加到总重量和价格中
            $total_weight += $product_weight;
            $total_price += $product_price;
        }

        // 在小购物车挂件底部输出总重量和价格
        $total_weight_display = $total_weight . ' Kg'; // 在总重量后附加' Kg'
        $total_price_display = wc_price( $total_price ); // 将总价格格式化为 WooCommerce 价格

        echo '<tr class="total-weight-row">
                <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>';

        echo '<tr class="total-price-row">
                <td colspan="3" class="total-price-cell">
                    <p class="total-price-label woocommerce-mini-cart__total">' . __('总价格 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' );

请注意,我已将代码翻译为中文,但其中的变量名和函数名等部分保持不变。

英文:

You can check if the quantity is less than 1 then you have to consider min qty as 1. Check the below code.

/* Display the total weight and price in the mini cart shopping cart widget footer */
function display_mini_cart_total_weight() {
if ( ! WC()-&gt;cart-&gt;is_empty() ) {
$total_weight = 0;
$total_price = 0;
foreach ( WC()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $cart_item ) {
$weight = 0;
$price = 0;
$product = $cart_item[&#39;data&#39;];
$variation_id = $cart_item[&#39;variation_id&#39;];
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-&gt;get_weight();
$price = $variation-&gt;get_price();
}
} else {
// Get the weight and price of the product
$weight = $product-&gt;get_weight();
$price = $product-&gt;get_price();
}
$quantity = $cart_item[&#39;quantity&#39;];
if( $quantity &lt; 1 ){
$quantity = 1;
}
// Calculate the weight and price of the current product
$product_weight = $weight * $quantity;
$product_price = $price * $quantity;
// Add the product&#39;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 . &#39; Kg&#39;; // Append &#39; Kg&#39; to the total weight
$total_price_display = wc_price( $total_price ); // Format the total price as WooCommerce price
echo &#39;&lt;tr class=&quot;total-weight-row&quot;&gt;
&lt;td colspan=&quot;3&quot; class=&quot;total-weight-cell&quot;&gt;
&lt;p class=&quot;total-weight-label woocommerce-mini-cart__total&quot;&gt;&#39; . __(&#39;Total Weight:&#39;, &#39;chahar-4-rahewordpress&#39;) . &#39;&lt;/p&gt;
&lt;p class=&quot;total-weight-value woocommerce-Price-amount amount&quot;&gt;&#39; . $total_weight_display . &#39;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;&#39;;
echo &#39;&lt;tr class=&quot;total-price-row&quot;&gt;
&lt;td colspan=&quot;3&quot; class=&quot;total-price-cell&quot;&gt;
&lt;p class=&quot;total-price-label woocommerce-mini-cart__total&quot;&gt;&#39; . __(&#39;Total Price 1:&#39;, &#39;chahar-4-rahewordpress&#39;) . &#39;&lt;/p&gt;
&lt;p class=&quot;total-price-value woocommerce-Price-amount amount&quot;&gt;&#39; . $total_price_display . &#39;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;&#39;;
}
}
add_action( &#39;woocommerce_widget_shopping_cart_before_buttons&#39;, &#39;display_mini_cart_total_weight&#39; );

huangapple
  • 本文由 发表于 2023年7月6日 19:59:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628595.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定