Home Forums Front-end Issues WooCommerce price + ACF number field sum Reply To: WooCommerce price + ACF number field sum

  • Hi,

    The ACF number field will return an integer.
    In php you’ll never have some decimal with an integer value, you have to work with float values.

    Maybe you could simply try something like this

    $price_html = $product->get_price(); //It will return a string
    $x = get_field(‘custom-price’); //Integer
    $float_price = floatval($price_html); //Float
    $float_custom_price = floatval($x); //Float
    $finalPrice = ($float_price + $float_custom_price); //Float
    echo $finalPrice;

    But this will only display your custom price.
    If you want to actually modify your price you have to use woocommerce filters.
    In product page :

    // Simple, grouped and external products
    add_filter(‘woocommerce_product_get_price’, ‘custom_price’, 99, 2 );
    add_filter(‘woocommerce_product_get_regular_price’, ‘custom_price’, 99, 2 );
    // Variations
    add_filter(‘woocommerce_product_variation_get_regular_price’, ‘custom_price’, 99, 2 );
    add_filter(‘woocommerce_product_variation_get_price’, ‘custom_price’, 99, 2 );