It will Trigger action after item added to Cart and insert User ip to table.
add_action('woocommerce_add_to_cart' , function() {
global $product;
$user_ip = $_SERVER['REMOTE_ADDR'];
$meta = get_post_meta( $product->ID, 'added_in_cart', TRUE );
$meta = '' !== $meta ? explode( ',', $meta ) : array();
$meta = array_filter( array_unique( $meta ) );
if( ! in_array( $user_ip, $meta ) ) {
array_push( $meta, $user_ip );
update_post_meta( $product->ID, 'added_in_cart', implode(',', $meta));
}
});
Display particular product product added to cart count before add to cart button.
add_action( 'woocommerce_before_add_to_cart_button', 'add_content_before_addtocart_button_func',0 );
function add_content_before_addtocart_button_func() {
global $product;
$id = $product->id;
$meta = get_post_meta( $id, 'added_in_cart', TRUE );
if(empty($meta))
{
$result = 0;
}
else
{
$result = count(explode(',',$meta));
}
echo "<div class='custom-visitor-count-st' style='font-size: 20px;'>";
echo "<i class='fa fa-cart'></i>";
echo "<span class='cv-value'>";
echo $result;
echo " times added to cart</span></div>";
}
You can use any other woo commerce hook to display added to cart count.
Insert the below code into function.php file. you can insert view count on table by user IP address.
add_action('wp', function() {
global $post;
$user_ip = $_SERVER['REMOTE_ADDR'];
$meta = get_post_meta( $post->ID, 'views_count', TRUE );
$meta = '' !== $meta ? explode( ',', $meta ) : array();
$meta = array_filter( array_unique( $meta ) );
if( ! in_array( $user_ip, $meta ) ) {
array_push( $meta, $user_ip );
update_post_meta( $post->ID, 'views_count', implode(',', $meta) );
}
});
Display particular product view count before add to cart button.
add_action( 'woocommerce_before_add_to_cart_button', 'add_content_before_addtocart_button_func',0 );
function add_content_before_addtocart_button_func() {
global $product;
$id = $product->id;
$meta = get_post_meta( $id, 'views_count', TRUE );
if(empty($meta))
{
$result = 0;
}
else
{
$result = count(explode(',',$meta));
}
echo "<div class='custom-visitor-count-st' style='font-size: 20px;'>";
echo "<i class='fa fa-eye'></i>";
echo "<span class='cv-value'>";
echo $result;
echo " Views</span></div>";
}
You can use any other woo commerce hook to display view count.