# tier-pricing-table/2.0.2/src/BackgroundProcessing/Updater/Updates/VersionTwoDotZero.php

Tiered Pricing Table for WooCommerce, version 2.0.2. 113 lines.

- Page: https://pluginprobe.com/plugins/tier-pricing-table/2.0.2/code/src/BackgroundProcessing/Updater/Updates/VersionTwoDotZero.php
- Raw: https://pluginprobe.com/plugins/tier-pricing-table/2.0.2/raw/src/BackgroundProcessing/Updater/Updates/VersionTwoDotZero.php
- Modified: 2019-03-18T16:29:42+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/tier-pricing-table/2.0.2/code/src/BackgroundProcessing/Updater/Updates/VersionTwoDotZero.php#L10-L20`.

```php
<?php namespace TierPricingTable\BackgroundProcessing\Updater\Updates;

/**
 * Class VersionTwoDotZero
 *
 * @package TierPricingTable\BackgroundProcessing\Updater\Updates
 */
class VersionTwoDotZero extends VersionAbstract {

	/**
	 * Update to version
	 *
	 * @var string
	 */
	public $version = '2.0';

	/**
	 * @var string
	 */
	protected $action = 'tiered_price_table_update_two_dot_zero';

	/**
	 * Main method to run updating. Version 2.0
	 */
	public function run() {
		add_action( 'init', function () {
			$this->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();
	}
}
```
