• Resolved room34

    (@room34)


    There’s a high probability that “you’re doing it wrong” applies here, as I am building a block theme for the first time.

    I found that I needed to override some of the HTML of the default WooCommerce block templates for archives and single products, so I handled that by creating templates/single-product.html (etc.) in my theme, copying the contents of woocommerce/packages/woocommerce-blocks/templates/templates/single-product.html into my new file, and making the necessary changes.

    On the front end, this works perfectly. But in the admin, if I try to edit the product, I get a fatal error:

    An error of type E_ERROR was caused in line 279 of the file /woocommerce/includes/admin/class-wc-admin-meta-boxes.php. Error message: Uncaught TypeError: in_array(): Argument #2 ($haystack) must be of type array, null given in /woocommerce/includes/admin/class-wc-admin-meta-boxes.php:279

    Here’s line 279 from class-wc-admin-meta-boxes.php in WC:

    if ( $block_template && in_array( 'product', $block_template->post_types ) ) {

    Again, I am probably doing something wrong that is causing this issue, but I also think that WooCommerce should be verifying that $block_template->post_types is actually an array before calling in_array(). Even just casting its type with (array) will fix the issue. (As a habit in my own code, I always do this when I’m dealing with variables that may or may not be an array.)

    I should also note that I am running this code on a brand new server running Ubuntu 22.04 with PHP 8.1; this is the first time I’ve spent any considerable time working with PHP 8.x, so it’s possible this code in WC is not causing any problems on servers running PHP 7.4 or earlier.

Viewing 15 replies - 1 through 15 (of 16 total)
  • Mirko P.

    (@rainfallnixfig)

    Hi @room34,

    Thanks for reaching out!

    For help with development-oriented questions or issues, you’ll want to try the following channels.

    I will also leave this thread open for now just in case someone else wants to jump in to help.

    * This particular forum is more focused on the default features packaged with the WooCommerce core plugin.

    @room34
    you are not doing anything wrong. i have tested it a lot. and they have an issue there. see my post: my issue

    Hi @room34

    As you are working on building a theme, here’s the WooCommerce Theme Developer Handbook for your reference: https://woocommerce.com/document/woocommerce-theme-developer-handbook/

    You can also get help at WooCommerce Developer Resources Portal: https://developer.woocommerce.com/

    Thread Starter room34

    (@room34)

    @margaretwporg Thanks; I’m already familiar with these resources but the handbook does not appear to be updated to deal with these kinds of issues. (I already skimmed that before starting this thread.) Do you know if updated docs are in the works?

    Thread Starter room34

    (@room34)

    @derrida Thanks! Definitely the same issue I’m seeing.

    *Edit*: Out of curiosity, are you also running PHP 8.x or are you on 7.x? I have a lot of “moving parts” (i.e. doing a lot of new things for the first time) in this scenario but I have a sneaking suspicion that PHP 8 is a factor in this being a problem.

    • This reply was modified 2 years, 6 months ago by room34.

    Hi @room34

    the handbook does not appear to be updated to deal with these kinds of issues. (I already skimmed that before starting this thread.) Do you know if updated docs are in the works?

    Thanks for letting us know. We will have our concerned department look into this.

    Kindly be informed that support for custom coding is beyond the scope of support we are able to provide in this forum.

    * This particular forum is for questions that are related to the WooCommerce core features.

    For help with development related topics you can make use of the resources listed above.

    Nevertheless, I’m going to leave this thread open for a bit to see if anyone is able to chime in to help you out here.

    Thanks

    Thread Starter room34

    (@room34)

    This may or may not be an additional relevant detail:

    The Quick Edit links on the Products index page don’t work. I’m guessing that whatever is causing the fatal error on the actual editing page is also causing an error in the AJAX call this link fires off. Here’s the error message from the JavaScript console:

    ReferenceError: Can’t find variable: inlineEditPost

    hi @room34
    after i read this i have tried with version 8, and the same error. by the way the same with the quick edit.
    then i have added my custom templates to the theme.json and the errors gone, also with the quick edit.

    the issue is that if you add a template from within the post editor, the errors come back.
    so eventually it is their code which does not check for array and type.

    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @room34.

    Just curious, but if you edit the code to cast its type with (array) on $block_template->post_types does it resolve the issue?

    Cheers!

    hi @rynald0s
    at least in my test, it does.

    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @room34 | @derrida

    Just to let you know that this does indeed appear to be a bug and it has been reported here if you would like to follow along:

    https://github.com/woocommerce/woocommerce/issues/33179

    Cheers!

    Thread Starter room34

    (@room34)

    @rynald0s Yes that fixes the problem but of course editing the source code of a third-party plugin is not a fix. (Not to say I haven’t done it… especially WooCommerce. But I’ve also been burned by that choice.)

    Thanks for sharing the GitHub link! That has taught me that I need to declare these templates in the theme.json file. I had no idea. (I am really struggling to piece FSE together from the currently available documentation.)

    • This reply was modified 2 years, 6 months ago by room34.
    Thread Starter room34

    (@room34)

    Then again… I’m not sure declaring the template in the theme.json file is actually the issue. I did that (partial excerpt):

    "customTemplates": [
    	{
    		"name": "single-product",
    		"postTypes": [ "product" ],
    		"title": "Single WooCommerce product"
    	}
    ]

    And I’m still getting the fatal error.

    HI @room34, declaring the single-product template will not fix this because that is actually the only template that skips the code that is responsible for the fatal error. Make sure that all other theme templates are declared as well.

    Thread Starter room34

    (@room34)

    @ravanh I do have all of the theme templates declared (hence ‘partial excerpt’ in my post)… I just wanted to make sure I had this formatted correctly.

    Looking at the source code I do see a part where Woo is specifically skipping the single-product template. So I’m not sure the presence/absence of these templates in theme.json is really what’s causing the issue… even when I do have all of my templates in there, I still get the error.

    As I just noted over here, I’m finding some hackish success using the get_block_file_template filter to force the right output, but I’m still trying to understand exactly why it’s coming to this in the first place.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Fatal error on product editing screen in custom block theme’ is closed to new replies.