Inicio › Toolset Professional Support › [Resuelto] Critical error when using custom Shortcode but only on Toolset Layout**
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.
Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.
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)
Este tema contiene 20 respuestas, tiene 2 mensajes.
Última actualización por Diego Walter Ricciardi hace 2 años, 6 meses.
Asistido por: Shane.
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?
enlace oculto
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.
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
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
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
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.
enlace oculto
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
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
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
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
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>enlace oculto</u></em>" />'; } if ( $product_stock_status == 'instock' ) { echo '<img src="<em><u>enlace oculto</u></em>" />'; } } } add_action( 'woocommerce_before_add_to_cart_quantity', 'woocommerce_my_callback', 10 );
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>enlace oculto</u></em>" />'; } if ( $product_stock_status == 'instock' ) { echo '<img src="<em><u>enlace oculto</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
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.
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
Hi, it's the archive "archiveWoo"