//for cart items 
add_filter( 'wdp_save_cart_item_keys', function ( $keys ) {
    $keys[] = 'addons';
    return $keys;
});
$wdp_get_product_any_price = function ($price, $product, $item_meta) {
    if ( ! empty( $item_meta["addons"] ) && isset($item_meta['addons_price_before_calc']) ) {
        $price = floatval($item_meta['addons_price_before_calc']);
        foreach ( $item_meta["addons"] as $addon ) {
            if ( ! empty( $addon["price"] ) ) {
                $price = floatval( $price ) + floatval( $addon["price"] );
            }
        }
    }
    return $price;
};
add_filter( "wdp_get_product_regular_price", $wdp_get_product_any_price, 10, 3 );
add_filter( "wdp_get_product_sale_price", $wdp_get_product_any_price, 10, 3 );
add_filter( "wdp_get_product_initial_price", $wdp_get_product_any_price, 10, 3 );


//to adjust Subtotals on product page 
add_action('wp_print_scripts',function(){
    ?>
    <script>
        document.addEventListener("DOMContentLoaded", function (evt) {
            if ( typeof DynamicPrice === 'undefined') {
                return;
            }
            var $form = jQuery('form.cart').first();
            var $targets = [];
            $targets.push(".product-addon-totals .amount:eq(-1)");

            var dynamicPrice = new DynamicPrice($form);
            dynamicPrice.addSubtotalDestination($targets);

            var $lock = false;

            $form.on('updated_addons', function () {
                setTimeout(function () {
                    if ($lock) {
                        return;
                    } else {
                        $lock = true;
                    }

                    var promise = dynamicPrice.update({
                        'custom_price': jQuery(".product-addon-totals .amount:eq(-1)").text(),
                    });

                    if (typeof promise !== 'undefined') {
                        promise.done(function () {
                            $lock = false;
                        });
                    } else {
                        $lock = false;
                    }
                }, 0);
            });
        });

    </script>
    <?php
});