php - Custom price on WooCommerce 5.5.2

one text

I've been stuck on this for far too long. I've tried everything I've found on stackoverflow and I feel like I'm going around in circles.

I have a form on a product page asking for an invoice number and an amount on the invoice. I can get product attributes to update and display on the Cart page using the following code:

function invoice_save_price( $cart_item_data, $product_id ) {
    if( isset( $_POST['invoice_id'] ) ) {
        $cart_item_data = array();
        $cart_item_data[ "invoice_number" ] = $_POST['invoice_number'];
        $cart_item_data[ "invoice_amount" ] = $_POST['invoice_amount'];
    }
    return $cart_item_data;
}

add_filter( 'woocommerce_add_cart_item_data', 'invoice_save_price', 99, 2 );

I'm using a custom product type - extending simple products. I'm not sure if that's the correct way to do this, as my product has to have a default price set ($1 for now) or it doesn't display.

function wcpt_register_invoice_type () {

    class WC_Product_Invoice extends WC_Product {

        public function __construct( $product ) {
            $this->product_type = 'invoice';
            parent::__construct( $product );
        }
    }

}

However, I can't get the price to update. My code for that is here:

add_action( 'woocommerce_before_calculate_totals', 'invoice_add_custom_price', 99 );

function invoice_add_custom_price($cart_object) {
    if (isset($_POST['invoice_id'])) {
        foreach ( $cart_object->get_cart() as $value ) {
            $newValue = number_format($_POST['invoice_amount'],2);
            $value['data']->set_price($newValue);
        }
        return $value;
    }
}

I thought there might've been a conflict with another plugin, so I started testing on a clean install of Wordpress 5.8 and WooCommerce 5.5.2

I've found a lot of references to this in older versions of WooCommerce (3.x) but I'm not having much luck in later versions.

Can someone please tell me what I'm missing? Any help will be appreciated!

Thanks,

Source