Prevent category updates in WordPress Woocommerce REST API

I take care of an E-Commerce that receives the products automatically through the physical store’s system. The physical store automatically syncs products including names, prices, quantities and categories. The physical store system doesn’t allow me to choose the items I want it to update. It simply sends this data via REST API and is automatically updated in E-Commerce. But I don’t want the category field to be updated. How can I handle POST receipt coming from the physical store system in WooCommerce? I’ve tried to manipulate the file “wp-includes/rest-api/class-wp-rest-server.php”, but I was unsuccessful.

I tried to manipulate the function below to delete the $data[‘cetagories’], but I didn’t get the expected result.

/**
     * Converts a response to data to send.
     *
     * @since 4.4.0
     * @since 5.4.0 The $embed parameter can now contain a list of link relations to include.
     *
     * @param WP_REST_Response $response Response object.
     * @param bool|string[]    $embed    Whether to embed all links, a filtered list of link relations, or no links.
     * @return array {
     *     Data with sub-requests embedded.
     *
     *     @type array $_links    Links.
     *     @type array $_embedded Embedded objects.
     * }
     */
    public function response_to_data( $response, $embed ) {
        $data  = $response->get_data();
        $links = self::get_compact_response_links( $response );

        if ( ! empty( $links ) ) {
            // Convert links to part of the data.
            $data['_links'] = $links;
        }

        if ( $embed ) {
            $this->embed_cache = array();
            // Determine if this is a numeric array.
            if ( wp_is_numeric_array( $data ) ) {
                foreach ( $data as $key => $item ) {
                    $data[ $key ] = $this->embed_links( $item, $embed );
                }
            } else {
                $data = $this->embed_links( $data, $embed );
            }
            $this->embed_cache = array();
        }
unset($data['categories']); //Retira a categoria dos dados a serem salvos

        return $data;
    }

Leave a Comment