How can i get price in woocommerce cart product ti

2019-09-19 21:21发布

问题:

I am working on woocommerce cart page with add_filter "woocommerce_in_cart_product_title", There I want to show price with title.

Like "Product title ($price)"

i have tried this code, but i can not get price.

add_filter( 'woocommerce_in_cart_product_title', 'cart_product_title', 20, 3);

function cart_product_title( $title, $values, $cart_item_key ) {
    global $woocommerce;

    $items = $woocommerce->cart->get_cart();
    print_r($items);
    foreach ($items as $cart_item_key => $values) {
            echo $name = $values['prodaddons'][0]['name'].' --- ';
            echo $price = $values['prodaddons'][0]['price'].'<br>';
        }
    echo 'aaaa-'.$title.'-'.$pricees;
}

回答1:

try this :

add_filter( 'woocommerce_cart_item_name', 'cart_product_title', 20, 3);

function cart_product_title( $title, $values, $cart_item_key ) {
    return $title . ' - ' . $values[ 'line_total' ] . '€';
}

I don't know your version of woocommerce but woocommerce_in_cart_product_title is depreciated, it's why I use woocommerce_cart_item_name, so you should try both ;)