在 WooCommerce 中为定义的产品类别设置购物车总计

问题描述 投票:0回答:1

我需要按产品类别获取购物车总价,减去不属于特定类别的所有产品。

add_filter( 'woocommerce_calculated_total', 'calculated_total', 10, 2 );
function calculated_total( $total, $cart ) {

    foreach ( $cart->get_cart() as $cart_item ) {
        if ( has_term( 'shoes', 'product_cat', $cart_item['product_id'] ) ) {
            $total = $total - ;
        }
    }
   
    return $total;
}

示例:如果鞋子类别中有 4 件产品,每件产品价格为 50 美元,则购物车中的总价将是 200 美元(即使还有其他类别的其他产品),总计超过 200 美元。

php wordpress woocommerce hook-woocommerce cart
1个回答
0
投票

根据您的代码,要设置已定义产品类别的总金额(减去不属于该特定类别的任何产品),请尝试:

add_filter( 'woocommerce_calculated_total', 'calculated_total', 10, 2 );
function calculated_total( $total, $cart ) {

    foreach ( $cart->get_cart() as $cart_item ) {
        if ( ! has_term( 'shoes', 'product_cat', $cart_item['product_id'] ) ) {
            $total -= ($cart_item['total'] + $cart_item['line_tax']);
        }
    }
   
    return $total;
}

应该有效

© www.soinside.com 2019 - 2024. All rights reserved.