co-cart / co-cart

🛒 CoCart makes it easy to decouple your WooCommerce store via a customizable REST API that takes the pain out of developing – allowing you to build fast and flexible headless stores.

Home Page:https://cocartapi.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug: Unable to validate product data passed when updating item quantity. Product data returns null

seb86 opened this issue · comments

Description

Somehow the product data is not returning on a rare occasion and instead returns null so it's unable to validate the quantity. It has been reported to only be doing this on a specific product according to the "Alwin Roosen" on CoCart's Slack.

Original conversation: https://cocart.slack.com/archives/CMGGJP03U/p1662140478611689

Source of issue

https://github.com/co-cart/co-cart/blob/trunk/includes/api/cocart/v2/cart/class-cocart-update-item-controller.php#L82,L84

Screenshots

screenshot

Possible solution

Fetch the product data again based on the ID of the product.

$_product = ! is_null( $current_data['data'] ) ? $current_data['data'] : null;

// If product data is somehow not there on a rare occasion then we need to get that product data to validate it.
if ( is_null( $_product ) ) {
	$_product = wc_get_product( $current_data['variation_id'] ? $current_data['variation_id'] : $current_data['product_id'] );
}

Additional info
PHP: 7.4.30

Patched in v3.7.6

I'm afraid the issue (or another one) is still ocurring. Didn't happen again until yesterday and today though:

2022-10-04T16:33:33+00:00 CRITICAL Uncaught TypeError: Argument 2 passed to CoCart_Cart_V2_Controller::validate_quantity() must be an instance of WC_Product or null, bool given, called in /var/www/html-local/wp-content/plugins/cart-rest-api-for-woocommerce/includes/api/cocart/v2/cart/class-cocart-update-item-controller.php on line 89 and defined in /var/www/html-local/wp-content/plugins/cart-rest-api-for-woocommerce/includes/api/cocart/v2/cart/class-cocart-cart-controller.php:332
Stack trace:
#0 /var/www/html-local/wp-content/plugins/cart-rest-api-for-woocommerce/includes/api/cocart/v2/cart/class-cocart-update-item-controller.php(89): CoCart_Cart_V2_Controller->validate_quantity(1, false)
#1 /var/www/html-local/wp-includes/rest-api/class-wp-rest-server.php(1143): CoCart_Update_Item_v2_Controller->update_item(Object(WP_REST_Request))
#2 /var/www/html-local/wp-includes/rest-api/class-wp-rest-server.php(990): WP_REST_Server->respond_to_request(Object(WP_REST_Request), '/cocart/v2/cart...', Array, NULL)
#3 /var/www/html-local/wp-includes/rest-api/ in /var/www/html-local/wp-content/plugins/cart-rest-api-for-woocommerce/includes/api/cocart/v2/cart/class-cocart-cart-controller.php op de lijn 332

2022-10-04T17:32:00+00:00 CRITICAL Uncaught TypeError: Argument 2 passed to CoCart_Cart_V2_Controller::validate_quantity() must be an instance of WC_Product or null, bool given, called in /var/www/html-local/wp-content/plugins/cart-rest-api-for-woocommerce/includes/api/cocart/v2/cart/class-cocart-update-item-controller.php on line 89 and defined in /var/www/html-local/wp-content/plugins/cart-rest-api-for-woocommerce/includes/api/cocart/v2/cart/class-cocart-cart-controller.php:332
Stack trace:
#0 /var/www/html-local/wp-content/plugins/cart-rest-api-for-woocommerce/includes/api/cocart/v2/cart/class-cocart-update-item-controller.php(89): CoCart_Cart_V2_Controller->validate_quantity(1, false)
#1 /var/www/html-local/wp-includes/rest-api/class-wp-rest-server.php(1143): CoCart_Update_Item_v2_Controller->update_item(Object(WP_REST_Request))
#2 /var/www/html-local/wp-includes/rest-api/class-wp-rest-server.php(990): WP_REST_Server->respond_to_request(Object(WP_REST_Request), '/cocart/v2/cart...', Array, NULL)
#3 /var/www/html-local/wp-includes/rest-api/ in /var/www/html-local/wp-content/plugins/cart-rest-api-for-woocommerce/includes/api/cocart/v2/cart/class-cocart-cart-controller.php op de lijn 332

2022-10-05T08:56:28+00:00 CRITICAL Uncaught TypeError: Argument 2 passed to CoCart_Cart_V2_Controller::validate_quantity() must be an instance of WC_Product or null, bool given, called in /var/www/html-local/wp-content/plugins/cart-rest-api-for-woocommerce/includes/api/cocart/v2/cart/class-cocart-update-item-controller.php on line 89 and defined in /var/www/html-local/wp-content/plugins/cart-rest-api-for-woocommerce/includes/api/cocart/v2/cart/class-cocart-cart-controller.php:332
Stack trace:
#0 /var/www/html-local/wp-content/plugins/cart-rest-api-for-woocommerce/includes/api/cocart/v2/cart/class-cocart-update-item-controller.php(89): CoCart_Cart_V2_Controller->validate_quantity(1, false)
#1 /var/www/html-local/wp-includes/rest-api/class-wp-rest-server.php(1143): CoCart_Update_Item_v2_Controller->update_item(Object(WP_REST_Request))
#2 /var/www/html-local/wp-includes/rest-api/class-wp-rest-server.php(990): WP_REST_Server->respond_to_request(Object(WP_REST_Request), '/cocart/v2/cart...', Array, NULL)
#3 /var/www/html-local/wp-includes/rest-api/ in /var/www/html-local/wp-content/plugins/cart-rest-api-for-woocommerce/includes/api/cocart/v2/cart/class-cocart-cart-controller.php op de lijn 332

While the issue is happening in the same location. This error is due to the product no longer existing which is why it returns false. I will create a new issue to add more validation here.

Issue connected: #356