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
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:
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
LoicTheAztec is there a way to make it sortable?thanks!