admin管理员组文章数量:1134247
I want to add a product attribute (wc_create_attribute) & assigned terms (wp_insert_term) via the functions.php.
Example:
$args = array(
'name' => "Color",
'slug' => "color",
'order_by' => "menu_order",
'has_archives' => "",
);
wc_create_attribute($args);
$add = wp_insert_term( "Blue", 'pa_color', array( 'slug' => "blue" ) );
print_r($add);
The wc_create_attribute()
works fine, but when the wp_insert_term()
runs it throws an error because of “Invalid taxonomy”.
When I then reload the page, it works fine because then the taxonomy is not invalid anymore.
So it seems that the wp_insert_term()
are executed before the create_attribute somehow. I don’t understand why it does not work in one attempt.
Maybe you can help me
I want to add a product attribute (wc_create_attribute) & assigned terms (wp_insert_term) via the functions.php.
Example:
$args = array(
'name' => "Color",
'slug' => "color",
'order_by' => "menu_order",
'has_archives' => "",
);
wc_create_attribute($args);
$add = wp_insert_term( "Blue", 'pa_color', array( 'slug' => "blue" ) );
print_r($add);
The wc_create_attribute()
works fine, but when the wp_insert_term()
runs it throws an error because of “Invalid taxonomy”.
When I then reload the page, it works fine because then the taxonomy is not invalid anymore.
So it seems that the wp_insert_term()
are executed before the create_attribute somehow. I don’t understand why it does not work in one attempt.
Maybe you can help me
Share Improve this question edited Sep 27, 2018 at 12:27 Fabian Marz 5723 silver badges10 bronze badges asked Sep 27, 2018 at 10:00 BenmayBenmay 3617 silver badges17 bronze badges 4 |2 Answers
Reset to default 1wc_create_attribute( array(
'name' => 'color',
'type' => 'select'
) );
register_taxonomy( 'pa_color', array( 'product' ), array() );
wp_insert_term( 'Rood', 'pa_color' );
Try to reregister taxonomies
$args = array(
'name' => "Color",
'slug' => "color",
'order_by' => "menu_order",
'has_archives' => "",
);
wc_create_attribute($args);
WC_Post_Types::register_taxonomies();
$add = wp_insert_term( "Blue", 'pa_color', array( 'slug' => "blue" ) );
print_r($add);
本文标签: woocommerce offtopicWPError on attributeterm insert
版权声明:本文标题:woocommerce offtopic - WP_Error on attribute + term insert 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736818043a1954172.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wc_create_attribute()
saves the attribute information to the database in a custom table. WooCommerce reads this table registers taxonomies for the saved attributes on theinit
hook at priority5
. To do what you want you need to create the attribute beforeinit
at priority5
, and then assign to to the product afterinit
at priority5
. – Jacob Peattie Commented Sep 27, 2018 at 10:31