Created
August 6, 2019 01:38
-
-
Save itzmekhokan/841ae4a8233d185c44d727f830cd5f13 to your computer and use it in GitHub Desktop.
Show how many people added product in their current cart.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function show_no_of_people_added_in_cart(){ | |
global $wpdb, $product; | |
$in_basket = 0; | |
$wc_session_data = $wpdb->get_results( "SELECT session_key FROM {$wpdb->prefix}woocommerce_sessions" ); | |
$wc_session_keys = wp_list_pluck( $wc_session_data, 'session_key' ); | |
if( $wc_session_keys ) { | |
foreach ( $wc_session_keys as $key => $_customer_id ) { | |
// if you want to skip current viewer cart item in counts or else can remove belows checking | |
if( WC()->session->get_customer_id() == $_customer_id ) continue; | |
$session_contents = WC()->session->get_session( $_customer_id, array() ); | |
$cart_contents = maybe_unserialize( $session_contents['cart'] ); | |
if( $cart_contents ){ | |
foreach ( $cart_contents as $cart_key => $item ) { | |
if( $item['product_id'] == $product->get_id() ) { | |
$in_basket += 1; | |
} | |
} | |
} | |
} | |
} | |
if( $in_basket ) | |
echo '<a class="in-basket">' . sprintf( __( '%d people have this in their cart', 'text-domain' ), $in_basket ) . '</a>'; | |
} | |
add_action( 'woocommerce_after_shop_loop_item_title', 'show_no_of_people_added_in_cart', 11 ); | |
add_action( 'woocommerce_single_product_summary', 'show_no_of_people_added_in_cart', 21 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment