0

We are using WP All Import to import product data from a CSV into WooCommerce. Everything works fine (great plugin!) but we have one problem:

Two of our product attributes contain disallowed symbols:

< 10 million views
> 10 million views

Wordpress automatically removes the '<' and '>' symbols meaning that the second attribute is treated as a duplicate and therefore doesn't get imported.

WP All Import's developers suggest using the wp_insert_term_data hook to change the slugs as they're being inserted into the database.

This is a good idea as we would then be able to maintain both attributes. For example:

< 10 million views becomes lt-10-million-views
> 10 million views becomes gt-10-million-views

I'm sure this is pretty simple, but I'm still a newbie when it comes to hooking into Wordpress! What's the best way to do this?

Thanks in advance for any help!

1

You add the following snippet to your theme's functions.php file;

The "wp_insert_term_data" filter allows you to modify a term's name, slug or term_group before it is added to the database. This works for terms added via WP All Import and also manually through admin as well.

The filter passes all taxonomy terms, not just woocommerce attributes so you would need to add the taxonomy's slug (attribute)

add_filter('wp_insert_term_data', function ($data, $taxonomy, $args) {

        //replace my-attribute with attribute slug
        if ($taxonomy == 'pa_my-attribute') {

            //Replace all < with lt-
            if (false !== strpos($data['name'], '&lt;')) {
                $data['slug'] = 'lt-' . $data['slug'];
            }

            //Replace all > with gt-
            if (false !== strpos($data['name'], '&gt;')) {
                $data['slug'] = 'gt-' . $data['slug'];
            }
        }

        return $data;

}, 10, 3);

To get the attribute slug go to Woocommerce > Attributes and copy the slug from there. Be sure to include pa_ before i.e my-attribute becomes pa_my-attribute.

enter image description here

As you can see it works with all < and > terms

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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