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