WooCommerce:检查购物车商品是否免费并将 class 添加到 table 行

WooCommerce: Check if cart item is free and add class to table row

如果产品是免费的(价格为 0),我想在购物车 table 行中额外添加一个 class。

我有办法添加额外的 class:

function custom_add_cart_item_classes( $class, $values, $values_key ) {

    if ( isset( $values[ 'product_id' ] ) ) {
        $class .= ' cart_item_' . $values[ 'product_id' ];
        
        // This is the problem: $cart_item is not available here
        $get_item_cart_price = $cart_item['data']->get_price();
        if ($get_item_cart_price == '0') :
            $class .= ' cart_item_free';
        endif;
        
    }

    return $class;
}
add_filter( 'woocommerce_cart_item_class', 'custom_add_cart_item_classes', 10, 3 );

问题是,我无法在函数中访问 $cart_item。 我为 $values 制作了一个 var_dump,并在其中找到了产品 ID。

有什么办法可以得到带有产品ID的$cart_item吗?或者以任何其他方式在此处加载该元素?

编辑:我需要购物车商品,因为购物车中的价格已更改。正品价格不免费

使用 WC 4.9.0 测试正常

function custom_add_cart_item_classes( $class, $values, $values_key ) {

    
        if ( isset( $values[ 'data' ] ) ) {
            $product = $values[ 'data' ];
            if(!$product->get_price()){
                $class .= ' cart_item_free';
            }
        }

    return $class;
}
add_filter( 'woocommerce_cart_item_class', 'custom_add_cart_item_classes', 10, 3 );