• Resolved curiousrob

    (@curiousrob)


    I use the following snippet to show people have added an item to their cart (WooCommerce):

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

    Which I got here:

    https://gist.github.com/itzmekhokan/841ae4a8233d185c44d727f830cd5f13

    https://itzmekhokan.wordpress.com/2019/08/06/show-how-many-people-have-added-a-product-in-their-current-cart/

    It works fine, BUT when editing via Gutenberg editor I cannot save without an error being thrown.

    Also looking at the WordPress error log file I’m getting

    PHP Fatal error: Uncaught Error: Call to a member function get_customer_id() on null
    Stack trace:
    #0 /home/public_html/wp-includes/class-wp-hook.php(307): show_no_of_people_added_in_cart('')
    #1 /home/public_html/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters('', Array)
    #2 /home/public_html/wp-includes/plugin.php(474): WP_Hook->do_action(Array)
    #3 /home/public_html/wp-content/plugins/woocommerce/templates/content-product.php(57): do_action('woocommerce_aft...')
    #4 /home/public_html/wp-includes/template.php(772): require('/home/...')
    #5 /home/public_html/wp-content/plugins/woocommerce/includes/wc-core-functions.php(284): load_template('/home/..', false)
    #6 /home/public_html/wp-content/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-products.php(660): wc_get_template_part('content', 'product')
    #7 /home/ in /home/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(484) : eval()'d code on line 10

    I use a Snippets plugin with setting “Only run on site front end” but I am also getting this error when placing the code directly into functions.php file.

    Any ideas?

    It’s a very useful bit of code when it works!

    Cheers, Rob`

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Unfortunately, it looks like this is an issue with the code itself – it’s just not designed to work with Gutenberg. I recommend contacting a developer familiar with WooCommerce and ask them how to fix the code.

    Thread Starter curiousrob

    (@curiousrob)

    Hi Shea,

    Thanks for your reply.

    As a workaround I just temporarily disable the particular Snippet when I need to work in Gutenberg editor.

    And it only affects the Gutenberg editor if I’ve used WooCommerce shortcodes [products limit=”4″ columns=”4″ best_selling=”true”] in a block or page.

    Cheers,
    Rob

    Plugin Author Shea Bunge

    (@bungeshea)

    Hi Rob,

    It’s definitely down to the snippet trying to access data that might not always be available, and it sounds like it’s not available when editing with Gutenberg.

    As a workaround, you can just add an extra check to the if conditional to prevent the error. Here’s a modified version of the code which should work a bit better:

    $callback = function () {
    	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 && 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', $callback, 11 );
    add_action( 'woocommerce_single_product_summary', $callback, 21 );
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WooCommerce Custom Code Fatal Error with Gutenberg’ is closed to new replies.