woocommerce / woocommerce-blocks

(Deprecated) This plugin has been merged into woocommerce/woocommerce
https://wordpress.org/plugins/woo-gutenberg-products-block/
GNU General Public License v3.0
405 stars 218 forks source link

Blocks Error #4787

Closed CheVZwasSH closed 2 years ago

CheVZwasSH commented 3 years ago

Describe the bug

Fatal error: Uncaught Error: Call to undefined method WP_Error::get_data() in /usr/www/users/dictcajyhm/wp-content/plugins/woocommerce/packages/woocommerce-blocks/src/RestApi.php:64 Stack trace: #0 /usr/www/users/dictcajyhm/wp-content/plugins/woocommerce/packages/woocommerce-blocks/src/BlockTypes/AbstractBlock.php(349): Automattic\WooCommerce\Blocks\RestApi->get_routes_from_namespace('wc/store') woocommerce/woocommerce-blocks#12058 /usr/www/users/dictcajyhm/wp-content/plugins/woocommerce/packages/woocommerce-blocks/src/BlockTypes/AbstractBlock.php(287): Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->enqueue_data(Array) woocommerce/woocommerce-blocks#2 /usr/www/users/dictcajyhm/wp-content/plugins/woocommerce/packages/woocommerce-blocks/src/BlockTypes/AbstractBlock.php(84): Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->enqueue_assets(Array) woocommerce/woocommerce-blocks#3 /usr/www/users/dictcajyhm/wp-includes/class-wp-block.php(221): Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->render_callback(Array, '', Object(WP_Block)) woocommerce/woocommerce-blocks#4 /usr/www/users/dictcajyhm/wp-includes/blocks.php(868): WP_Blo in /usr/www/users/dictcajyhm/wp-content/plugins/woocommerce/packages/woocommerce-blocks/src/RestApi.php on line 64

To reproduce

  1. Go to 'Elementor Theme Builder'
  2. Click on 'Edit Shop'
  3. See error

WordPress:

Desktop (please complete the following information):

Aljullu commented 3 years ago

Hi @CheVZwasSH, thanks for reporting this issue!

Do you have a link to the plugin you are using? I tried with Elementor Website Builder, but I couldn't find any 'Edit Shop' menu item. Besides that, did you contact with the Elementor team? Even though the error mentions our code, the error is occurring in an Elementor page so they would probably be able to assist you with more detail.

nerrad commented 3 years ago

Hi @CheVZwasSH ,

Thanks for taking the time to leave your original report, however we've been unable to successfully reproduce this. It's been a while with no additional information or answers to the latest questions so I'm going to go ahead and close this issue.

You're still able to leave comments if you have additional info and if that helps us with reproducing we can re-open the issue then.

litexservice commented 3 years ago

@nerrad additional info may be found here:

elementor/elementor#16662

I hope this will provide you with some insight into the issue. If not please let us know and we will open yet another issue, this time within the Woo & Woo Blocks repository.

choijun commented 2 years ago

Hello guys,

I found the cause of this problem, it caused by core block api of WooCommerce

This is an error of WooCommerce block core api. As I mentioned above, $namespace_index variable may return an error ( WP_ERROR ) and in WP_ERROR class there is no get_data() method – you can see here

You need to check the validity of the variable before calling the get_data() method.

Please check it and solve it soon.

Thank you.

Aljullu commented 2 years ago

Thanks for providing more details @litexservice and for suggesting a fix @choijun.

Unfortunately, I'm not used to Elementor and I'm still unable to reproduce the issue. In addition to that, testing steps from this issue and elementor/elementor#16662 are very different, so I don't know which one we should follow (I tried both and wasn't able to reproduce).

Would any of you be able to post new steps to reproduce? If you could share a video of it would make things much easier because it will make it clear we understand all steps.

Thank you in advance!

choijun commented 2 years ago

Hi @Aljullu

I am sorry for late response, and for this problem - you won't need to reproduce the problem if you have programming knowledge, if you look at the screenshot I sent you will see that the current code will throw an exception.

Also I recorded to reproduce the problem https://somup.com/crXvDd0uDK

I reinstalled the WP site and imported the WC demo data, and add the Product Category List widget to the Sidebar then created a new page using Elementor and using the Sidebar widget .

This problem doesn't come up often, it just happens occasionally and i tested on PHP 7.4 and 8.0

Please check it out.

Thank you.

Aljullu commented 2 years ago

Thanks for sharing the video, @choijun. This was very useful! I think I could reproduce it once, but wasn't able to reproduce it again anymore. :confused:

I will re-open the issue, but I would like a second pair of eyes on this. @mikejolley is that something you could help me with? Some users are reporting a PHP fatal error when using Elementor. According to @choijun, the issue is on this line, and could be fixed checking that $namespace_index is not an error. Would something along these lines work or do you have any other ideas of what might be the source of the issue?

    public function get_routes_from_namespace( $namespace ) {
        $rest_server     = rest_get_server();
        $namespace_index = $rest_server->get_namespace_index(
            [
                'namespace' => $namespace,
                'context'   => 'view',
            ]
        );

+       if ( ! $namespace_index instanceof \WP_REST_Response ) {
+           return null;
+       }

        $response_data = $namespace_index->get_data();

        return isset( $response_data['routes'] ) ? $response_data['routes'] : null;
    }
mikejolley commented 2 years ago

@Aljullu looks like it can return an error. Not sure why unless the namespace is not registered for whatever reason. Based on the return type (WP_REST_Response|WP_Error) we could add an is_wp_error check or what you suggested above.

mikejolley commented 2 years ago

Looks like this is actually a duplicate of https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/4683 so lets tackle it there.