I'm new to Woocommerce. But I think I can help you a little, because I have to research the same topic recently.
Try this. All the credit go to WooCommerce - Adding a custom price to each product in cart woocommerce, how can i add additional cost in cart product total price?.
I only made a little modifications (Already tested. 13558 is the id of Product B).
add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );
function calculate_discounted_price( $price, $values, $cart_object ) {
$terms = wc_get_product_terms( $product->id, $attribute_name, array( 'fields' => 'all' ) );
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if( $_product->id == '13558' ) {
//print_r("Producto B en Carrito");
$price +=5;
}
}
return $price;
}
function display_discounted_price( $values, $item ) {
return wc_price( $item[ 'line_total' ] );
}