Woocommerce products are not displayed on front-end . No products were found matching your selection [closed]

No products were found matching your selection.

This error appears only for non-admin users/guests on all category pages. But if I go to some product link directly, I can see the product page and add it to the cart.

I’ve already tried to change “default sorting” option.

The only way that I’ve found is to go to “Edit Product” and click “Update” button. Only after this action, this product will appear in shop. But it’s not a solution cuz I have a script which import new products time by time and I can’t click this button for 10000+ products.

Updated:

private function createProduct($product, $brand)
    {
        global $wpdb;
        $item = [
            'post_title' => $product['name'],
            'post_status' => 'publish',
            'post_type' => 'product'
        ];
        $postId = wp_insert_post($item);

        $meta_keys = [];
        $meta_keys['_visibility'] = 'visible';
        $meta_keys['_stock_status'] = 'instock';
        $meta_keys['wholesale_customer_wholesale_price'] = $product['wholesale_price'];
        $meta_keys['wholesale_customer_have_wholesale_price'] = $product['have_wholesale_price'];
        $meta_keys['_price'] = $product['price'];
        $meta_keys['_regular_price'] = $product['price'];
        $meta_keys['_weight'] = $product['weight'];
        $meta_keys['_sku'] = $product['sku'];
        $meta_keys['_stock'] = $product['stock_quantity'];
        $meta_keys['_manage_stock'] = 'yes';
        $meta_keys['_id_ds'] = $product['id_ds'];
        $meta_keys['_ean'] = $product['ean'];
        $meta_keys['_retail_price'] = $product['retail_price'];
        //$meta_keys['_product_attributes'] = maybe_serialize(wp_unslash($product['attributes']));;

        /* SET Attributes */
        $meta_keys['_product_attributes'] = maybe_serialize(wp_unslash($this->createAttributes($postId, $product['attributes'])));
        /* SET Attributes */

        $custom_fields = [];
        $place_holders = [];
        $query_string = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES ";
        foreach($meta_keys as $key => $value) {
            array_push($custom_fields, $postId, $key, $value);
            $place_holders[] = "('%d', '%s', '%s')";
        }
        $query_string .= implode(', ', $place_holders);
        $wpdb->query($wpdb->prepare("$query_string ", $custom_fields));

        $this->generateFeaturedImage($product['image'], $postId);

        wp_set_object_terms( $postId, 'simple', 'product_type' );
        wp_set_object_terms($postId, get_term_by('name', $brand->name,'product_cat')->term_id, 'product_cat');

        //update_post_meta($postId, '_product_attributes', $product['attributes']);
        return $postId;
    }

1 Answer
1

WooCommerce products have a lot of metadata. Give a look at this answer to see most of them.

In your case it seems that the problem is the missing _visibility.

Did you already try something like this?

$item = [
    'post_title' => $product['name'],
    'post_status' => 'publish',
    'post_type' => 'product',
];
$postId = wp_insert_post($item);
update_post_meta( $postId, '_visibility', 'visible' );

Leave a Comment