Skip to content
Advertisement

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

I am using a click counter on my woocommerce product page where the counter adds the number of clicks on a specific button.

The counter works fine and counts the clicks perfectly but I want to add random pre-defined values from given range so each product has unique pre-defined values on counter which is incremented when the button is pressed.

Here are my codes:

Front end (added in through hooks PHP File):

//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

The code works fine and counts each click on button but, I simply want to add specific pre-defined values which are showed on random bases on each product.

E.g. the pre-defined range is: 5000 – 20000

For example:

  • Second product counter value would be 5523.
  • Third product counter value would be 15789

one button on product a should have:

The value should add one if the corresponding button is pressed once

I am still learning PHP and WordPress so any help would be appreciated.

Advertisement

Answer

In the update_counter_ajax() function, when you define a default value for your counter if it’s empty, you could generate a random number of your choice instead of setting to “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);

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement