Ciao, il seguente codice è nella pagina Wooimporter.php (un plugin per importare prodotti in woocommerce), lo script legge i dati presi da una pagina web e li importa come prodotti, io devo far si che al momento dell'importazione se legge un attributo di nome 'arance rosse' me lo deve sostituire e inserire nel database con nome 'rosse', il codice è il seguente:
Codice PHP:
public static function addAttributes(array $features, $product_id)
{
if (!$product = \wc_get_product($product_id))
return false;
$attributes = $product->get_attributes();
$taxonomy_count = 0;
foreach ($features as $feature)
{
$f_name = \wc_clean($feature['name']);
$f_slug = self::getSlug($f_name);
// exists?
if (isset($attributes[$f_slug]) || isset($attributes['pa_' . $f_slug]))
continue;
$f_value = \wc_sanitize_term_text_based($feature['value']);
$f_value_array = self::value2Array($f_value);
$term_ids = array();
$taxonomy = '';
// Taxonomy exists?
if (\taxonomy_exists(\wc_attribute_taxonomy_name($f_name)))
{
// Taxonomy attribute
$taxonomy_count++;
if ($attr_id = self::createTaxonomyAttribute($f_name, $f_slug))
{
$taxonomy = \wc_attribute_taxonomy_name_by_id($attr_id);
// Register the taxonomy now so that the import works!
if (!\taxonomy_exists($taxonomy))
{
$taxonomy = TextHelper::truncate($taxonomy, 32, '');
\register_taxonomy($taxonomy, apply_filters('woocommerce_taxonomy_objects_' . $taxonomy, array('product')), apply_filters('woocommerce_taxonomy_args_' . $taxonomy, array('hierarchical' => true, 'show_ui' => false, 'query_var' => true, 'rewrite' => false)));
}
// Creates the term and taxonomy relationship if it doesn't already exist.
// It may be confusing but the returned array consists of term_taxonomy_ids instead of term_ids.
\wp_set_object_terms($product->get_id(), $f_value_array, $taxonomy);
$term_ids = array();
foreach ($f_value_array as $term)
{
if ($term_info = \term_exists($term, $taxonomy))
$term_ids[] = $term_info['term_id'];
}
$term_ids = array_map('intval', $term_ids);
} else
$attr_id = 0;
} else
{
// Local Attribute
$attr_id = 0;
}
$attribute = new \WC_Product_Attribute();
$attribute->set_id($attr_id); // 0 for product level attributes. ID for global attributes.
if ($taxonomy)
$attribute->set_name($taxonomy);
else
$attribute->set_name($f_name);
// attribute value or array of term ids/names.
if ($term_ids)
$attribute->set_options($term_ids);
else
$attribute->set_options($f_value_array);
$attribute->set_visible(true); // If visible on frontend.
$attributes[] = $attribute;
}
$product->set_attributes($attributes);
$res = $product->save();
if ($taxonomy_count)
\flush_rewrite_rules();
return $res;
}
public static function createTaxonomyAttribute($slug, $name, $type = 'text')
{
global $wpdb;
$attribute = array();
$attribute['attribute_label'] = \wc_clean($slug);
$attribute['attribute_name'] = \wc_sanitize_taxonomy_name($name);
$attribute['attribute_type'] = $type;
$attribute['attribute_orderby'] = 'menu_order';
// validate slug
if (strlen($attribute['attribute_name']) >= 28 || \wc_check_if_attribute_name_is_reserved($attribute['attribute_name']))
return false;
if (\taxonomy_exists(\wc_attribute_taxonomy_name($attribute['attribute_name'])))
return \wc_attribute_taxonomy_id_by_name($attribute['attribute_name']);
// Create the taxonomy
$insert = $wpdb->insert($wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute);
if (\is_wp_error($insert))
return false;
$id = $wpdb->insert_id;
\wp_schedule_single_event(time(), 'woocommerce_flush_rewrite_rules');
\delete_transient('wc_attribute_taxonomies');
return $id;
}
Come fare?
Grazie