WooCommerce Custom Code Fatal Error with Gutenberg
-
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
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)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘WooCommerce Custom Code Fatal Error with Gutenberg’ is closed to new replies.