Creating custom Woocommerce attributes

Question

I am trying to create custom Woocommerce attributes.
I already read a lot of answers everywhere, and my code is support to work, but not

What I understand is if I want to create a new attribute, first I have to create new taxonomy.

this is my code:

public function attributes($attributes_data, $product_id)
{

    $attributes = array(); // Initializing

    // Loop through defined attribute data
    foreach ($attributes_data as $key => $attribute_array) {

        if (isset($attribute_array->attribute) && isset($attribute_array->value)) {
            // Clean attribute name to get the taxonomy
            $taxonomy = 'pa_' . wc_sanitize_taxonomy_name($attribute_array->attribute);
            
            $option_term_ids = array(); // Initializing

            $this->register_taxonomy($taxonomy);

            if (!term_exists($attribute_array->value, $taxonomy)) {
                wp_set_object_terms($product_id, $attribute_array->value, $taxonomy, true);
                $term_data = wp_insert_term($attribute_array->value, $taxonomy);
                $term_id = $term_data->term_id;
            } else {
                wp_set_object_terms($product_id, $attribute_array->value, $taxonomy, true);
                $term_id = get_term_by('name', $attribute_array->value, $taxonomy)->term_id;
            }

            // Loop through defined attribute data
            $attributes[$taxonomy] = array(
                'name' => $taxonomy,
                'value' => $term_id, // Need to be term IDs
                'position' => ++$key,
                'is_visible' => 1,
                'is_variation' => 0,
                'is_taxonomy' => '1'
            );
        }
    }

    // Save the meta entry for product attributes
    update_post_meta($product_id, '_product_attributes', $attributes);

}

First of all, I am create new taxonomy if is not exist

 private function register_taxonomy($taxonomy)
{
    if (!taxonomy_exists($taxonomy)) {

        register_taxonomy(
            $taxonomy,
            array('product'),
            array(
                'label' => __(str_replace("-", " ", str_replace("pa_", "", $taxonomy))),
                'rewrite' => array('slug' => strtolower($taxonomy)),
                'hierarchical' => true,
            )
        );

    }

}

Then I check, if the term does not exist I create new one otherways I get the ID.

I dumped everywhere, the term ID always return.

I create manually a new attribute called “material” and when the code finds that term it is added to the product without any issue so I think the update_post_meta it is work

This is the $attribute dump

'pa_shipping-within' => 
    array (size=6)
      'name' => string 'pa_shipping-within' (length=18)
      'value' => int 81
      'position' => int 2
      'is_visible' => int 1
      'is_variation' => int 0
      'is_taxonomy' => string '1' (length=1)
  'pa_colour' => 
    array (size=6)
      'name' => string 'pa_colour' (length=9)
      'value' => int 120
      'position' => int 3
      'is_visible' => int 1
      'is_variation' => int 0
      'is_taxonomy' => string '1' (length=1)

I cannot understand what is wrong.

Thanks in advance

0
Gabriele Carbonai 4 months 0 Answers 18 views 0

Leave an answer