Mahesh Patel
He creado atributos de productos de Woo-Commerce programáticamente con el siguiente código:
$data = array(
'name' => 'My attribute',
'slug' => wc_sanitize_taxonomy_name(wp_unslash('My attribute')),
'type' => 'select',
'order_by' => 'menu_order',
'has_archives' => 1
);
wc_create_attribute( $data );
y este código agrega atributos con éxito y son visibles en la lista de woo-commerce en Productos-> Atributos, pero después de eso, intenté agregar algunos terms
a ellos que no están afectados a la lista de atributos usando el siguiente código:
wp_insert_term( 'term_1' ,'pa_'.$data['slug']);
también, confirmo que cuando vuelvo el resultado de var_dump(get_term_by('name','term_1','pa_'.$data['slug']))
Obtengo resultados con ID de término, nombre, slug, term_taxonomy_id, etc., pero el problema es que estos términos no están visibles en los atributos del producto de WooCommerce y tampoco en la página de edición del producto.
Porque me he buscado mucho antes de llegar a una solución que funcione para Woocommerce 3.8+
doy esta respuesta para que otros obtengan ayuda sobre un tema realmente confuso como los atributos de Woocommerce.
He creado dos métodos/funciones: createAttribute
y createTerm
basado en el código de muestra que Woocomerce
proporciona como ejemplo: Ver código
function createAttribute(string $attributeName, string $attributeSlug): ?\stdClass {
delete_transient('wc_attribute_taxonomies');
\WC_Cache_Helper::incr_cache_prefix('woocommerce-attributes');
$attributeLabels = wp_list_pluck(wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name');
$attributeWCName = array_search($attributeSlug, $attributeLabels, TRUE);
if (! $attributeWCName) {
$attributeWCName = wc_sanitize_taxonomy_name($attributeSlug);
}
$attributeId = wc_attribute_taxonomy_id_by_name($attributeWCName);
if (! $attributeId) {
$taxonomyName = wc_attribute_taxonomy_name($attributeWCName);
unregister_taxonomy($taxonomyName);
$attributeId = wc_create_attribute(array(
'name' => $attributeName,
'slug' => $attributeSlug,
'type' => 'select',
'order_by' => 'menu_order',
'has_archives' => 0,
));
register_taxonomy($taxonomyName, apply_filters('woocommerce_taxonomy_objects_' . $taxonomyName, array(
'product'
)), apply_filters('woocommerce_taxonomy_args_' . $taxonomyName, array(
'labels' => array(
'name' => $attributeSlug,
),
'hierarchical' => FALSE,
'show_ui' => FALSE,
'query_var' => TRUE,
'rewrite' => FALSE,
)));
}
return wc_get_attribute($attributeId);
}
function createTerm(string $termName, string $termSlug, string $taxonomy, int $order = 0): ?\WP_Term {
$taxonomy = wc_attribute_taxonomy_name($taxonomy);
if (! $term = get_term_by('slug', $termSlug, $taxonomy)) {
$term = wp_insert_term($termName, $taxonomy, array(
'slug' => $termSlug,
));
$term = get_term_by('id', $term['term_id'], $taxonomy);
if ($term) {
update_term_meta($term->term_id, 'order', $order);
}
}
return $term;
}
Entonces se pueden usar como:
createAttribute('Colors', 'my-colors');
createTerm('Red', 'my-red', 'my-colors', 10);
createTerm('Green', 'my-green', 'my-colors', 20);
createTerm('Blue', 'my-blue', 'my-colors', 30);
-
Gran trabajo especialmente en
register_taxonomy
–Steve Moretz
1 de marzo a las 13:37