通过代码添加属性分类不会出现在管理产品页面中

时间:2017-06-01 作者:walle

在通过代码插入新产品后,我手动插入了该属性:

$attributes = Array( 
                        "type_product1" => $data1, 
                        "type_product2" => $data2, 
                        "type_product3" => $data3,
                        "type_product4" => $data4, 
                        "type_product5" => $data5, 
                        "type_product6" => $data5
                    ); 

                foreach ($attributes as $key => $value) {
                    $term_taxonomy_ids = wp_set_object_terms( $product_id, $value, \'pa_\'.$key, true );
                    $product_attributes = Array (
                        \'pa_\'.$key => Array(
                        \'name\' => \'pa_\'.$key, // set attribute name
                        \'value\' => $value, // set attribute value
                        \'is_visible\' => 1,
                        \'is_variation\' => \'1\',
                        \'is_taxonomy\' => 1
                    )
                    );


                }
                    //Add as post meta
                    update_post_meta($post_ID, \'_product_attributes\', $product_attributes);
之后,如果我进入products->attribute,我会看到属性名称的单个项的计数增加,但如果我进入name attributes(custom product attribute)下的单个产品,我将只看到最后一个属性(type\\u product6),该属性的值设置为其他属性不显示。。。。奇怪的是,如果我转到属性页,单击单个属性项的计数,它会显示具有此属性集的产品列表!

我怎样才能修复它?

1 个回复
SO网友:walle

the solution is:

foreach ($attributes as $attr => $value) {
                            $attr = \'pa_\'.$attr;
                            wp_set_object_terms($post_ID, $value, $attr);
                             $thedata[sanitize_title($attr)] = Array(
                                        \'name\' => wc_clean($attr),
                                        \'value\' => $value,
                                        \'postion\' => \'0\',
                                        \'is_visible\' => \'1\',
                                        \'is_variation\' => \'1\',
                                        \'is_taxonomy\' => \'1\'
                                );
                            }

                        update_post_meta($post_ID, \'_product_attributes\', $thedata);
结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register