You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过代码根据Dokan卖家状态设置WooCommerce商品推荐状态?

问题

我曾尝试使用代码update_post_meta( $product->ID, '_featured', 'true')设置WooCommerce商品为推荐商品,但未生效。了解到该方法已被弃用,却找不到新的实现方式。我希望根据Dokan平台中推荐卖家和非推荐卖家的状态,批量将对应卖家的所有商品设置为推荐或非推荐状态,并编写了相关代码,现寻求正确的实现方案。

原尝试代码

update_post_meta( $product->ID, '_featured', 'true');

早期批量操作代码

$args = array( 'featured' => 'yes' );
$featured = dokan_get_sellers( $args );

$args = array( 'featured' => 'no' );
$not_featured = dokan_get_sellers( $args );

foreach ( $featured['users'] as $seller ) {
    $products_f = get_posts( array(
        'post_type' => 'product',
        'author' => $featured->ID, // 错误:应使用$seller->ID而非$featured->ID
        'posts_per_page' => -1
    ) );
}
foreach ( $not_featured['users'] as $seller ) {
    $products_nf = get_posts( array(
        'post_type' => 'product',
        'author' => $not_featured->ID, // 错误:应使用$seller->ID而非$not_featured->ID
        'posts_per_page' => -1
    ) );
}
foreach ( $products_f as $product) {
    update_post_meta( $product->ID, '_featured', 'true');
}
foreach ( $products_nf as $product) {
    update_post_meta( $product->ID, '_featured', 'false');
}

当前代码

add_action( 'set_featured_hook', 'set_featured' );
function set_featured(){
    $args_featured = array( 'featured' => 'yes' );
    $featured = dokan_get_sellers( $args_featured );
    
    $args_nf = array( 'featured' => 'no' );
    $not_featured = dokan_get_sellers( $args_nf );
    
    foreach ( $featured['users'] as $seller ) {
        $products_f = get_posts( array(
            'post_type' => 'product',
            'author' => $seller->ID,
            'posts_per_page' => -1
        ) );
    }
    
    foreach ( $not_featured['users'] as $seller ) {
        $products_nf = get_posts( array(
            'post_type' => 'product',
            'author' => $seller->ID,
            'posts_per_page' => -1
        ) );
    }
    foreach ($products_f as $product) {
        $wc_product_f = wc_get_product($product->ID);
        $wc_product_f ->set_featured(1);
        $wc_product_f ->save();
    }

    foreach ($products_nf as $product) {
        $wc_product_nf = wc_get_product($product->ID);
        $wc_product_nf->set_featured(0);
        $wc_product_nf->save();
    }
}

修正后的正确实现方案

原代码核心问题是循环卖家时会覆盖商品数组,最终仅处理最后一个卖家的商品,且缺少商品对象有效性判断。以下是修正后的代码:

add_action( 'set_featured_hook', 'set_featured_based_on_seller' );
function set_featured_based_on_seller(){
    // 获取推荐卖家列表
    $featured_sellers = dokan_get_sellers( array( 'featured' => 'yes' ) );
    // 获取非推荐卖家列表
    $non_featured_sellers = dokan_get_sellers( array( 'featured' => 'no' ) );

    // 批量设置推荐卖家的商品为推荐状态
    if ( !empty( $featured_sellers['users'] ) ) {
        foreach ( $featured_sellers['users'] as $seller ) {
            // 查询该卖家的所有已发布商品
            $products = get_posts( array(
                'post_type'      => 'product',
                'author'         => $seller->ID,
                'posts_per_page' => -1,
                'post_status'    => 'publish' // 仅处理已发布商品,可按需移除
            ) );

            foreach ( $products as $product_post ) {
                $wc_product = wc_get_product( $product_post->ID );
                // 确保商品对象有效再操作
                if ( $wc_product ) {
                    $wc_product->set_featured( true );
                    $wc_product->save();
                }
            }
        }
    }

    // 批量设置非推荐卖家的商品为非推荐状态
    if ( !empty( $non_featured_sellers['users'] ) ) {
        foreach ( $non_featured_sellers['users'] as $seller ) {
            $products = get_posts( array(
                'post_type'      => 'product',
                'author'         => $seller->ID,
                'posts_per_page' => -1,
                'post_status'    => 'publish'
            ) );

            foreach ( $products as $product_post ) {
                $wc_product = wc_get_product( $product_post->ID );
                if ( $wc_product ) {
                    $wc_product->set_featured( false );
                    $wc_product->save();
                }
            }
        }
    }
}

关键修正说明

  1. 避免数组覆盖:移除全局商品数组,每个卖家的商品在循环内直接处理,不会丢失前面卖家的商品数据
  2. 有效性判断:添加if ( $wc_product )确保商品对象存在,避免无效操作报错
  3. API规范贴合:使用true/false替代1/0,符合WooCommerce官方API参数规范
  4. 可选过滤:增加post_status参数,仅处理已发布商品,可根据业务需求调整
  5. 性能提示:若卖家和商品数量较多,建议用WP CLI执行该函数或添加后台异步任务,避免前端超时

内容的提问来源于stack exchange,提问作者user19846605

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 17:45:29