skip to Main Content

The general idea here is pretty straight forward: get a better understanding of how many click(s) each product’s "Add To Cart" button get. Why? To understand if the product is at least of interest or not.

If the customer fulfills the checkout or not is a different matter, and therefore the "next step" in this saga of "counting".

Again, I found bits and pieces here and there – but nothing really worked. I’ve managed to create the column in WooCommerce > Proudcts and I’ve managed to start to understand the JS part of things – but no matter what I do, the clicks are just not counted.

What I want and need:
Each time the a visitor or a logged in customer clicks on the "Add To Cart" button (loop, archive, product page etc), that click should be counted. It all starts from 0. Then 1 click = changes the cont into 1. Then another click = changes the counter into 2 – and so forth.

This is the code I am working with.

Part 1:

// remove all versions of the "Add To Cart" and replace them
remove_action(' woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );

add_action('woocommerce_simple_add_to_cart', 'count_add_to_cart_with_clicks', 1);
function count_add_to_cart_with_clicks() {

    // add our own version
    add_action( 'woocommerce_simple_add_to_cart', 'count_simple_add_to_cart_with_clicks', 30 );
}

function count_simple_add_to_cart_with_clicks() {

    global $product;

        if ( ! $product->add_to_cart_url() ) return;

            echo '<p><a href="' . $product->add_to_cart_url() . '" class="single_add_to_cart_button button alt countable" 
            data-pid="' . $product->get_id() . '">' . $product->single_add_to_cart_text() . '</a></p>';

        // js that should count each click and show the number in wp-admin under WoooCommerce > Products
        wc_enqueue_js( "
            $( 'a.countable' ).click( function( e ) {
            e.preventDefault();
            $.post( '" . '/wp-admin/admin-ajax.php' . "', { action: 'increment_counter', pid: $( this ).data( 'pid' ) } );
            window.open($ ( this ).attr( 'href' ) );
            }
        );
    ");
}

Part 2:

add_action( 'wp_ajax_increment_counter', 'count_increment_counter' );
add_action( 'wp_ajax_nopriv_increment_counter', 'count_increment_counter' );
function count_increment_counter() {

    $pid = $_POST['pid'];

    $click = get_post_meta( $pid, '_click_counter', true ) ? (int) get_post_meta( $pid, '_click_counter', true ) + 1 : 1; update_post_meta( $pid, '_click_counter', $click );

    wp_die();
}

Part 3:

add_filter( 'manage_edit-product_columns', 'count_clicks_in_admin_products_column', 9999);
function count_clicks_in_admin_products_column( $columns ) {

    $columns['click'] = 'click';
    
    return $columns;
}

Part 4:

add_action( 'manage_product_posts_custom_column', 'click_admin_products_views_column_content', 9999, 2 );
function click_admin_products_views_column_content( $column, $product_id ) {

    if ( $column == 'click' ) {

            echo get_post_meta( $product_id, '_click_counter', true );
    }
}

Researched JavaScript and how to create a column, using a counter. But I am stuck.

2

Answers


  1. Try the following simplified code version (without JavaScript and without replacing add to cart core code), that will count all add_to_cart clicks, even Ajax add to cart clicks:

    // Utility function: Updating the click count
    function update_add_to_cart_click_counter( $product_id ) {
        $product = wc_get_product($product_id);
        $count   = (int) $product->get_meta('_click_counter');
    
        $count++;
        $product->update_meta_data('_click_counter', $count);
        $product->save();
    }
    
    // Count clicks on add to cart action (works with ajax add to cart too)
    add_action( 'woocommerce_add_to_cart', 'count_add_to_cart_clicks', 90, 4 );
    function count_add_to_cart_clicks( $cart_item_key, $product_id, $quantity, $variation_id = null ) {
        if( did_action('woocommerce_add_to_cart') === 1 ) {
            update_add_to_cart_click_counter( $product_id );
    
            // Handling variation click count (optional)
            if( $variation_id > 0 ) {
                update_add_to_cart_click_counter( $variation_id );
            }
        }
    }
    
    // Add a custom column to Admin product list
    add_filter('manage_edit-product_columns', 'admin_product_click_count_column');
    function admin_product_click_count_column($columns) {
        $columns['click_counter'] = __('Click', 'woocommerce');
        return $columns;
    }
    
    // Display the click count for each product in the "Click" column
    add_action('manage_product_posts_custom_column', 'admin_product_click_count_column_content', 10, 2);
    function admin_product_click_count_column_content($column, $post_id) {
        if ($column === 'click_counter') {
            global $product;
    
            echo (int) $product->get_meta('_click_counter');
        }
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

    Login or Signup to reply.
  2. LoicTheAztec is there a way to make it sortable?thanks!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search