$e ) { /* translators: Placeholders: %1$s - error message */ $message = sprintf( __( 'There was an error trying sync products using the Catalog Batch API for job %1$s: %2$s', 'facebook-for-woocommerce' ), $job->id, $e->getMessage() ); facebook_for_woocommerce()->log( $message ); } } } /** * Processes a single item. * * @param mixed $item * @param object|\stdClass $job * @return array|null * @throws PluginException In case of invalid sync request method. */ public function process_item( $item, $job ) { list( $item_id, $method ) = $item; if ( ! in_array( $method, [ Sync::ACTION_UPDATE, Sync::ACTION_DELETE ], true ) ) { throw new PluginException( "Invalid sync request method: {$method}." ); } if ( Sync::ACTION_UPDATE === $method ) { $request = $this->process_item_update( $item_id ); } else { $request = $this->process_item_delete( $item_id ); } return $request; } /** * Processes an UPDATE sync request for the given product. * * @since 2.0.0 * * @param string $prefixed_product_id prefixed product ID * @return array|null * @throws PluginException In case no product was found. */ private function process_item_update( $prefixed_product_id ) { $product_id = (int) str_replace( Sync::PRODUCT_INDEX_PREFIX, '', $prefixed_product_id ); $product = wc_get_product( $product_id ); if ( ! $product instanceof \WC_Product ) { throw new PluginException( "No product found with ID equal to {$product_id}." ); } $request = null; if ( ! Products::product_should_be_deleted( $product ) && Products::product_should_be_synced( $product ) ) { if ( $product->is_type( 'variation' ) ) { $product_data = \WC_Facebookcommerce_Utils::prepare_product_variation_data_items_batch( $product ); } else { $product_data = \WC_Facebookcommerce_Utils::prepare_product_data_items_batch( $product ); } // extract the retailer_id $retailer_id = $product_data['retailer_id']; // NB: Changing this to get items_batch to work // retailer_id cannot be included in the data object unset( $product_data['retailer_id'] ); $product_data['id'] = $retailer_id; $request = [ 'method' => Sync::ACTION_UPDATE, 'data' => $product_data, ]; /** * Filters the data that will be included in a UPDATE sync request. * * @since 2.0.0 * * @param array $request request data * @param \WC_Product $product product object */ $request = apply_filters( 'wc_facebook_sync_background_item_update_request', $request, $product ); } return $request; } /** * Processes a DELETE sync request for the given product. * * @param string $prefixed_retailer_id Product retailer ID. */ private function process_item_delete( $prefixed_retailer_id ) { $retailer_id = str_replace( Sync::PRODUCT_INDEX_PREFIX, '', $prefixed_retailer_id ); $request = [ 'data' => [ 'id' => $retailer_id ], 'method' => Sync::ACTION_DELETE, ]; /** * Filters the data that will be included in a DELETE sync request. * * @since 2.0.0 * * @param array $request request data * @param string $retailer prefixed product retailer ID */ return apply_filters( 'wc_facebook_sync_background_item_delete_request', $request, $prefixed_retailer_id ); } /** * Sends item updates to Facebook. * * @param array $requests Array of JSON objects containing batch requests. Each batch request consists of method and data fields. * @return array An array of handles. * @throws ApiException In case of failed API request. */ private function send_item_updates( array $requests ): array { $facebook_catalog_id = facebook_for_woocommerce()->get_integration()->get_product_catalog_id(); $response = facebook_for_woocommerce()->get_api()->send_item_updates( $facebook_catalog_id, $requests ); $response_handles = $response->handles; $handles = ( isset( $response_handles ) && is_array( $response_handles ) ) ? $response_handles : array(); return $handles; } }