attributes - 通过 PHP 代码添加 WooCommerce 属性

标签 attributes woocommerce product

我可以看到很多关于添加 woocommerce 产品属性 的问题。但是这样做,我可以在这个屏幕上看到那些添加的属性

wp-admin/edit.php?post_type=product&page=product_attributes

我的问题是我们如何通过 PHP 代码添加属性,以便它出现在上面指定的 woocommerce 属性屏幕中?我的目的是让这些属性在“分层导航”小部件下可见以过滤掉。

最佳答案

以下代码将以编程方式创建属性,该属性将显示在后端的 product_attributes 页面上。

        global $wpdb; 

        $insert = $wpdb->insert(
            $wpdb->prefix . 'woocommerce_attribute_taxonomies',
            array(
                'attribute_label'   => 'name',
                'attribute_name'    => 'slug',
                'attribute_type'    => 'type',
                'attribute_orderby' => 'order_by',
                'attribute_public'  => 1
            ),
            array( '%s', '%s', '%s', '%s', '%d' )
        );

        if ( is_wp_error( $insert ) ) {
            throw new WC_API_Exception( 'woocommerce_api_cannot_create_product_attribute', $insert->get_error_message(), 400 );
        }

        // Clear transients
        delete_transient( 'wc_attribute_taxonomies' );

用相关值更改nameslug 等。

关于attributes - 通过 PHP 代码添加 WooCommerce 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32516305/

相关文章:

python - 内置属性错误: 'str' object has no attribute 'name'

coding-style - 使用属性以 Python 方式限制属性访问

php - 使用 WooCommerce wc_get_product_terms 函数对产品属性术语进行排序

php - 在 WooCommerce 中的正常价格之前显示销售价格

php - 将折扣百分比添加到 Woocommerce 中的可变产品价格范围

eclipse - Eclipse RCP 中的插件、功能和产品之间有什么区别?

windows - 在 Windows 资源管理器中隐藏 Vim 备份 (*~)

javascript - 如何从功能标签的页面源代码中获取值?

wordpress - WooCommerce REST API - 获取订单的自定义字段

php - 如何将产品添加到现有的付费 Woocommerce 订单中?