Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

318
Views
WooCommerce: muestra cuántas personas tienen un producto en su carrito con un mensaje condicional

Estoy usando Mostrar cuántas personas agregaron un producto en su carrito actual para mostrar cuántas personas agregaron un producto en su carrito actual en las páginas Tienda y Producto único; similar a lo que ves en tiendas como Etsy.

El fragmento funciona tal como está, pero sería bueno cambiar condicionalmente el mensaje dependiendo de si hay 1 usuario frente a varias personas (es decir, "1 persona tiene esto en su carrito en este momento" frente a "10 personas tienen esto en sus carritos correcto ahora.").

Si alguien tiene alguna sugerencia sobre la mejor manera de manejarlo, se lo agradecería. :)

 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 answers
Answer question

0

Puede usar para esta función de situaciones _n

El código después de cambiar

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!