【问题标题】:Create new product attribute programmatically in Woocommerce在 Woocommerce 中以编程方式创建新产品属性
【发布时间】:2015-06-15 11:16:25
【问题描述】:

如何通过插件为 WooCommerce 创建属性? 我发现只有:

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

来自this stack-question

但这种方法需要某些产品的 id。我需要生成一些不附加到任何产品的属性。

【问题讨论】:

  • 但是默认属性 产品特定的,所以我不确定你在问什么。
  • 当我的插件激活时,我需要在这里joxi.ru/gmvegqWt185Vra 生成一些默认属性。我可以手动完成,但我想要 - 自动
  • 但是默认属性是什么意思?正如我所提到的,一个术语只能是特定产品的默认值。
  • 当我创建类似类别的属性时,我没有将他附加到任何产品,对吗?所以我需要生成一些属性而不绑定到产品。
  • 啊好吧....所以我想你想创建一个术语。在这种情况下,您需要wp_insert_term()

标签: php wordpress woocommerce product custom-taxonomy


【解决方案1】:

要创建一个术语,您可以使用wp_insert_term()

像这样:

wp_insert_term( 'red', 'pa_colors' );

其中colors 是您的属性的名称。属性的分类名称总是以pa_ 开头。

编辑 属性只是自定义分类法。或者您可以说它们是由用户在后端手动创建的动态分类法。尽管如此,自定义分类规则仍然适用。

您可以看到 source code here 循环遍历属性并在每个属性上运行 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' );

【讨论】:

  • 不,它将为 EXISTING 属性创建值。我需要创建自己的 pa_**** 元素
  • 那你需要register_taxonomy()。查看编辑。
  • @helgatheviking 除了使用 register_taxonomy 之外,还有其他方法可以创建 woocommerce 属性吗? register_taxonomy 使用了太多资源,我无法使用它,因为我以编程方式创建产品和属性。
  • @jameshwartlopez WooCommerce 属性 WordPress 分类法,所以你需要register_taxonomy()。作为一种可能的选择,您可能能够使用 API?但我不确定。
  • 似乎我没有其他方法,只能使用自定义属性。我认为 register_taxonomy 可能存在黑客攻击
【解决方案2】:

对于 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 文件中。经过测试并且可以工作。


基于:

相关: Create programmatically a product using CRUD methods in Woocommerce 3

【讨论】:

  • 似乎一直在为我创建重复的分类法。似乎taxonomy_exists 检查不起作用。
猜你喜欢
  • 1970-01-01
  • 2018-05-11
  • 2016-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-02
  • 1970-01-01
  • 2019-05-04
相关资源
最近更新 更多