在 woodmart 迷你购物车小部件中添加简单和可变产品的总重量
P粉447495069
P粉447495069 2023-09-05 00:18:25
[PHP讨论组]
<p>我正在使用 Woodmart 主题和迷你购物车小部件,我想显示简单和可变产品的总重量和总价格。 所以我修改了代码,但它不起作用并且存在以下问题:</p> <p><强>1。 (总重量):</strong>当简单或可变产品添加到购物车时,<strong>总重量</strong>显示为产品重量的一半。例如,如果产品重量设置为 0.5 ,当添加到购物车时,迷你购物车上的总重量显示为 0.25。</p> <p><强>2。 (总价):</strong>当简单或可变产品添加到购物车时,<strong>总价</strong>显示为产品价格的一半。例如,如果基于重量 (0.5) 的产品价格为 7500,当添加到购物车时,迷你购物车上的总价显示为 3750。</p> <p>感谢任何帮助。非常感谢。 这是我的代码:</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()-&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['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-&gt;get_weight(); } } else { // Get the weight of the product $weight = $product-&gt;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 '&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;' . __('Total Weight:', 'chahar-4-rahewordpress') . '&lt;/p&gt; &lt;p class=&quot;total-weight-value woocommerce-Price-amount amount&quot;&gt;' . $total_weight_display . '&lt;/p&gt; &lt;/td&gt; &lt;/tr&gt;'; } } add_action( 'woocommerce_widget_shopping_cart_before_buttons', 'display_mini_cart_total_weight' );</pre></p>
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 '<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' );
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号