Monday, November 7, 2022
HomeWordPress Developmentwoocommerce offtopic - can not create product attribute (taxonomy) and add phrases...

woocommerce offtopic – can not create product attribute (taxonomy) and add phrases on the identical execution


I’ve the next features to create a world product attribute and populate it with phrases based mostly on information from an API.

protected perform getOrCreateAttribute($slug, $title)
    {
        $taxonomyId = wc_attribute_taxonomy_id_by_name($slug);
        if (!empty($taxonomyId) && $taxonomyId > 0) {
            return $taxonomyId;
        }
        $taxonomyId = wc_create_attribute([
            'name' => $name,
            'slug' => $slug,
            'type' => 'select',
        ]);
        if ($taxonomyId instanceof WP_Error) {
            throw new Exception($taxonomyId->get_error_message());
        }
        return $taxonomyId;
    }

    protected perform getOrCreateTerm($slug, $title, $taxonomySlug)
    {
        $time period = get_term_by('slug', $slug, $taxonomySlug);
        if ($time period === false) WP_Error $time period */
            $time period = wp_insert_term($title, $taxonomySlug, ['slug' => $slug]);
            clean_term_cache($term->term_id, $taxonomySlug);
        
        if ($time period instanceof WP_Error) {
            throw new Exception($term->get_error_message());
        }
        return $time period;
    }

Now in my information processing loop I am attempting this technique to create an attribute if it does not exist, and the identical for every time period in that attribute.

Lastly I am attempting so as to add them to the product.

            foreach ($featuresGroup->Options as $function) {
                $taxonomyName = $feature->Characteristic->Identify->Worth;
                $taxonomySlug = $this->slugger->slugify($taxonomyName);
                $taxonomyId = $this->getOrCreateAttribute($taxonomySlug, $taxonomyName);
                $attribute = wc_get_attribute($taxonomyId);

                $termsToSet = [];
                $termName = $feature->PresentationValue;
                $termSlug = $this->slugger->slugify($termName);
                $time period = $this->getOrCreateTerm($termSlug, $termName, $attribute->slug);

                $termsToSet[] = $term->term_id;
                if (array_key_exists($attribute->slug, $productAttributes)) {
                    $productAttribute = &$productAttributes[$attribute->slug];
                    $productAttribute->set_options($termsToSet);
                } else {
                    $wcProductAttribute = new WC_Product_Attribute();
                    $wcProductAttribute->set_name($attribute->title);
                    $wcProductAttribute->set_id($attribute->id);
                    $wcProductAttribute->set_visible(1);
                    $wcProductAttribute->set_options($termsToSet);
                    $productAttributes[$attribute->slug] = $wcProductAttribute;
                }
            }

Now my downside right here is that I’ve to run the identical piece of code a number of occasions to really get a end result and I am undecided why. Usually on the primary run I am encountering an error

Invalid taxonomy

Through the wp_insert_term execution which as an alternative of making the WP_Term produces a WP_Error

Nonetheless if I run it a number of occasions, every thing finally will get generated.

I am undecided what is going on on right here.

I’ve tried clearing the caches for phrases and taxonomies in case it was failing as a result of cache, to no avail.

Let me add some information for instance the $attribute variable earlier than getOrCreateTerm perform name

enter image description here

And the results of taxonomy_exists exhibits false throughout wp_insert_term despite the fact that it was clearly outlined earlier than and has an ID

enter image description here

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments