Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

php - Create new product attribute programmatically in Woocommerce

How can I create attributes for WooCommerce from a plugin? I find only :

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

From this stack-question

But this approach required id of some product. I need to generate some attributes not attached to any products.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To create a term you can use wp_insert_term()

like so:

wp_insert_term( 'red', 'pa_colors' );

where colors is the name of your attribute. The taxonomy name of an attribute is always prepended by pa_.

Edit Attributes are merely custom taxonomies. Or you could say they are dynamic taxonomies that are manually created by the user in the back-end. Still, all the same, custom taxonomy rules apply.

You can see the source code here which loops through the attributes and runs register_taxonomy() on each. So to create a new attribute (remember it is just a taxonomy) then you need to run register_taxonomy() and simple prepend pa_ to the start of the taxonomy name.

Mimicking some of the values of the taxonomy args from core would get you something like this for a 'Colors' attribute.

/**
 * Register a taxonomy.
 */
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'              => __( 'My Colors', 'your-textdomain' ),
                                'singular_name'     => __( 'Color', 'your-textdomain' ),
                                'search_items'      => __( 'Search colors', 'your-textdomain' ),
                                'all_items'         => __( 'All colors', 'your-textdomain' ),
                                'parent_item'       => __( 'Parent color', 'your-textdomain' ),
                                'parent_item_colon' => __( 'Parent color:', 'your-textdomain' ),
                                'edit_item'         => __( 'Edit color', 'your-textdomain' ),
                                'update_item'       => __( 'Update color', 'your-textdomain' ),
                                'add_new_item'      => __( 'Add new color', 'your-textdomain' ),
                                'new_item_name'     => __( 'New color', 'your-textdomain' )
                            ),
                        '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_my_color', array('product'), $taxonomy_data );

}
add_action( 'woocommerce_after_register_taxonomy', 'so_29549525_register_attribute' );

Update 2020-11-18

Attribute taxonomies are stored in the {$wpdb->prefix}woocommerce_attribute_taxonomies database table. And from there WooCommerce runs register_taxonomy() on each one that's found in the table. So in order to create an attribute taxonomy, a row should be added to this table. WooCommerce has a function wc_create_attribute() that will handle this for us. (Since 3.2+).

My conditional logic to test if the attribute exists is not the greatest and I would advise using some kind of version option in your plugin's update routine. But as an example of using wc_create_taxonomy() this should insert an attribute called "My Color".

/**
 * Register an attribute taxonomy.
 */
function so_29549525_create_attribute_taxonomies() {

    $attributes = wc_get_attribute_taxonomies();

    $slugs = wp_list_pluck( $attributes, 'attribute_name' );

    if ( ! in_array( 'my_color', $slugs ) ) {

        $args = array(
            'slug'    => 'my_color',
            'name'   => __( 'My Color', 'your-textdomain' ),
            'type'    => 'select',
            'orderby' => 'menu_order',
            'has_archives'  => false,
        );

        $result = wc_create_attribute( $args );

    }
}
add_action( 'admin_init', 'so_29549525_create_attribute_taxonomies' );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...