Home > Software engineering >  WooCommerce REST API - filter what meta_data is sent in orders
WooCommerce REST API - filter what meta_data is sent in orders

Time:10-30

enter image description hereProblem

We are using Zapier to fetch orders from WooCommerce, through the REST Api. We encountered an error that Zapier can't recieve some orders because of the data sent by WooCommerce is to large (more than 7MB).

This is caused by the plugin Fancy Product Designer, which sends a PNG image as a base64 string in the line_items meta_data (see example of this in the image above).

Can't find any documentation or hooks on how to filter the order data being sent by the Api.

Suggested solution

We need to filter the order data that WooCommerce is sending, and avoid sending the line_items -> meta_data -> field where the key is _fpd_product_thumbnail.

(We can't delete the field, since its used internally for other cases.)

CodePudding user response:

With the example provided by @VijayHardaha I was able to put together a working code.

For anyone facing the same issue with Fancy Product Designer sending to large data to Zapier, you can use the following code to remove the image data from the REST API for Zapier only.

add_filter('woocommerce_rest_prepare_shop_order_object', 'filter_order_response', 10, 3);
function filter_order_response($response, $post, $request){
  global $wpdb;
  $zapier_user_id = 1320; // User ID of the "Zapier" account in WordPress
  if(get_current_user_id() == $zapier_user_id) {

    $i = 0;
    $lineItemsCount = count($response->data["line_items"]);

    while($i < $lineItemsCount) {
      $j = 0;
      $metaDataCount = count($response->data["line_items"][$i]["meta_data"]);
      
      while($j < $metaDataCount) {
        if($response->data["line_items"][$i]["meta_data"][$j]["key"] == "_fpd_product_thumbnail") {
          $response->data["line_items"][$i]["meta_data"][$j]["value"] = "";
          $response->data["line_items"][$i]["meta_data"][$j]["display_value"] = "";
        }
        $j  ;
      }
      $i  ;
    }
  }
    return $response;
}
  • Related