begin(); $this->showUpdatingMessage(); } ); } /** * @param mixed $item * * @return bool|mixed */ protected function task( $item ) { $postsToUpdate = $this->getProductsToUpdate( 10 ); if ( empty( $postsToUpdate ) ) { $this->complete(); return false; } // Single post if ( $postsToUpdate instanceof \WP_Post ) { $this->updatePost( $postsToUpdate->ID ); } foreach ( $postsToUpdate as $postId ) { $this->updatePost( $postId ); } return $item; } /** * @param int $postId */ protected function updatePost( $postId ) { $priceRules = get_post_meta( $postId, '_price_rules', true ); if ( ! empty( $priceRules ) ) { update_post_meta( $postId, '_fixed_price_rules', $priceRules ); } update_post_meta( $postId, '_price_rules_updated', 'yes' ); } /** * Complete updating. Delete old meta values. */ protected function complete() { delete_post_meta_by_key( '_price_rules_updated' ); delete_post_meta_by_key( '_price_rules' ); parent::complete(); } /** * Get list of products to update. * * @param $count * * @return array */ protected function getProductsToUpdate( $count ) { $args = array( 'post_type' => ['product', 'product_variation'], 'posts_per_page' => $count, 'meta_query' => array( 'relation' => 'AND', array( 'key' => '_price_rules', 'compare' => 'EXISTS', ), array( 'key' => '_price_rules_updated', 'compare' => 'NOT EXISTS', ) ), 'fields' => 'ids' ); $query = new \WP_Query( $args ); return $query->get_posts(); } }