Woocommerce 如何将预定义值添加到产品页面上的计数器

Woocommerce how to add a pre-defined value to a counter on product page

我在我的 woocommerce 产品页面上使用了一个点击计数器,该计数器添加了对特定按钮的点击次数。

计数器工作正常并且可以完美地计算点击次数,但我想添加给定范围内的随机预定义值,以便每个产品在计数器上都有唯一的预定义值,当按下按钮时该值会增加。

这是我的代码:

前端(通过 hooks PHP 文件添加):

//button that is used to count clicks
<div id="link_count">
<a href="#" onclick="PrintImage('<?php echo wp_get_attachment_url( $product->get_image_id() ); ?>'); return false;"><button id="printImage" class="single_add_to_cart_button button alt">Print this Image</button></a>
</div>



//output count
    global $post;
    print get_post_meta($post->ID,'link_check_click_counter',true);

 // update counter in post database 
function update_counter_ajax() { 
    $postID = trim($_POST['post_id']); 
    $count_key = 'download'; 
    $counter = get_post_meta($postID, $count_key, true); 
        if($counter==''){ 
            $count = 1; 
            delete_post_meta($postID, $count_key); 
            add_post_meta($postID, $count_key, '1'); 
        }else{ 
            $counter++; 
            update_post_meta($postID, $count_key, $counter); 
        } 
    wp_die(); 
    } 
    add_action('wp_ajax_update_counter', 'update_counter_ajax'); 
    add_action('wp_ajax_nopriv_update_counter', 'update_counter_ajax');

Functions.php:

//counter code start
add_action( 'wp_ajax_link_check_click_counter', 'link_check_click_counter');
add_action( 'wp_ajax_nopriv_link_check_click_counter', 'link_check_click_counter' );
function link_check_click_counter() {

    if ( isset( $_POST['nonce'] ) &&  isset( $_POST['post_id'] ) && wp_verify_nonce( $_POST['nonce'], 'link_check_click_counter_' . $_POST['post_id'] ) ) {
        $count = get_post_meta( $_POST['post_id'], 'link_check_click_counter', true );
        update_post_meta( $_POST['post_id'], 'link_check_click_counter', ( $count === '' ? 1 : $count + 1 ) );
    }
    exit();
}


add_action( 'wp_footer', 'link_click' );
//add_action( 'wp_head', 'link_click' );
function link_click() {
    global $post;

    if( isset( $post->ID ) ) {
?>
    <script type="text/javascript" >
    jQuery(function ($) {
        var ajax_options = {
            action: 'link_check_click_counter',
            nonce: '<?php echo wp_create_nonce( 'link_check_click_counter_' . $post->ID ); ?>',
            ajaxurl: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
            post_id: '<?php echo $post->ID; ?>'
        };

        $( '#link_count a' ).on( 'click', function() {
            var href = $( this ).attr( "href" );
            var redirectWindow = window.open(href, '_blank');   
            $.post( ajax_options.ajaxurl, ajax_options, function() {
                redirectWindow.location;
            });
            return false;
        });
    });
    </script>
<?php
    }
}



function link_check_meta_values( $key = '', $type = 'post', $status = 'publish' ) {

    global $wpdb;

    if( empty( $key ) )
        return;

    $r = $wpdb->get_col( $wpdb->prepare( "
        SELECT pm.meta_value FROM {$wpdb->postmeta} pm
        LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
        WHERE pm.meta_key = '%s'
        AND p.post_status = '%s'
        AND p.post_type = '%s'
        ", $key, $status,

 $type ) );

    return $r;
}

//counter code end

该代码工作正常并计算每次点击按钮,但我只是想添加特定的预定义值,这些值在每个产品上随机显示。

例如预定义的范围是:5000 - 20000

例如:

产品上的一个按钮应该有:

相应的按钮按一次该值应加一

我仍在学习 PHP 和 WordPress,因此我们将不胜感激。

update_counter_ajax() 函数中,当您为计数器定义默认值(如果它为空)时,您可以生成您选择的随机数而不是设置为“1”!

$counter = get_post_meta($postID, $count_key, true); 
        if(empty($counter)){ 
            $counter = rand(5000, 20000);
            // OR you could use the other php random function
            // $count = mt_rand(5000, 20000);
            add_post_meta($postID, $count_key, $counter);