Search code examples
phpwoocommercehook-woocommercewordpress-rest-apiwoocommerce-rest-api

Remove a field from WooCommerce API


My website communicates with retail management software for everything related to products on WooCommerce. Whenever synchronisation takes place, all fields present are sent to WooCommerce overwriting existing data if present.

Since I use the "Description" field within the retail management software for other purposes than WooCommerce where I manually enter SEO-optimised product descriptions, is there a way to disable the writing of the "description" and "short_description" fields during the synchronisation phase? I have no way to manipulate the retail management software and was wondering if there was a function that would allow me to do this.

After your suggestion, no many products were synced to WooCommerce. Here's why:

CRITICAL Uncaught ArgumentCountError: Too few arguments to function alter_product_fields(), 1 passed in /home/u191122490/domains/hange.com/public_html/wp-includes/class-wp-hook.php on line 309 and exactly 3 expected in /home/u191122490/domains/hange.com/public_html/wp-content/themes/bazaar-child/functions.php:91
Stack trace:
#0 /home/u191122490/domains/hange.com/public_html/wp-includes/class-wp-hook.php(309): alter_product_fields(Object(WC_Product_Variable))
#1 /home/u191122490/domains/hange.com/public_html/wp-includes/plugin.php(189): WP_Hook->apply_filters(Object(WC_Product_Variable), Array)
#2 /home/u191122490/domains/hange.com/public_html/wp-content/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-products-controller.php(724): apply_filters('woocommerce_res...', Object(WC_Product_Variable), Object(WP_REST_Request), true)
#3 /home/u191122490/domains/hange.com/public_html/wp-content/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-crud-con in /home/u191122490/domains/hange.com/public_html/wp-content/themes/bazaar-child/functions.php alla riga 91

Solution

  • The hook woocommerce_rest_pre_insert_{$this->post_type}_object can be used for altering before inserting/updating a post type through WC Rest API. The $this->post_type can be 'product', 'shop_order', 'shop_coupon'...etc.

    /**
    * Filters an object before it is inserted via the REST API.
    *
    * The dynamic portion of the hook name, `$this->post_type`,
    * refers to the object type slug.
    *
    * @param WC_Data         $product  Object object.
    * @param WP_REST_Request $request  Request object.
    * @param bool            $creating If is creating a new object.
    */
    return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $product, $request, $creating );
    

    This $creating object decides whether it is an update or insert call.

    add_filter('woocommerce_rest_pre_insert_product_object', 'alter_product_fields', 10, 3);
    
    
    function alter_product_fields($product, $request, $creating) {
    
    // $creating -- True If is creating a new object. False is update request
    
    if(!$creating){
        $existing_product_details = wc_get_product($product->get_id());
        $product->set_description($existing_product_details->get_description());
        $product->set_short_description($existing_product_details->get_short_description());
    
        return $product;
    }