簡體   English   中英

在 Woocommerce 中以編程方式創建新的產品屬性

[英]Create new product attribute programmatically in Woocommerce

如何從插件為 WooCommerce 創建屬性? 我發現只有:

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

這個堆棧問題

但是這種方法需要某些產品的 id。 我需要生成一些不附加到任何產品的屬性。

要創建一個術語,您可以使用wp_insert_term()

像這樣:

wp_insert_term( 'red', 'pa_colors' );

其中colors是您的屬性的名稱。 屬性的分類名稱始終以pa_

編輯屬性只是自定義分類法。 或者您可以說它們是由用戶在后端手動創建的動態分類法。 盡管如此,自定義分類規則仍然適用。

您可以在此處查看源代碼,它循環遍歷屬性並在每個屬性上運行register_taxonomy() 因此,要創建一個新屬性(請記住它只是一個分類法),那么您需要運行register_taxonomy()並簡單地將pa_到分類法名稱的開頭。

模仿核心的分類參數的一些值會給你帶來類似這樣的“顏色”屬性。

/**
 * 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' );

更新 2020-11-18

屬性分類存儲在{$wpdb->prefix}woocommerce_attribute_taxonomies數據庫表中。 從那里 WooCommerce 對表中找到的每個人運行register_taxonomy() 因此,為了創建屬性分類法,應向該表中添加一行。 WooCommerce 有一個函數wc_create_attribute()可以為我們處理這個問題。 (從 3.2+ 開始)。

我測試屬性是否存在的條件邏輯不是最大的,我建議在插件的更新例程中使用某種版本選項。 但是作為使用wc_create_taxonomy()的示例,這應該插入一個名為“我的顏色”的屬性。

/**
 * 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' );

對於 Woocommerce 3+ (2018)

要從標簽名稱創建新的產品屬性,請使用以下函數:

function create_product_attribute( $label_name ){
    global $wpdb;

    $slug = sanitize_title( $label_name );

    if ( strlen( $slug ) >= 28 ) {
        return new WP_Error( 'invalid_product_attribute_slug_too_long', sprintf( __( 'Name "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
    } elseif ( wc_check_if_attribute_name_is_reserved( $slug ) ) {
        return new WP_Error( 'invalid_product_attribute_slug_reserved_name', sprintf( __( 'Name "%s" is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
    } elseif ( taxonomy_exists( wc_attribute_taxonomy_name( $label_name ) ) ) {
        return new WP_Error( 'invalid_product_attribute_slug_already_exists', sprintf( __( 'Name "%s" is already in use. Change it, please.', 'woocommerce' ), $label_name ), array( 'status' => 400 ) );
    }

    $data = array(
        'attribute_label'   => $label_name,
        'attribute_name'    => $slug,
        'attribute_type'    => 'select',
        'attribute_orderby' => 'menu_order',
        'attribute_public'  => 0, // Enable archives ==> true (or 1)
    );

    $results = $wpdb->insert( "{$wpdb->prefix}woocommerce_attribute_taxonomies", $data );

    if ( is_wp_error( $results ) ) {
        return new WP_Error( 'cannot_create_attribute', $results->get_error_message(), array( 'status' => 400 ) );
    }

    $id = $wpdb->insert_id;

    do_action('woocommerce_attribute_added', $id, $data);

    wp_schedule_single_event( time(), 'woocommerce_flush_rewrite_rules' );

    delete_transient('wc_attribute_taxonomies');
}

代碼位於活動子主題(或活動主題)的 function.php 文件中。 測試和工作。


基於:

相關: 在 Woocommerce 3 中使用 CRUD 方法以編程方式創建產品

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM