Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
1.0 kB
1
Indexable
Never

function update_product_stock_b2b_meta() {
    // Get all product IDs
    $args = array(
        'post_type'      => array('product', 'product_variation'),
        'posts_per_page' => -1,
        'post_status'    => 'publish',
        'fields'         => 'ids',
    );

    $products = get_posts($args);

    foreach ($products as $product_id) {
        $product = wc_get_product($product_id);
        
        if ($product->is_type('simple')) {
            // For simple products
            update_post_meta($product_id, '_stock_b2b', 0);
        } elseif ($product->is_type('variable')) {
            // For variable products
            update_post_meta($product_id, '_stock_b2b', 0);
            
            // Get variations
            $variations = $product->get_children();
            
            foreach ($variations as $variation_id) {
                update_post_meta($variation_id, 'variable_stock_b2b', 0);
            }
        }
    }
}

add_action('init', 'update_product_stock_b2b_meta');
Leave a Comment