为什么我无法通过Ajax获取WP自定义字段值或发布ID?

时间:2022-10-07 17:52:55

I wrote a code that generate a link based on the visitor location and it worked perfectly but i discovered that the generated code get cached since i'm using a full page caching so i though to solve that issue i can use ajax to load that link. I used the below code which worked perfectly in getting some variables that i need such as location variable and link domain variable etc.. however i'm unable to get the WooCommerce custom field data or even the product id it just return blank.

我写了一个基于访问者位置生成链接的代码,它工作得很好,但我发现生成的代码被缓存,因为我使用的是整页缓存,所以我虽然解决了这个问题但我可以使用ajax加载该链接。我使用下面的代码,它完美地获得了我需要的一些变量,如位置变量和链接域变量等。但是我无法获得WooCommerce自定义字段数据甚至产品ID它只是返回空白。

I'm using this code to get the custom field which worked perfectly when used directly in function however can't get it to work in ajax

我正在使用此代码来获取自定义字段,该字段在函数中直接使用时工作正常但是无法在ajax中使用它

$uk_asin = get_post_meta(get_post()->ID, "wccaf_uk_asin", true );

I used that code in functions.php

我在functions.php中使用了该代码

add_action( 'woocommerce_before_add_to_cart_button', 'affiliate_link_ajax', 11);
function affiliate_link_ajax() {    
?>
<script>
jQuery(document).ready(function(){

             jQuery.ajax({
                url: "<?php echo admin_url('admin-ajax.php'); ?>",
                type: 'POST',
                data: {
                action: 'getmyfunctionform1'
                },
                dataType: 'html',
                success: function(response) {

                jQuery("#myResultsform1").html(response);

                }

        }); 
    });
</script> 
<!-- end Ajax call to getmyfunctionform1 smc 11-22-2013 -->

<div id="myResultsform1"></div>
<?php
}

and this code in funnctions.php as well

这个代码也在funnctions.php中

// Ajax Function to Load PHP Function myfunctionform1 smc 11/22/2013

add_action('wp_ajax_getmyfunctionform1', 'myfunctionform1');
add_action('wp_ajax_nopriv_getmyfunctionform1', 'myfunctionform1');

function myfunctionform1() { 
    $uk_asin = get_post_meta(get_post()->ID, "wccaf_uk_asin", true );
echo $uk_asin;
// Whatever php and or html you want outputed by the ajax call in template file

die(); } // important must use

// end Ajax Function to Load PHP Function myfunctionform1 smc 11/22/2013

I would really appreciate if the answer was simple since i'm still very new to coding

如果答案很简单,我会非常感激,因为我对编码还很新

1 个解决方案

#1


3  

Pass your post ID to your AJAX request:

将您的帖子ID传递给您的AJAX请求:

data: {
    action: 'getmyfunctionform1',
    postId: <?php echo get_post()->ID; ?>
}

And then use it in your callback function to get the meta value:

然后在回调函数中使用它来获取元值:

function myfunctionform1() { 
    $postId = filter_input( INPUT_POST, 'postId', FILTER_SANITIZE_NUMBER_INT );
    $uk_asin = get_post_meta( $postId, "wccaf_uk_asin", true );
    // etc.
}

#1


3  

Pass your post ID to your AJAX request:

将您的帖子ID传递给您的AJAX请求:

data: {
    action: 'getmyfunctionform1',
    postId: <?php echo get_post()->ID; ?>
}

And then use it in your callback function to get the meta value:

然后在回调函数中使用它来获取元值:

function myfunctionform1() { 
    $postId = filter_input( INPUT_POST, 'postId', FILTER_SANITIZE_NUMBER_INT );
    $uk_asin = get_post_meta( $postId, "wccaf_uk_asin", true );
    // etc.
}