I use rest API to manage the store. It connects correctly, I can add new products. Unfortunately, there is a problem with product updates, such that other plugins do not see the changes. Only by manually clicking update on the product, makes the changes visible. I mean if change color attribute with api from red to black, it works fine, but im using WP Gridbuilder for filtering and filter dosent catch these changes until manual update. Same thing with ACF fields, im using some on product page ane this fields also need manual update of product after api changes, to work. Is there any action that will simulate / force such an update after changes made by the API.
I was trying to simulate manual update, cache clean etc. with:
add_action('woocommerce_rest_insert_product', 'handle_product_update', 10, 3);
function handle_product_update($product, $request, $creating) {
$product_id = $product->get_id();
$post_data = array(
'ID' => $product_id,
'post_status' => 'publish',
'post_type' => 'product',
);
wp_update_post($post_data);
$product = wc_get_product($product_id);
$product->save();
clean_post_cache($product_id);
wp_cache_flush();
if (function_exists('acf_save_post')) {
acf_save_post($product_id);
}
do_action('woocommerce_update_product', $product_id);
$attributes = $product->get_attributes();
if (!empty($attributes)) {
foreach ($attributes as $attribute) {
if (is_object($attribute) && method_exists($attribute, 'get_id')) {
wp_update_term($attribute->get_id(), $attribute->get_taxonomy());
}
}
}
flush_rewrite_rules();
}
function update_product_meta($product_id, $meta_key, $meta_value) {
update_post_meta($product_id, $meta_key, $meta_value);
// Wywołanie akcji po aktualizacji metadanych
do_action('updated_post_meta', $meta_key, $meta_value, $product_id);
}
Chili Palmer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.