Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

321
Visualizações
WooCommerce - Display how many people have a product in their cart with conditional message

I'm using Show how many people have added a product in their current cart to display how many people have added a product in their current cart on the Shop and Single Product pages; similar to what you see on stores like Etsy.

The snippet works as is but it would be nice to conditionally change the message depending on if there is 1 user vs. multiple people (i.e "1 person has this in their cart right now." vs "10 people have this in their carts right now.").

If anyone has any suggestions on the best way to handle that it would be appreciated. :)

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 '<div class="in-basket">' . sprintf( __( '%d people have this in their cart', 'text-domain' ), $in_basket ) . '</div>';

}

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 );
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can use for this situations function _n

The code after changing


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 '<div class="in-basket">' . sprintf(_n( '%s person have this in their cart', '%s people have this in their cart', $in_basket, 'text-domain' ), number_format_i18n($in_basket ) ) . '</div>';

}

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 );

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda