Данный код уберет численные значения товара на складе, оставив просто instock
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php include( 'wp-load.php' ); // Setup your custom query $args = [ 'post_type' => ['product'/*, 'post', 'page'*/], 'posts_per_page' => -1, 'post_status' => 'publish' ]; $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); global $post; $product = wc_get_product( $post->ID ); if($product->get_stock_quantity()>0 && $product->get_manage_stock() == 'yes' && $post->post_name != 'bra'){ echo 'Update stock for simple product ID: '.$post->ID.' slug: '.$post->post_name.' Title: '.$product->get_title().' Old quantity: '.$product->get_stock_quantity().'<br>'; // Using WC setters $product->set_manage_stock(false); $product->set_stock_quantity(0); $product->set_stock_status('instock'); // Save Product $product->save(); } if ($product->get_type() == "variable" && $post->post_name != 'bra') { $variations = $product->get_available_variations(); foreach($variations as $variation){ $variation_id = $variation['variation_id']; $variation_obj = new WC_Product_variation($variation_id); if($variation_obj->get_stock_quantity()>0 && $variation_obj->get_manage_stock() == 'yes'){ echo 'Update stock for variation product ID: '.$variation_id .' slug: '.$post->post_name.' Title: '.$variation_obj->get_title().' Old quantity: '.$variation_obj->get_stock_quantity().'<br>'; // Using WC setters $variation_obj->set_manage_stock(false); $variation_obj->set_stock_quantity(0); $variation_obj->set_stock_status('instock'); // Save Product $variation_obj->save(); } } } endwhile; wp_reset_query(); // Remember to reset echo 'stock update done'; ?> |
Создается отдельный php файл, к которому напрямую идет обращение из адресной строки браузера.