隐藏某些用户在Woocommerce上发布的产品

问题描述 投票:0回答:1

我试图根据发布它们的用户ID隐藏Woocommerce中的产品。

我创建了以下代码,但它不能很好地工作。

function Products_for_vendor() {
$args     = array( 'post_type' => 'product', 'post_author' => '2' );
$products = get_posts( $args );

    foreach ($products as $product->ID) {

        $post_id = $product->ID

        $terms = array( 'exclude-from-catalog', 'exclude-from-search' );
        wp_set_object_terms( $post_id, $terms, 'product_visibility', false );

    }

}

add_action( 'init', 'Products_for_vendor' );

隐藏帖子我提取了此查询中提到的代码:Change product visibility via PHP on Woocommerce 3+

任何帮助或评论都很受欢迎。

提前致谢。

php wordpress woocommerce
1个回答
0
投票

您不应该直接更改此类内容 - WooCommerce具有特殊功能,以确保将来与任何数据库结构更改兼容,并确保产品数据在其内部缓存中正确同步。

相反,在foreach循环内部,使用:

// Get an instance of the product
$theproduct = wc_get_product($product->ID);
// Change the product visibility (options are: 'hidden', 'visible', 'search' and 'catalog'.
$theproduct->set_catalog_visibility('hidden');
// Finally, save and sync the product changes
$theproduct->save();
© www.soinside.com 2019 - 2024. All rights reserved.