php - WooCommerce create new attributes programmatically -



php - WooCommerce create new attributes programmatically -

how can create attributes woocommerce plugin? find :

wp_set_object_terms( $object_id, $terms, $taxonomy, $append);

from this stack-question

but approach required id of product. need generate attributes not attached products.

to create term can utilize wp_insert_term()

like so:

wp_insert_term( 'red', 'pa_colors' );

where colors name of attribute. taxonomy name of attribute prepended pa_.

edit attributes simply custom taxonomies. or dynamic taxonomies manually created user in back-end. still same custom taxonomy rules apply.

you can see source code here loops through attributes , runs register_taxonomy() on each. create new attribute (remember taxonomy) need run register_taxonomy() , simple prepend pa_ start of taxonomy name.

mimicking of values of taxonomy args core, 'colors' attribute.

add_action( 'woocommerce_after_register_taxonomy', 'so_29549525_register_attribute' ); function so_29549525_register_attribute(){ $permalinks = get_option( 'woocommerce_permalinks' ); $taxonomy_data = array( 'hierarchical' => true, 'update_count_callback' => '_update_post_term_count', 'labels' => array( 'name' => 'colors', 'singular_name' => 'color', 'search_items' => sprintf( __( 'search %s', 'woocommerce' ), $label ), 'all_items' => sprintf( __( 'all %s', 'woocommerce' ), $label ), 'parent_item' => sprintf( __( 'parent %s', 'woocommerce' ), $label ), 'parent_item_colon' => sprintf( __( 'parent %s:', 'woocommerce' ), $label ), 'edit_item' => sprintf( __( 'edit %s', 'woocommerce' ), $label ), 'update_item' => sprintf( __( 'update %s', 'woocommerce' ), $label ), 'add_new_item' => sprintf( __( 'add new %s', 'woocommerce' ), $label ), 'new_item_name' => sprintf( __( 'new %s', 'woocommerce' ), $label ) ), 'show_ui' => false, 'query_var' => true, 'rewrite' => array( 'slug' => empty( $permalinks['attribute_base'] ) ? '' : trailingslashit( $permalinks['attribute_base'] ) . sanitize_title( 'colors' ), 'with_front' => false, 'hierarchical' => true ), 'sort' => false, 'public' => true, 'show_in_nav_menus' => false, 'capabilities' => array( 'manage_terms' => 'manage_product_terms', 'edit_terms' => 'edit_product_terms', 'delete_terms' => 'delete_product_terms', 'assign_terms' => 'assign_product_terms', ) ); register_taxonomy( 'pa_colors', , array( 'product' ) ), $taxonomy_data ); }

php wordpress woocommerce

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -