WooCommerce - 使用条件消息显示有多少人的购物车中有产品

WooCommerce - Display how many people have a product in their cart with conditional message

我正在使用 Show how many people have added a product in their current cart 来显示有多少人在“商店”和“单一产品”页面上的当前购物车中添加了产品;类似于您在 Etsy 等商店看到的内容。

该代码段按原样工作,但如果有条件地更改消息会很好,具体取决于是否有 1 个用户与多人(即“1 人现在在他们的购物车中有这个。”与“10 个人有现在就在他们的购物车里。”)。

如果有人对处理该问题的最佳方式有任何建议,我们将不胜感激。 :)

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

对于这种情况你可以使用函数_n

修改后的代码


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