Ajax call not working

时间:2021-11-02 作者:B. Venter

我有下面的wordpress代码片段,除了ajax调用之外,它运行良好。我收到警报是,但不是警报数据。我怀疑ajax url不正确,但不知道指向何处,因为所有代码都在同一个片段中。我可能会收到一个提示,说是的,然后打招呼,如果我能让它起作用,其余的应该会自行纠正。

/*
if (isset($_POST[\'data_x\'])) {
    //echo get_update_dynamic_price_xyz($_REQUEST[\'data_x\']);
    echo \'hallo\';
}
*/

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($qty) {
    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(), $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: \'/wp-content/themes/astra/functions.php\',
                type: \'POST\',
                data: {data_x: \'4\'},
                success: function(data) { 
                    //var price = data;
                    alert(\'yes\');
                    alert(data);
                }
            });
            */
            
        $.ajax({
            url: \'<?php echo admin_url(\'admin-ajax.php\') ?>\',
            type: \'POST\',
            data: {data_x: \'4\', 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 );

1 个回复
SO网友:BlueSuiter

我已更新了您共享代码中的代码。如果您没有得到更改,请在评论中询问。

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);