从 WooCommerce 中删除字段 API

Remove a field from WooCommerce API

我的网站与零售管理软件进行通信,以处理与 WooCommerce 上的产品相关的所有内容。每当发生同步时,所有存在的字段都会发送到 WooCommerce 并覆盖现有数据(如果存在)。

由于我将零售管理软件中的“描述”字段用于 WooCommerce 之外的其他目的,我在 WooCommerce 中手动输入 SEO 优化的产品描述,有没有办法禁用“描述”和“short_description" 字段在同步阶段?我无法操作零售管理软件,想知道是否有功能可以让我这样做。

根据您的建议,没有多少产品同步到 WooCommerce。原因如下:

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

挂钩 woocommerce_rest_pre_insert_{$this->post_type}_object 可用于通过 WC Rest API 在 inserting/updating 之前更改 post 类型。 $this->post_type 可以是 'product'、'shop_order'、'shop_coupon'...等

/**
* 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 );

这个 $creating 对象决定它是更新调用还是插入调用。

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;
}