WooCommerce - 按下按钮更新所有产品说明(站点范围)

WooCommerce - Update All Product Descriptions (site-wide) on Button Press

我一直在研究以下代码,希望能够创建一个按钮 - 按下它 - 然后我网站上的所有描述都会自动生成。梦想.

我的问题:为什么我的代码没有按预期更新所有产品?

到目前为止,我有以下代码,虽然它在一定程度上可以工作,但当按钮位于产品页面上时无法正常工作 - 它将更新该产品页面描述以及所有空描述实例ID 大于当前产品的产品,但不包含以下任何内容?!

我一直在使用以下 post 来尝试帮助我找到答案: 但是到目前为止我还没有成功!

我当前的代码:

// EDIT ALL PRODUCTS //

add_action( 'woocommerce_admin_process_product_object', 'update_products_by_x' );
function update_products_by_x(){
    
    if(isset($_POST['button_all_descriptions']) && $_POST['button_all_descriptions'] == 'All_Descriptions'){
    
        $product_all = get_posts( array(
            'post_type'        => 'product','product_variation',
            'post_status'      => 'publish','future','draft','pending','private','trash','auto-draft','inherit',
            'fields'           => 'ids'
        ) );

        // Loop through product Ids
        foreach ( $product_all as $product_id ) {
    
            // Get the WC_Product object
            $WCproduct = wc_get_product($product_id);
            
                if($WCproduct->is_type('simple') or $WCproduct->is_type('variable')){
                
                    $title = $WCproduct->get_name();
                    $mpn = $WCproduct->get_meta('sp_wc_barcode_field');
                    $description = $WCproduct->get_description();
                    $output = '';
                    
                    if(!empty($title)){
            
                        $output .= "<p>A " . $title;
                    
                    }if(!empty($mpn)){

                        $output .= " (MPN: " . $mpn . ").";             
                    
                    }if(empty($description)){
                        
                        $WCproduct->set_description($output);
                        $WCproduct->save();
                        
                    }   
                }
        }
    }
}

您可能有一个默认结果限制,限制了您 get_posts 调用 returns

的产品数量
$product_all = get_posts( array(
    'post_type'        => 'product','product_variation',
    'post_status'      => 'publish','future','draft','pending','private','trash','auto-draft','inherit',
    'fields'           => 'ids',
    'numberposts'      => -1
) );