• Resolved sguha007

    (@sguha007)


    Hello,

    I am using this code to add the product into the cart – WC()->cart->add_to_cart( 25, 1 ). It was working fine but from today I have got the below error message –
    wc_add_number_precision(): Argument #1 ($value) must be of type float, null given, called in /chroot/home/a23cc473/fdd76f6eaa.nxcli.net/html/wp-content/plugins/woocommerce/includes/wc-core-functions.php on line 1955

    I am using the latest WordPress and Woocommerce. Can you please advise me why this error message showing

Viewing 5 replies - 1 through 5 (of 5 total)
  • Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @sguha007

    The function there takes a number value to add precision to, then rounds if after the precision is added. In should return a float, but in your case, it returns a null.

    With that said, can you please share the code snippet you are using, so we can take a closer look at that and see where things are going wrong?

    Cheers!

    Thread Starter sguha007

    (@sguha007)

    Hello,

    Thank you for your response. Here is my code

    add_action( 'wp_ajax_getCheckoutPageContent', 'getCheckoutPageContentCallBack' );
    add_action( 'wp_ajax_nopriv_getCheckoutPageContent', 'getCheckoutPageContentCallBack' );
    	function getCheckoutPageContentCallBack() {
            $product_id        = absint( $_POST['product_id'] );
            $quantity          = absint( $_POST['quantity'] );
            $product_status    = get_post_status( $product_id );
            $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
            if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity ) && 'publish' === $product_status ) {
    			do_action( 'woocommerce_ajax_added_to_cart', $product_id );
                global $woocommerce;
                $items = $woocommerce->cart->get_cart();
                wc_setcookie( 'woocommerce_items_in_cart', count( $items ) );
                wc_setcookie( 'woocommerce_cart_hash', md5( json_encode( $items ) ) );
                do_action( 'woocommerce_set_cart_cookies', true );
                define( 'WOOCOMMERCE_CHECKOUT', true );
                echo do_shortcode('[woocommerce_checkout]');
            } 
            die();
        }
    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @sguha007

    How are you specifically adding it to the cart as per the code below:

    > WC()->cart->add_to_cart( 25, 1 )

    How does that tie in with the snippet and what exactly are you trying to achieve with the code you passed along? It looks like you are trying to create an order programatically or load the checkout dynamically using Ajax?

    Could you provide a bit more background and if possible, the steps you are taking with regard to adding the product to the cart (code-wise), to the point where you see the error?

    Cheers!

    Thread Starter sguha007

    (@sguha007)

    Hello,

    Yes I am trying to load the checkout dynamically.

    There is one page where I listed the products. When I click on the button Buy now, jQuery ajax will call with the post value product id, and quantity.

    Then the above function will call in function.php

    If there is no error then the checkout form will load on that page itself but from today when I click on the buy now button, the above error is showing.

    Mirko P.

    (@rainfallnixfig)

    Hi @sguha007,

    Thank you for getting back to us with more details.

    It sounds like a fairly complex development topic. This forum is intended to support the default functionality of WooCommerce, so there aren’t as many developers here as in other channels.

    For this type of questions, you’re more likely to get input by asking in the WooCommerce Developer Slack or Advanced WooCommerce Facebook group. As those communities are both frequented by developers.

    However, I’m going to leave this thread open for a bit to see if anyone else is able to chime in to help you out ??

    All the best.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘add_to_cart function not working’ is closed to new replies.