dse3168 2016-09-07 02:27
浏览 45
已采纳

如何在购物车中获取自定义属性值的总和?

Some of my products has custom attribute pa_ves-g. In cart total I need to get entire volume of all products in cart.

I found this code for weight:

function myprefix_cart_extra_info() {
    global $woocommerce;
    echo '<div class="cart-extra-info">';
    echo '<p class="total-weight">' . __('Total weight', 'woocommerce');
    echo ' ' . $woocommerce->cart->cart_contents_weight . ' ' . 'kg';
    echo '</p>';
    echo '</div>';
}

So how to do the same for pa_ves-g?

Thanks.

  • 写回答

1条回答 默认 最新

  • dplo59755 2016-09-07 03:44
    关注

    First you are using old (working) cart syntax: you don't need anymore global woocommerce; if instead using $woocommerce->cart you use WC()->cart actual syntax.

    Use that code to get total cart volume value for volume attribute:

    function myprefix_cart_extra_info() {
        $volume = 0;
    
        // Iterating though each item in cart
        $cart_items = WC()->cart->get_cart();
        foreach( $cart_items as $cart_item ){
            $item_id = $cart_item['product_id'];
            $terms = get_the_terms( $item_id , 'pa_ves-g');
                foreach($terms as $key => $term)
                    if(!empty($term->name)) $volume += $term->name;
            }
    
        echo '<div class="cart-extra-info">';
        echo '<p class="total-weight">' . __('Total volume', 'woocommerce');
        echo ' ' . $volume . ' ' . 'm3';
        echo '</p>';
        echo '</div>';
    }
    

    This goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code is tested and works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料