找不到与您的选择匹配的产品。
此错误仅对所有类别页面上的非管理员用户/来宾显示。但是如果我直接转到某个产品链接,我可以看到产品页面并将其添加到购物车中。
我已经尝试更改“默认排序”选项。
我找到的唯一方法是进入“编辑产品”并点击“更新”按钮。只有执行此操作后,此产品才会出现在商店中。但这并不是一个解决方案,因为我有一个脚本,可以一次又一次地导入新产品,我不能为10000多个产品单击此按钮。
更新日期:
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;
}
最合适的回答,由SO网友:Felipe Elia 整理而成
WooCommerce产品有很多元数据。看一看this answer 看到他们中的大多数。
在你的情况下,问题似乎是缺少_visibility
.
你已经试过这样的东西了吗?
$item = [
\'post_title\' => $product[\'name\'],
\'post_status\' => \'publish\',
\'post_type\' => \'product\',
];
$postId = wp_insert_post($item);
update_post_meta( $postId, \'_visibility\', \'visible\' );