• Resolved benventer83

    (@benventer83)


    Hi, I have the following ajax call that does not return a value when I add it in my snippet.

    $.ajax({
    url: ‘/wp-content/themes/astra/functions.php’,
    type: ‘POST’,
    data: {data_x: 4},
    success: function(data) {
    alert(‘yes’);
    alert(data);
    }
    });

    Alert yes does work but alert data doesn’t it is just blank. Here is the code where I send it to in the same snippet at the very top of the snippet.

    if (isset($_POST[‘data_x’])) {
    echo ‘hallo’;
    }

    So if everything is working, alert data should return hallo. I suspect that the ajax url is incorrect but I have no idea where else to send the call to or where the snippet saves the code. Please help!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    You can use admin_url( 'admin-ajax.php' ) to find the AJAX url using PHP, or if this code is running in the admin area then you can use the built-in ajaxurl variable:

    $.ajax({
    url: ajaxurl,
    type: 'POST',
    ....
    
    Thread Starter benventer83

    (@benventer83)

    Hi

    I have tried that, but lo luck, alert yes and alert data doesn’t return any values. Here is the code.

    $.ajax({
                        url: '<?php echo admin_url('admin-ajax.php') ?>',
                        type: 'POST',
                        data: {
                            quantity: quantity,
                            action: 'dynamic_price'
                        },
                        success: function(data) {
                            //var price = data;
                            alert('yes');
                            alert(data);
                        }
                    });
    Plugin Author Shea Bunge

    (@bungeshea)

    Can you post the full snippet?

    Thread Starter benventer83

    (@benventer83)

    add_action('wp_ajax_dynamic_price', 'get_update_dynamic_price_xyz');
    add_action('wp_ajax_nopriv_dynamic_price', 'get_update_dynamic_price_xyz');
    
    function get_update_dynamic_price_xyz()
    {
        global $product;
    
        /**
        * Get the discount details of given product with custom price
        * @param $price float/integer
        * @param $product object (Product object Example: wc_get_product($product_id))
        * @param $quantity int
        * @param $custom_price float/integer (0 for calculate discount from product price)
        * @param $return_details string (Default value 'discounted_price' accepted values = 'discounted_price','all')
        * @param $manual_request boolean (Default value false: pass this as true for get discount even if there is no item in cart)
        * @param $is_cart boolean (Default value true)
        * @return array|float|int - is product has no discounts, returns $price;else return array of discount details based on $return_details
        */
    	
        $qty = $_POST['quantity'];
        $price = apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', $product->get_price(), $product->get_id(), $qty, 0, 'discounted_price', 'true', 'true');
    
        if ($price == '')
        {
            $price = $product->get_price();
        }
    
        echo $price;
    }
    
    function action_woocommerce_single_product_summary()
    {
        global $product;
    
        /**
        * Get the discount details of given product with custom price
        * @param $price float/integer
        * @param $product object (Product object Example: wc_get_product($product_id))
        * @param $quantity int
        * @param $custom_price float/integer (0 for calculate discount from product price)
        * @param $return_details string (Default value 'discounted_price' accepted values = 'discounted_price','all')
        * @param $manual_request boolean (Default value false: pass this as true for get discount even if there is no item in cart)
        * @param $is_cart boolean (Default value true)
        * @return array|float|int - is product has no discounts, returns $price;else return array of discount details based on $return_details
        */
    
        $price = apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', $product->get_price(), $product->get_id(), 1, 0, 'discounted_price', 'true', 'true');
    
        if ($price == '')
        {
            $price = $product->get_price();
        }
    
        $currency_symbol = get_woocommerce_currency_symbol();
    
        // let's setup our div
        echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>', __('Product Total:', 'woocommerce'), '<span class="price">' . $currency_symbol . $price . '</span>');
    ?>
        <script>
            jQuery(function($) {
                // jQuery variables
                var price = <?php echo $price; ?>,
                    currency = '<?php echo $currency_symbol; ?>',
                    quantity = $('[name=quantity]').val();
    
                // Code to run when the document is ready
                var product_total = parseFloat(price * quantity);
                $('#product_total_price .price').html(currency + product_total.toFixed(2));
    
                // On change
                $('[name=quantity]').change(function() {
                    var quantity = this.value;
    
                    $.ajax({
                        url: '<?php echo admin_url('admin-ajax.php') ?>',
                        type: 'POST',
                        data: {
                            quantity: quantity,
    						action: 'dynamic_price'
                        },
                        success: function(data) {
                            //var price = data;
                            alert('yes');
                            alert(data);
                        }
                    });
    
                    if (!(this.value < 1)) {
                        product_total = parseFloat(price * this.value);
                        $('#product_total_price .price').html(currency + product_total.toFixed(2));
                    }
                });
            });
        </script>
    
    <?php
    }
    // We are going to hook this on priority 31, so that it would display below add to cart button.
    add_action('woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 31, 0);
    • This reply was modified 3 years, 3 months ago by benventer83.
    Plugin Author Shea Bunge

    (@bungeshea)

    You should use wp_send_json_success to send the price back to your script instead of just echoing it.

    Thread Starter benventer83

    (@benventer83)

    Will you be able to send me an example please, never used it before.

    Plugin Author Shea Bunge

    (@bungeshea)

    instead of

    echo $price;

    you should use:

    wp_send_json_success( $price );

    Thread Starter benventer83

    (@benventer83)

    add_action('wp_ajax_dynamic_price', 'get_update_dynamic_price_xyz');
    add_action('wp_ajax_nopriv_dynamic_price', 'get_update_dynamic_price_xyz');
    
    function get_update_dynamic_price_xyz()
    {
        global $product;
    
        /**
        * Get the discount details of given product with custom price
        * @param $price float/integer
        * @param $product object (Product object Example: wc_get_product($product_id))
        * @param $quantity int
        * @param $custom_price float/integer (0 for calculate discount from product price)
        * @param $return_details string (Default value 'discounted_price' accepted values = 'discounted_price','all')
        * @param $manual_request boolean (Default value false: pass this as true for get discount even if there is no item in cart)
        * @param $is_cart boolean (Default value true)
        * @return array|float|int - is product has no discounts, returns $price;else return array of discount details based on $return_details
        */
    	
    	if (isset($_POST['quantity'])) {
    		$qty = $_POST['quantity'];
    	} else {
    		$qty = 3;
    	}
        
        $price = apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', $product->get_price(), $product->get_id(), $qty, 0, 'discounted_price', 'true', 'true');
    
        if ($price == '')
        {
            $price = $product->get_price();
        }
    
        //echo $price;
    	wp_send_json_success( $price );
    }
    
    function action_woocommerce_single_product_summary()
    {
        global $product;
    
        /**
        * Get the discount details of given product with custom price
        * @param $price float/integer
        * @param $product object (Product object Example: wc_get_product($product_id))
        * @param $quantity int
        * @param $custom_price float/integer (0 for calculate discount from product price)
        * @param $return_details string (Default value 'discounted_price' accepted values = 'discounted_price','all')
        * @param $manual_request boolean (Default value false: pass this as true for get discount even if there is no item in cart)
        * @param $is_cart boolean (Default value true)
        * @return array|float|int - is product has no discounts, returns $price;else return array of discount details based on $return_details
        */
    
        $price = apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', $product->get_price(), $product->get_id(), 1, 0, 'discounted_price', 'true', 'true');
    
        if ($price == '')
        {
            $price = $product->get_price();
        }
    
        $currency_symbol = get_woocommerce_currency_symbol();
    
        // let's setup our div
        echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>', __('Product Total:', 'woocommerce'), '<span class="price">' . $currency_symbol . $price . '</span>');
    ?>
        <script>
            jQuery(function($) {
                // jQuery variables
                var price = <?php echo $price; ?>,
                    currency = '<?php echo $currency_symbol; ?>',
                    quantity = $('[name=quantity]').val();
    
                // Code to run when the document is ready
                var product_total = parseFloat(price * quantity);
                $('#product_total_price .price').html(currency + product_total.toFixed(2));
    
                // On change
                $('[name=quantity]').change(function() {
                    var quantity = this.value;
    
                    $.ajax({
                        url: '<?php echo admin_url('admin-ajax.php') ?>',
                        type: 'POST',
                        data: {
                            quantity: quantity,
    						action: 'dynamic_price'
                        },
                        success: function(data) {
                            //var price = data;
                            alert('yes');
                            alert(data);
                        }
                    });
    
                    if (!(this.value < 1)) {
                        product_total = parseFloat(price * this.value);
                        $('#product_total_price .price').html(currency + product_total.toFixed(2));
                    }
                });
            });
        </script>
    
    <?php
    }
    // We are going to hook this on priority 31, so that it would display below add to cart button.
    add_action('woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 31, 0);
    Thread Starter benventer83

    (@benventer83)

    Still did not work, even alert yes does not work. Usually alert yes works, good indication that ajax is working but in this case alert yes does not work. Is there maybe a test that we can do to see if ajax is working fine and the path is correct?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Ajax call not returning a value’ is closed to new replies.