Skip Navigation

[Resolved] Critical error when using custom Shortcode but only on Toolset Layout**

This support ticket is created 2 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 20 replies, has 2 voices.

Last updated by Diego Walter Ricciardi 2 years, 6 months ago.

Assisted by: Shane.

Author
Posts
#2361805

Tell us what you are trying to do?
I'm using a custom shortcode to display SKU and wrapping in it in a conditional, works in the wordpress archive (although it does cause the block individual block to crash?) and it crashes the custom layout until I disable the shortcode not sure why, shortcode is very simple and works well on it's own.

function display_woo_sku() {

global $product;
return $product->get_sku();

}
add_shortcode( 'woo_sku', 'display_woo_sku' );

Is there any documentation that you are following?

Not really.

What is the link to your site?
hidden link

#2361807
simple_shortcode.png

Can't show it before giving access, since the idea of the shortcode is to show SKU only if you're admin or shop manager but I can snag a pic, it's so simple is ridiculous.

#2361869

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Diego,

Thank you for getting in touch.
What we recommend doing here is to enable the wordpress debugging functions to understand the error being thrown.

To enable the wordpress debugging you will either need to log into your website using FTP or using your Host's CPanel's file manager.

From there you will need to edit the wp-config file by adding the following.

// Enable WP_DEBUG mode
define( 'WP_DEBUG', true );

// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', true );

If the define( 'WP_DEBUG', true ); function already exists you will need to change 'false' to 'true'

Once you've done this please revisit the page with the issue and let me know what the issue is.
Thanks,
Shane

#2362617
Fatal error: Uncaught Error: Call to a member function get_sku() on null in /home/745301.cloudwaysapps.com/jfcnzyyzgh/public_html/wp-content/themes/astra-child/functions.php:276 Stack trace: #0 /home/745301.cloudwaysapps.com/jfcnzyyzgh/public_html/wp-includes/shortcodes.php(356): display_woo_sku() #1
[internal function]: do_shortcode_tag() #2 
/home/745301.cloudwaysapps.com/jfcnzyyzgh/public_html/wp-includes/shortcodes.php(228): preg_replace_callback() #3 
/home/745301.cloudwaysapps.com/jfcnzyyzgh/public_html/wp-includes/class-wp-hook.php(307): do_shortcode() #4 
/home/745301.cloudwaysapps.com/jfcnzyyzgh/public_html/wp-includes/plugin.php(189): WP_Hook->apply_filters() #5 
/home/745301.cloudwaysapps.com/jfcnzyyzgh/public_html/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php(1811): apply_filters() #6 
/home/745301.cloudwaysapps.com/jfcnzyyzgh/public_html/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php(560): WP_REST_Posts_Controller->prepare_item_for_response() #7 
/home/745301.cloudwaysapps.com in /home/745301.cloudwaysapps.com/jfcnzyyzgh/public_html/wp-content/themes/astra-child/functions.php on line 276

I think it's perhaps an issue because sku = null since it's the editor

#2362673

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Diego,

The problem is the product variable may not contain a product object so the get_sku() function is failing.

Can you perform a var_dump($product) to see what is displayed on the frontend?

If nothing is displayed then this is where you have your issue.

Thanks,
Shane

#2362939
shortcode working.png

Think is the shortcode works it just fails in the editor but in the site itself it works obviously, not sure what to do with var_dump($product) and where to see the potential error.

hidden link

#2362997

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Diego,

Based on the page you're using it on, it is a view correct?

If so then it won't work like this because you're calling for the global product which being set on the page itself when the page is loaded.

In this case you're using a regular page to load a view which isn't a product. So you will need to modify your code to pass the ID from the view into the shortcode.

function display_woo_sku($atts) {

// Attributes
	$atts = shortcode_atts(
		array(
			'id' => '',
		),
		$atts
	);

global $product;

if (empty($product)){
$product = wc_get_product($atts['id']);
return $product->get_sku();
}else{
return $product->get_sku();
}

}
add_shortcode( 'woo_sku', 'display_woo_sku' );

Please try this in the view [woo_sku id='[wpv-post-id]']

Thanks,
Shane

#2364437

Hey,

No, still the same.
I'm using in a page template and the critical error is when entering the block editor. I can see I wasn't clear enough, the shortcode in itself works fine in the frontend the critical error occurs when using the block editor.

Fatal error: Uncaught Error: Call to a member function get_sku() on bool in /home/745301.cloudwaysapps.com/jfcnzyyzgh/public_html/wp-content/themes/astra-child/functions.php:287 Stack trace: #0
 /home/745301.cloudwaysapps.com/jfcnzyyzgh/public_html/wp-includes/shortcodes.php(356): display_woo_sku() #1
[internal function]: do_shortcode_tag() #2
/home/745301.cloudwaysapps.com/jfcnzyyzgh/public_html/wp-includes/shortcodes.php(228): preg_replace_callback() #3
/home/745301.cloudwaysapps.com/jfcnzyyzgh/public_html/wp-includes/class-wp-hook.php(307): do_shortcode() #4 
/home/745301.cloudwaysapps.com/jfcnzyyzgh/public_html/wp-includes/plugin.php(189): WP_Hook->apply_filters() #5 
/home/745301.cloudwaysapps.com/jfcnzyyzgh/public_html/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php(1811): apply_filters() #6
/home/745301.cloudwaysapps.com/jfcnzyyzgh/public_html/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php(560): WP_REST_Posts_Controller->prepare_item_for_response() #7
/home/745301.cloudwaysapps.com in /home/745301.cloudwaysapps.com/jfcnzyyzgh/public_html/wp-content/themes/astra-child/functions.php on line 287
#2364537

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Diego,

Would you mind allowing me to have admin access to the website so that I can have a more detailed look at this for you ?

Please where applicable please provide me with a link to an example page where I can see the issue.

I've enabled the private fields for your next response.

Thanks,
Shane

#2364673

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Diego,

I see the issue now.

The problem is that the shortcode isn't being restricted to a run area. I've added your shortcode to our Toolset custom code section and set it to only run on the frontend.

You shouldn't be getting the error on the backend anymore.

Thanks,
Shane

#2365207
blocks.png

That's excellent Shane, thank you.
While I have you here I have two issues:
1- Strange ¿bug?. In the block editor of the wordpress archive, while having AJAX pagination active, the blocks bug out and I cant' see them properly or edit them, but if I turn of AJAX pagination for some reason I can edit / etc. pic attached

2- I'm wondering in the wordpress archive to what I can hook into, I'm using a custom code to show an image depending on the stock status and I can hook into woocommerce_before_add_to_cart_quantity for the Single Product Page but doesn't work in the archive.

function woocommerce_my_callback() {
    // An example based on global $product
    // Get the global product object
    global $product;

    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Get stock status
        $product_stock_status = $product->get_stock_status();
        
        // Compare
        if ( $product_stock_status == 'onbackorder' ) {
            echo '<img src="<em><u>hidden link</u></em>" />';
        }
		if ( $product_stock_status == 'instock' ) {
            echo '<img src="<em><u>hidden link</u></em>" />';
        }
    }
}
add_action( 'woocommerce_before_add_to_cart_quantity', 'woocommerce_my_callback', 10 );
#2365245

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Diego,

Happy i'm able to assist you.


1- Strange ¿bug?. In the block editor of the wordpress archive, while having AJAX pagination active, the blocks bug out and I cant' see them properly or edit them, but if I turn of AJAX pagination for some reason I can edit / etc. pic attached

Is there any custom code on the view itself ? Meaning do you have any specific custom code that is targeting this view ? Also if you disable all the non-toolset plugins does the issue still remain?

2- I'm wondering in the wordpress archive to what I can hook into, I'm using a custom code to show an image depending on the stock status and I can hook into woocommerce_before_add_to_cart_quantity for the Single Product Page but doesn't work in the archive.

This is because you code is only targeting the post page itself with this line below.

 if ( is_a( $product, 'WC_Product' ) ) {
        // Get stock status
        $product_stock_status = $product->get_stock_status();
         
        // Compare
        if ( $product_stock_status == 'onbackorder' ) {
            echo '<img src="<em><u>hidden link</u></em>" />';
        }
        if ( $product_stock_status == 'instock' ) {
            echo '<img src="<em><u>hidden link</u></em>" />';
        }
    }

Essentially the conditional in the lines above is checking if it is a product page and not if there is an archive. Given that this is Woocommerce specific then Im not able to provide advanced support with crafting the code.

You will need to check if the page is an archive as well.
https://developer.wordpress.org/reference/functions/is_post_type_archive/

However modifying the woocommerce archive might be a little more tricky. Are you building out your product archive template with Toolset ? The reason is that you should be able to achieve this with our conditional blocks by checking the product stock status.

Please let me know.
Thanks,
Shane

#2365321

You know, I didn't even think about the conditional not sure why but I'll do that.
On the other issue, nope, even if I disable all plugins but toolset the issue still happens.
There's no code targeting the archive, only some JS but inside the editor so I doubt that is it.

#2365399

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Diego,

Can you let me know the page that is in your previous screenshot ? This way I can have a look at the view on my end.

Thanks,
Shane

#2365999

Hi, it's the archive "archiveWoo"