Title: html_entity_decode() while trying to insert product variations
Last modified: June 28, 2024

---

# html_entity_decode() while trying to insert product variations

 *  Resolved [ayyzenli](https://wordpress.org/support/users/ayyzenli/)
 * (@ayyzenli)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/html_entity_decode-while-trying-to-insert-product-variations/)
 * Hello!
 * I’ve been trying for a while to insert variations for a product, from the error
   logs I’ve done, it looks like it inserts them, but when the function finishes,
   I get the error PHP Fatal error: Uncaught TypeError: html_entity_decode(): Argument#
   1 ($string) must be of type string, array given.
   I’ve been trying to debug this
   for a whole day now, and to no luck. Sometimes I’ve gotten rid of this error,
   but then the variations don’t have the attributes.
 * Now I tried to see if the attributes actually are inserted into the variation
   using wc_get_product_variation_attributes(), but I get the error when it runs.
   So I’m guessing something is wrongly inserted, but I’m not sure what.
 *     ```wp-block-code
       function insert_product_variations($post_id, $variations) {    foreach ($variations as $index => $variation) {        $variation_post = array(            'post_title'   => 'Variation #' . ($index + 1) . ' of ' . count($variations) . ' for product #' . $post_id,            'post_name'    => 'product-' . $post_id . '-variation-' . ($index + 1),            'post_status'  => 'publish',            'post_parent'  => $post_id,            'post_type'    => 'product_variation',            'guid'         => home_url() . '/?product_variation=product-' . $post_id . '-variation-' . ($index + 1)        );        $variation_post_id = wp_insert_post($variation_post);        if (is_wp_error($variation_post_id)) {            error_log('Failed to create variation: ' . $variation_post_id->get_error_message());            continue;        }        $variation_obj = new WC_Product_Variation($variation_post_id);        $variation_attributes = array();        foreach ($variation['attributes'] as $attribute => $value) {            $taxonomy = 'pa_' . sanitize_title($attribute);            error_log('Processing taxonomy: ' . $taxonomy);            if (!taxonomy_exists($taxonomy)) {                register_taxonomy(                    $taxonomy,                    'product_variation',                    array(                        'hierarchical' => false,                        'label'        => ucfirst($attribute),                        'query_var'    => true,                        'rewrite'      => array('slug' => sanitize_title($attribute)),                    )                );            }            if (!term_exists($value, $taxonomy)) {                wp_insert_term($value, $taxonomy);            }            $term = get_term_by('name', $value, $taxonomy);            $term_slug = sanitize_text_field($term->slug);            error_log('Term slug: ' . $term_slug . ' for taxonomy: ' . $taxonomy);            error_log('Variation attributes: ' . print_r($variation_attributes, true));            wp_set_post_terms($post_id, $value, $taxonomy, true);            error_log('Set post terms for taxonomy ' . $taxonomy . ': ' . print_r($value, true));            $variation_attributes[$taxonomy] = sanitize_text_field($term_slug);        }        error_log('Variation attributes: '. print_r($variation_attributes, true));        $variation_obj->set_attributes($variation_attributes);        if (!empty($variation['regular_price'])) {            $variation_obj->set_regular_price($variation['regular_price']);        }        if (!empty($variation['sale_price'])) {            $variation_obj->set_sale_price($variation['sale_price']);        }        if (!empty($variation['stock_qty'])) {            $variation_obj->set_stock_quantity($variation['stock_qty']);            $variation_obj->set_manage_stock(true);            $variation_obj->set_stock_status('instock');        } else {            $variation_obj->set_manage_stock(false);        }        $variation_obj->save();        error_log('Variation #' . ($index + 1) . ' info: ' . print_r($variation_obj, true));        error_log('Variation #' . ($index + 1) . ' attributes: ' . print_r($variation_obj->get_attributes(), true));    }    error_log('variation attributes after saving: ' . print_r(wc_get_product_variation_attributes($variation_post_id), true));}
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [carolm29 (woo-hc)](https://wordpress.org/support/users/carolm29/)
 * (@carolm29)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/html_entity_decode-while-trying-to-insert-product-variations/#post-17863878)
 * Hey there, [@ayyzenli](https://wordpress.org/support/users/ayyzenli/)! Thanks
   for contacting us.
 * To confirm, do you face any issues if you try to add variations to the product
   via the default WooCommerce interface? 
   You can find more about this in this **
   [guide](https://woocommerce.com/document/variable-product/)**.
 * While we can’t provide support for code customization as [per our support policy](https://www.woocommerce.com/support-policy/),
   we do our best to offer advice and direct you to appropriate resources.
 * You can visit the [WooCommerce Facebook group](https://www.facebook.com/groups/advanced.woocommerce/)
   or the `#developers` channel of the [WooCommerce Community Slack](https://woocommerce.com/community-slack/).
   We’re lucky to have a great community of open-source developers for WooCommerce,
   and many of our developers hang out there too.
 * Have a wonderful day!
 *  Thread Starter [ayyzenli](https://wordpress.org/support/users/ayyzenli/)
 * (@ayyzenli)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/html_entity_decode-while-trying-to-insert-product-variations/#post-17865783)
 * Hi [@carolm29](https://wordpress.org/support/users/carolm29/) . Thanks for answering!
 * Just checked, when making it manually through the interface I don’t get this 
   error. Seems like somewhere and something turns into an array when using update_post_meta()
   for attributes.
 * I used a bad solution for this, by changing the function where the fatal error
   happens. Added to the wc_get_text_attributes function, a check if $raw_attributes
   is an array, and if it is, it returns an empty array.
 * By the looks of it, everything looks fine, since this error was showing up in
   the price column in the interface, and now it shows the price correctly.
   Will
   check out the groups you sent me, so I can find the right fix for this and not
   change WooCommerce source code.
 * Thanks again!
 *  Plugin Support [Shameem – a11n](https://wordpress.org/support/users/shameemreza/)
 * (@shameemreza)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/html_entity_decode-while-trying-to-insert-product-variations/#post-17867640)
 * Hi [@ayyzenli](https://wordpress.org/support/users/ayyzenli/)
 * Thanks for letting us know.
 * Meanwhile, I will mark this thread as resolved. Should you have further inquiries,
   kindly [create a new topic here](https://wordpress.org/support/plugin/woocommerce/#new-topic-0).
 * Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘html_entity_decode() while trying to insert product variations’ is closed
to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)

 * 3 replies
 * 3 participants
 * Last reply from: [Shameem – a11n](https://wordpress.org/support/users/shameemreza/)
 * Last activity: [1 year, 11 months ago](https://wordpress.org/support/topic/html_entity_decode-while-trying-to-insert-product-variations/#post-17867640)
 * Status: resolved