Solution:
The correct code to be used since Woocommerce 3.2+ avoiding problems and errors is:
add_action('woocommerce_before_calculate_totals', 'cart_item_discount', 10, 1 );
function cart_item_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Avoiding hook repetition (when using price calculations for example)
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop Through cart items
foreach ( $cart->get_cart() as $cart_item ) {
$original_price = $cart_item['data']->get_price(); // Get original product price
$discounted_price = $original_price / 2; // 50 % of discount
$cart_item['data']->set_price( $discounted_price );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works (Tested on last versions: WordPress 5.0.x | Woocommerce 3.5.x | Storefront 2.4.x)
If it doesn’t work, it’s because some other things or customizations are interacting with it. You need first to check Woocommerce > Status for red items (where all overridden templates, at the end, need to be up to date).