首页 文章

如何将帖子转换为具有类别的产品?

提问于
浏览
0

我正在研究woocommerce产品Convert.I我将点击导入按钮转换为woocommerce产品.Convert工作正常,但我创建了相同的类别在帖子中,我需要导入与帖子相同类别的产品 . 例如我在帖子中有类别 .

风格 - 主要猫男 - Sub猫服装 - 男子Sub猫衬衫腰带女 - Sub猫服装 - 女子Sub Cat顶部钱包

我在woocommerce中创建了相同的类别 . 现在,当我导入帖子时,如果帖子属于女装类别,那么进口产品应该转到女装类别 . 产品应该按照类别和子类别进口 . 这是我的帖子转换为产品 .

foreach ($items as $post) { 
    $post_thumbnail_id = get_post_thumbnail_id( $post->ID );                
    $getpostid = $wpdb->get_results( "
        SELECT post_id 
        FROM " . $wpdb->prefix ."postmeta AS pm 
        INNER JOIN ".$wpdb->prefix."posts AS p 
            ON p.ID=pm.post_id 
        WHERE pm.`meta_key` = 'admitted_goods_id' 
        AND pm.`meta_value` ='".$post->ID."' 
        AND p.post_status='publish' 
    ");

    foreach($getpostid as $k=>$postdata) {
        $postId              = $postdata->post_id;
        $admited_goodsId[$k] = get_post_meta($postId, "admitted_goods_id", true);
    }

    if($post->ID==$admited_goodsId[0]) { 
        echo "Updated";
    } else { 
        //echo "Insert";        
        $post_id = wp_insert_post( array(
            'post_title'   =>  $post->post_title,
            'post_content' => $post->post_content,
            'post_status'  => 'publish',
            'post_type'    => "product",
        ) );

        wp_set_object_terms( $post_id, 'external', 'product_type' ); 
        update_post_meta( $post_id, '_visibility', 'visible' ); 
        update_post_meta( $post_id, '_downloadable', 'no' ); 
        update_post_meta( $post_id, '_thumbnail_id',$post_thumbnail_id);
        update_post_meta( $post_id, 'admit_ds_id',$post->ID);
        update_post_meta( $post_id, '_stock_status', 'instock'); 
        update_post_meta( $post_id, '_button_text','Buy Now');
        update_post_meta( $post_id, '_brand', $pdata->vendor );
        update_post_meta( $post_id, '_model', $pdata->model );  

        $term_list = wp_get_post_terms($post->ID, 'adm_category', array("fields" => "all"));
        foreach($term_list as $term) {
            wp_set_post_terms( $post_id, $term->name, 'product_cat', false );
        }

        $getOtherdata = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix ."admitad_product_data where post_id=".$post->ID); 

        foreach($getOtherdata as $pdata) { 
            //echo $pdata->price;
            update_post_meta( $post_id, '_regular_price', $pdata->old_price );
            update_post_meta( $post_id, '_sale_price', $pdata->price );
            update_post_meta( $post_id, '_product_url',$pdata->url);
            update_post_meta( $post_id, '_price', $pdata->price );
            update_post_meta( $post_id, '_currencyId', $pdata->currencyId );
        }
    }   
}

1 回答

  • 0

    您修复了我的代码中的两个错误,请查看以下内容:

    <?php 
    
    foreach ($items as $post) {
    
            $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
    
            $getpostid = $wpdb->get_results( "SELECT post_id FROM " . $wpdb->prefix ."postmeta as pm inner join ".$wpdb->prefix."posts as p on p.ID=pm.post_id WHERE pm.`meta_key` = 'admitted_goods_id' AND pm.`meta_value` ='".$post->ID."' and p.post_status='publish' ");
            foreach($getpostid as $k=>$postdata) 
            {
                $postId = $postdata->post_id;
                $admited_goodsId[$k] = get_post_meta($postId, "admitted_goods_id", true);
            } 
    
            if($post->ID==$admited_goodsId[0])
            { 
                echo "Updated";
    
    
            }else 
            { 
                //echo "Insert"; 
    
                $post_id = wp_insert_post( array(
                'post_title' =>  $post->post_title,
                'post_content' => $post->post_content,
                'post_status' => 'publish',
                'post_type' => "product",
                ) );  
                wp_set_object_terms( $post_id, 'external', 'product_type' ); 
                update_post_meta( $post_id, '_visibility', 'visible' ); 
                update_post_meta( $post_id, '_downloadable', 'no' ); 
                update_post_meta($post_id,'_thumbnail_id',$post_thumbnail_id);
                update_post_meta($post_id,'admit_ds_id',$post->ID);
                update_post_meta( $post_id, '_stock_status', 'instock'); 
                update_post_meta($post_id,'_button_text','Buy Now');
                update_post_meta( $post_id, '_brand', $pdata->vendor );
                update_post_meta( $post_id, '_model', $pdata->model );  
    
    
    
    
    
                //$term_list = wp_get_post_terms($post->ID, 'adm_category', array("fields" => "all"));
                $term_list = wp_get_post_categories( $post->ID );
                // $term_list variable is an array containing IDs of all the post categories
    
                // foreach($term_list as $term) 
                // {  
    
                //     wp_set_post_terms( $post_id, $term->name, 'product_cat', false );
    
                // } 
                // wp_set_post_term can't be used to update Product Category/Terms since this function can only be used to update Terms of native post type not custom post type. In order to update Terms of custom post type which is Products in our case we need to use wp_set_object_terms function.
    
    
    
    
                wp_set_object_terms( $post_id, $term_list, 'product_cat' );
    
    
    
    
                $getOtherdata = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix ."admitad_product_data where post_id=".$post->ID); 
                foreach($getOtherdata as $pdata)
                { 
                    //echo $pdata->price;
                    update_post_meta( $post_id, '_regular_price', $pdata->old_price );
                    update_post_meta( $post_id, '_sale_price', $pdata->price );
                    update_post_meta($post_id,'_product_url',$pdata->url);
                    update_post_meta( $post_id, '_price', $pdata->price );
                    update_post_meta( $post_id, '_currencyId', $pdata->currencyId );
                }   
    
        }   
    }  
    
    ?>
    

相关问题