I am using the WooCommerce Rest API to query the products on a WooCommerce store. The Product ID ($id) is retrieved from the order line items. Some of the old orders in my database have products that have since been deleted from the store.

I would like to catch this error and return "0.0" but the error keeps stopping my script. Please see the section from my script below and the error from the logs. I am using the Exception wrong in this case?

   try {
        $product = $this->client->get('products/' . $id);
    } catch (Exception $e) {
        return 0.0;

ERROR 404 | GET /wp-json/wc/v3/products/0 HTTP/1.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.