1

I have some products on WooCommerce, some of these products are published with a special tag.

I want these tags not to be changed in any way when I update the products.

(I update products remotely with a crawler software)

add_action('woocommerce_update_product', 'on_update_product', 10, 2);
function on_update_product($product_id, $product){

// code here
}

This is my code to access the product update process.

1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Community Bot
    Jan 31 at 16:44

1 Answer 1

0

To ensure that the special tags on your WooCommerce products remain unchanged during the update process, you'll need to modify your function to check for these tags and preserve them. Since you're using a crawler to update the products, the function can be tailored to ignore tag updates for products that have these special tags.

Here's a modified version of your function that should help with this:

add_action('woocommerce_update_product', 'on_update_product', 10, 2);
function on_update_product($product_id, $product){
    // Define your special tag(s) here
    $special_tags = ['special-tag-1', 'special-tag-2']; // Replace with your actual tag slugs

    // Get current product tags
    $current_tags = get_the_terms($product_id, 'product_tag');

    // Check if current product has any of the special tags
    $has_special_tag = false;
    if (!empty($current_tags)) {
        foreach ($current_tags as $tag) {
            if (in_array($tag->slug, $special_tags)) {
                $has_special_tag = true;
                break;
            }
        }
    }

    // If the product has a special tag, preserve these tags
    if ($has_special_tag) {
        $tag_ids = array_map(function($tag) { return $tag->term_id; }, $current_tags);
        wp_set_object_terms($product_id, $tag_ids, 'product_tag');
    }

    // Continue with other update processes if needed
    // ...
}

In this code:

You define the special tags you want to preserve. Before updating the product, it checks if the product has any of these special tags.

If the product has a special tag, the function preserves these tags by reapplying them.

Make sure to replace special-tag-1, special-tag-2 with the actual slugs of your special tags. This function is triggered during the product update process, and it will ensure that if a product has one of the specified special tags, those tags won't be altered during the update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.