PluginProbe
Tiered Pricing Table for WooCommerce / 2.0.2
Tiered Pricing Table for WooCommerce v2.0.2
7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 All 90 releases
tier-pricing-table / src / BackgroundProcessing / Updater / Updates / VersionTwoDotZero.php

VersionTwoDotZero.php in Tiered Pricing Table for WooCommerce 2.0.2, at src/BackgroundProcessing/Updater/Updates/VersionTwoDotZero.php

113 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\BackgroundProcessing\Updater\Updates;
2
3 /**
4 * Class VersionTwoDotZero
5 *
6 * @package TierPricingTable\BackgroundProcessing\Updater\Updates
7 */
8 class VersionTwoDotZero extends VersionAbstract {
9
10 /**
11 * Update to version
12 *
13 * @var string
14 */
15 public $version = '2.0';
16
17 /**
18 * @var string
19 */
20 protected $action = 'tiered_price_table_update_two_dot_zero';
21
22 /**
23 * Main method to run updating. Version 2.0
24 */
25 public function run() {
26 add_action( 'init', function () {
27 $this->begin();
28 $this->showUpdatingMessage();
29 } );
30 }
31
32 /**
33 * @param mixed $item
34 *
35 * @return bool|mixed
36 */
37 protected function task( $item ) {
38 $postsToUpdate = $this->getProductsToUpdate( 10 );
39
40 if ( empty( $postsToUpdate ) ) {
41 $this->complete();
42
43 return false;
44 }
45
46
47 // Single post
48 if ( $postsToUpdate instanceof \WP_Post ) {
49 $this->updatePost( $postsToUpdate->ID );
50 }
51
52 foreach ( $postsToUpdate as $postId ) {
53 $this->updatePost( $postId );
54 }
55
56 return $item;
57 }
58
59 /**
60 * @param int $postId
61 */
62 protected function updatePost( $postId ) {
63
64 $priceRules = get_post_meta( $postId, '_price_rules', true );
65
66 if ( ! empty( $priceRules ) ) {
67 update_post_meta( $postId, '_fixed_price_rules', $priceRules );
68 }
69
70 update_post_meta( $postId, '_price_rules_updated', 'yes' );
71 }
72
73 /**
74 * Complete updating. Delete old meta values.
75 */
76 protected function complete() {
77 delete_post_meta_by_key( '_price_rules_updated' );
78 delete_post_meta_by_key( '_price_rules' );
79
80 parent::complete();
81 }
82
83 /**
84 * Get list of products to update.
85 *
86 * @param $count
87 *
88 * @return array
89 */
90 protected function getProductsToUpdate( $count ) {
91 $args = array(
92 'post_type' => ['product', 'product_variation'],
93 'posts_per_page' => $count,
94
95 'meta_query' => array(
96 'relation' => 'AND',
97 array(
98 'key' => '_price_rules',
99 'compare' => 'EXISTS',
100 ),
101 array(
102 'key' => '_price_rules_updated',
103 'compare' => 'NOT EXISTS',
104 )
105 ),
106 'fields' => 'ids'
107 );
108
109 $query = new \WP_Query( $args );
110
111 return $query->get_posts();
112 }
113 }