首页 > 解决方案 > woocommerce 的问题设置子类别

问题描述

我正在尝试从数组中插入产品。
产品创建成功,除子类别外,值正常插入。
这里的问题是子类别注册为顶级(没有父级)或者根本没有注册。
任何人都知道我在这里缺少什么:(?

        $product_to_create = array(
            'post_content' => '',
            'post_status' => "publish",
            'post_title' => $myproduct['6'], 
            'post_status' => 'publish',
            'post_date' => $myproduct['22'],
            'post_parent' => '',
            'post_type' => "product",
                    );
                $post_id= wp_insert_post($product_to_create, true);
                if (!$post_id) //
                {
                    return false;
                }            
                update_post_meta($post_id, '_price', $myproduct['17']);
                update_post_meta($post_id, '_regular_price', $myproduct['17']);
                update_post_meta($post_id, '_sku', $myproduct['2'] );
                wp_set_object_terms($post_id, 'simple', 'product_type');

              if(strlen($myproduct['13']) > 1){
                    $parent_cata = term_exists($myproduct['13'], 'product_cat');
                    if($parent_cata !== null) //
                        {
                            wp_set_object_terms($post_id, $myproduct['13'] , 'product_cat');
                        }
                        else
                        {
                            $parent_term_a_id = $parent_cata['term_id'];  
                            wp_insert_term(
                                $myproduct['13'],  
                                'product_cat', 
                                array(
                                'slug' => 'sub-cat',
                                'parent'=> $parent_term_a_id
                                )
                            );
                            wp_set_object_terms($post_id, $myproduct['13'] , 'product_cat');
                        }
                    }
                    else{wp_set_object_terms($post_id, $myproduct['12'] , 'product_cat');}

标签: phpwordpresswoocommercecategories

解决方案


推荐阅读