问题
In WooCommerce I'm using scraping/import plugin which doesn't recognise and I can't insert outofstock meta based on actual stock in the "product_visibility" taxonomy.
The thing is, that all out of stock products are hidden when the stock status is Out of stock, but products are still counted in the product categories. If _stock_status
product metadata is outofstock, the product_visibility
outofstock
post term should be also set for the product.
My Question is:
How to set the missing product_visibility
outofstock
post term when the product _stock_status
metadata is set as outofstock
?
I was trying something with code below. I'm not a programmer, but my logic was to get term in _stock_status... but It doesn't work :/
function wc_update_300_product_visibility() {
global $wpdb;
WC_Install::create_terms();
$featured_term = get_term_by( 'name', 'outofstock', '_stock_status' );
if ( $outofstock_term ) {
$wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = 'product_visibility' AND meta_value = 'yes';", $featured_term->term_taxonomy_id ) );
}
}
来源:https://stackoverflow.com/questions/61509962/set-product-visibility-in-woocommerce-to-out-of-stock-from-outofstock-metada