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.
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));
}
- You must be logged in to reply to this topic.