admin管理员组

文章数量:1410689

i written a script in woocmmerce to import 1000 simple product my script is working fine and it's added all the product at my wordpress site however the products are not showing in front end store. when i open the single product it show the product with prices but when i search the category it's not showing

when i edit the product and update it from admin panel by clicking update button without changing anything then the product display at my store i am not able to figure out the issue please help NOTE 1. i have already tried deleting Transients and hence it's not working

here is my Code

$querystr = "SELECT * FROM `table_name`";$pageposts = $wpdb->get_results($querystr, OBJECT);
foreach($pageposts as $productpost) {
            $post = array(
                     'post_title'   => $productpost->attribute,
                     'post_excerpt' => '<pre> '.$productpost->porduct_name."<br/>".$productpost->description.' <pre>',
                     'post_status'  => "publish",
                     'post_content' => 'test',
                     'post_name'    => $productpost->attribute, //name/slug
                     'post_type'    => "product"
                     );
                    $new_post_id = wp_insert_post( $post);
                    wp_set_object_terms ($new_post_id,'simple','product_type');
                    wp_set_object_terms( $new_post_id, 2321, 'product_cat');
                    update_post_meta( $new_post_id, '_stock_status', 'instock');
                    update_post_meta( $new_post_id, '_weight', "0.06" );
                    update_post_meta( $new_post_id, '_sku', $productpost->sku);
                    update_post_meta( $new_post_id, '_stock', "100" );
                    update_post_meta($new_post_id, '_regular_price', (float)$productprice);
                    update_post_meta($new_post_id, '_price', (float)$productprice);
                    update_post_meta($new_post_id,'_meta_key','_visiblity');
                    require_once(ABSPATH . 'wp-admin/includes/media.php');
                    require_once(ABSPATH . 'wp-admin/includes/file.php');
                    require_once(ABSPATH . 'wp-admin/includes/image.php');
                    $thumb_url = "image_path.jpg";

                    if (@getimagesize($thumb_url)) {
                    $tmp = download_url( $thumb_url );
                    preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $thumb_url, $matches);
                    $file_array['name'] = basename($matches[0]);
                    $file_array['tmp_name'] = $tmp;
                    if ( is_wp_error( $tmp ) ) {
                    @unlink($file_array['tmp_name']);
                    $file_array['tmp_name'] = '';
                    $logtxt .= "Error: download_url error - $tmp\n";
                    }else{
                    $logtxt .= "download_url: $tmp\n";
                    }
                    //use media_handle_sideload to upload img:
                    $thumbid = media_handle_sideload( $file_array, $new_post_id, 'gallery desc' );
                    // If error storing permanently, unlink
                    if ( is_wp_error($thumbid) ) {
                    @unlink($file_array['tmp_name']);
                    //return $thumbid;
                    $logtxt .= "Error: media_handle_sideload error - $thumbid\n";
                    }else{
                    $logtxt .= "ThumbID: $thumbid\n";
                    set_post_thumbnail($new_post_id, $thumbid);
                    }
                    }
                update_post_meta( $new_post_id, '_visibility', 'visible' );
                update_post_meta($new_post_id, 'post_status', 'publish');

i written a script in woocmmerce to import 1000 simple product my script is working fine and it's added all the product at my wordpress site however the products are not showing in front end store. when i open the single product it show the product with prices but when i search the category it's not showing

when i edit the product and update it from admin panel by clicking update button without changing anything then the product display at my store i am not able to figure out the issue please help NOTE 1. i have already tried deleting Transients and hence it's not working

here is my Code

$querystr = "SELECT * FROM `table_name`";$pageposts = $wpdb->get_results($querystr, OBJECT);
foreach($pageposts as $productpost) {
            $post = array(
                     'post_title'   => $productpost->attribute,
                     'post_excerpt' => '<pre> '.$productpost->porduct_name."<br/>".$productpost->description.' <pre>',
                     'post_status'  => "publish",
                     'post_content' => 'test',
                     'post_name'    => $productpost->attribute, //name/slug
                     'post_type'    => "product"
                     );
                    $new_post_id = wp_insert_post( $post);
                    wp_set_object_terms ($new_post_id,'simple','product_type');
                    wp_set_object_terms( $new_post_id, 2321, 'product_cat');
                    update_post_meta( $new_post_id, '_stock_status', 'instock');
                    update_post_meta( $new_post_id, '_weight', "0.06" );
                    update_post_meta( $new_post_id, '_sku', $productpost->sku);
                    update_post_meta( $new_post_id, '_stock', "100" );
                    update_post_meta($new_post_id, '_regular_price', (float)$productprice);
                    update_post_meta($new_post_id, '_price', (float)$productprice);
                    update_post_meta($new_post_id,'_meta_key','_visiblity');
                    require_once(ABSPATH . 'wp-admin/includes/media.php');
                    require_once(ABSPATH . 'wp-admin/includes/file.php');
                    require_once(ABSPATH . 'wp-admin/includes/image.php');
                    $thumb_url = "image_path.jpg";

                    if (@getimagesize($thumb_url)) {
                    $tmp = download_url( $thumb_url );
                    preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $thumb_url, $matches);
                    $file_array['name'] = basename($matches[0]);
                    $file_array['tmp_name'] = $tmp;
                    if ( is_wp_error( $tmp ) ) {
                    @unlink($file_array['tmp_name']);
                    $file_array['tmp_name'] = '';
                    $logtxt .= "Error: download_url error - $tmp\n";
                    }else{
                    $logtxt .= "download_url: $tmp\n";
                    }
                    //use media_handle_sideload to upload img:
                    $thumbid = media_handle_sideload( $file_array, $new_post_id, 'gallery desc' );
                    // If error storing permanently, unlink
                    if ( is_wp_error($thumbid) ) {
                    @unlink($file_array['tmp_name']);
                    //return $thumbid;
                    $logtxt .= "Error: media_handle_sideload error - $thumbid\n";
                    }else{
                    $logtxt .= "ThumbID: $thumbid\n";
                    set_post_thumbnail($new_post_id, $thumbid);
                    }
                    }
                update_post_meta( $new_post_id, '_visibility', 'visible' );
                update_post_meta($new_post_id, 'post_status', 'publish');
Share Improve this question edited Jul 14, 2017 at 21:06 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Jul 14, 2017 at 19:13 Neha KapoorNeha Kapoor 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Set product_visibility to visible, _stock_status key to instock, _stock key to some value also post_date and post_date_gmt set to date lower than today's date.

After placing this in your script your simple product become visible on shop page.

本文标签: woocommerce offtopicSimple Product not displaying in frontend