Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en-US">
- <head>
- <meta charset="UTF-8" />
- <title>List Product Stock</title>
- <meta name="viewport" content="width=device-width, initial-scale=1" />
- </head>
- <body>
- <h1>List Product Stock</h1>
- <?php
- set_time_limit( 360 );
- define('WP_USE_THEMES', false);
- /** Loads the WordPress Environment and Template */
- require( dirname( __FILE__ ) . '/wp-blog-header.php' );
- // Get IDs of all products.
- $args = array('post_type'=>'product', 'posts_per_page' => -1, 'fields' => 'ids', 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false);
- $products_query = new WP_Query($args);
- $product_ids = array();
- $product_ids = $products_query->posts; // No loop because only requested IDs.
- wp_reset_postdata();
- // Only get 2 products because even that is timing out.
- $product_ids = array_slice( $product_ids, 0, 2 );
- //$product_ids = array_slice( $product_ids, 0, 1 );
- foreach ($product_ids as $id) {
- $product = wc_get_product( $id );
- echo "<p>Product name: ", $product->get_name(), "<br/>Type: ", $product->get_type(), "</p>";
- if ( 'variable' == $product->get_type( ) ) {
- $variation_attributes = $product->get_variation_attributes(); // Get titles of the variations.
- $attributes = array_keys( $variation_attributes );
- $available_variations = $product->get_available_variations();
- echo '<ul>';
- foreach ( array_keys( $available_variations ) as $index ) {
- if ( true == $available_variations[ $index ][ 'is_in_stock' ] ) {
- echo "<li><strong>", $variation_attributes[ $attributes[ 0 ] ][ $index ], "</strong> is in stock (", $available_variations[ $index ][ 'max_qty' ], " left).</li>";
- }
- }
- echo '</ul>';
- }
- }
- echo '<p>', get_num_queries (), ' SQL queries done.';
- echo '<br />Page generation took ', timer_stop(), ' seconds.', '</p>';
- ?>
- </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement