Records
6 years ago
Catalog_Item.php
6 years ago
Interval_Polling.php
5 years ago
Job.php
6 years ago
Manual_Synchronization.php
5 years ago
Product_Import.php
5 years ago
Records.php
6 years ago
Stepped_Job.php
6 years ago
Interval_Polling.php
282 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Square |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@woocommerce.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * DISCLAIMER |
| 14 | * |
| 15 | * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer |
| 16 | * versions in the future. If you wish to customize WooCommerce Square for your |
| 17 | * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/ |
| 18 | * |
| 19 | * @author WooCommerce |
| 20 | * @copyright Copyright: (c) 2019, Automattic, Inc. |
| 21 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 |
| 22 | */ |
| 23 | |
| 24 | namespace WooCommerce\Square\Sync; |
| 25 | |
| 26 | use SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework; |
| 27 | use SquareConnect\Model\SearchCatalogObjectsResponse; |
| 28 | use SquareConnect\Model\BatchRetrieveInventoryCountsResponse; |
| 29 | use WooCommerce\Square\Handlers\Product; |
| 30 | use WooCommerce\Square\Handlers\Category; |
| 31 | |
| 32 | defined( 'ABSPATH' ) || exit; |
| 33 | |
| 34 | /** |
| 35 | * Class to represent a synchronization job to poll latest product updates at intervals. |
| 36 | * |
| 37 | * @since 2.0.0 |
| 38 | */ |
| 39 | class Interval_Polling extends Stepped_Job { |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * Assigns the next steps needed for this sync job. |
| 44 | * |
| 45 | * Adds the next steps to the 'next_steps' attribute. |
| 46 | * |
| 47 | * @since 2.0.0 |
| 48 | */ |
| 49 | protected function assign_next_steps() { |
| 50 | |
| 51 | $next_steps = array(); |
| 52 | |
| 53 | if ( $this->is_system_of_record_square() ) { |
| 54 | |
| 55 | $next_steps = array( |
| 56 | 'update_category_data', |
| 57 | 'update_product_data', |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | // only pull latest inventory if enabled |
| 62 | if ( wc_square()->get_settings_handler()->is_inventory_sync_enabled() ) { |
| 63 | $next_steps[] = 'update_inventory_counts'; |
| 64 | } |
| 65 | |
| 66 | $this->set_attr( 'next_steps', $next_steps ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Updates categories from Square. |
| 71 | * |
| 72 | * @since 2.0.8 |
| 73 | * |
| 74 | * @throws Framework\SV_WC_Plugin_Exception |
| 75 | */ |
| 76 | protected function update_category_data() { |
| 77 | $date = new \DateTime(); |
| 78 | $date->setTimestamp( $this->get_attr( 'catalog_last_synced_at', (int) wc_square()->get_sync_handler()->get_last_synced_at() ) ); |
| 79 | $date->setTimezone( new \DateTimeZone( 'UTC' ) ); |
| 80 | |
| 81 | $response = wc_square()->get_api()->search_catalog_objects( |
| 82 | array( |
| 83 | 'object_types' => array( 'CATEGORY' ), |
| 84 | 'begin_time' => $date->format( DATE_ATOM ), |
| 85 | ) |
| 86 | ); |
| 87 | |
| 88 | $categories = $response->get_data()->getObjects(); |
| 89 | |
| 90 | if ( $categories && is_array( $categories ) ) { |
| 91 | foreach ( $categories as $category ) { |
| 92 | Category::import_or_update( $category ); |
| 93 | } |
| 94 | |
| 95 | Records::set_record( |
| 96 | array( |
| 97 | 'type' => 'info', |
| 98 | 'message' => sprintf( |
| 99 | /* translator: Placeholder %d number of categories */ |
| 100 | _n( 'Updated data for %d category.', 'Updated data for %d categories.', count( $categories ), 'woocommerce-square' ), |
| 101 | count( $categories ) |
| 102 | ), |
| 103 | ) |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | $this->complete_step( 'update_category_data' ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Updates products from Square. |
| 112 | * |
| 113 | * @since 2.0.0 |
| 114 | * |
| 115 | * @throws Framework\SV_WC_Plugin_Exception |
| 116 | */ |
| 117 | protected function update_product_data() { |
| 118 | |
| 119 | $date = new \DateTime(); |
| 120 | $date->setTimestamp( $this->get_attr( 'catalog_last_synced_at', (int) wc_square()->get_sync_handler()->get_last_synced_at() ) ); |
| 121 | $date->setTimezone( new \DateTimeZone( 'UTC' ) ); |
| 122 | |
| 123 | $products_updated = $this->get_attr( 'processed_product_ids', array() ); |
| 124 | $cursor = $this->get_attr( 'update_product_data_cursor' ); |
| 125 | |
| 126 | $response = wc_square()->get_api()->search_catalog_objects( |
| 127 | array( |
| 128 | 'object_types' => array( 'ITEM' ), |
| 129 | 'include_deleted_objects' => true, |
| 130 | 'begin_time' => $date->format( DATE_ATOM ), |
| 131 | 'cursor' => $cursor, |
| 132 | ) |
| 133 | ); |
| 134 | |
| 135 | // store the timestamp after this API request was completed |
| 136 | // we don't want to set it at the end, as counts may have changed in the time it takes to process the data |
| 137 | if ( ! $cursor ) { |
| 138 | wc_square()->get_sync_handler()->set_last_synced_at(); |
| 139 | } |
| 140 | |
| 141 | if ( $response->get_data() instanceof SearchCatalogObjectsResponse && is_array( $response->get_data()->getObjects() ) ) { |
| 142 | |
| 143 | foreach ( $response->get_data()->getObjects() as $object ) { |
| 144 | |
| 145 | // filter out objects that aren't at our configured location |
| 146 | if ( ! $object->getPresentAtAllLocations() && ( ! is_array( $object->getPresentAtLocationIds() ) || ! in_array( wc_square()->get_settings_handler()->get_location_id(), $object->getPresentAtLocationIds(), true ) ) ) { |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | $product = Product::get_product_by_square_id( $object->getId() ); |
| 151 | |
| 152 | if ( $product instanceof \WC_Product ) { |
| 153 | |
| 154 | // deleted items won't have any data to set, so don't try and update the product |
| 155 | if ( $object->getIsDeleted() ) { |
| 156 | |
| 157 | $record = array( |
| 158 | 'type' => 'alert', |
| 159 | 'product_id' => $product->get_id(), |
| 160 | ); |
| 161 | |
| 162 | // if enabled, hide the product from the catalog |
| 163 | if ( wc_square()->get_settings_handler()->hide_missing_square_products() ) { |
| 164 | |
| 165 | try { |
| 166 | |
| 167 | $product->set_catalog_visibility( 'hidden' ); |
| 168 | $product->save(); |
| 169 | |
| 170 | $record['product_hidden'] = true; |
| 171 | |
| 172 | } catch ( \Exception $e ) { |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | Records::set_record( $record ); |
| 177 | |
| 178 | } else { |
| 179 | |
| 180 | try { |
| 181 | |
| 182 | Product::update_from_square( $product, $object->getItemData(), false ); |
| 183 | |
| 184 | if ( ! $product->get_image_id() && $object->getImageId() ) { |
| 185 | Product::update_image_from_square( $product, $object->getImageId() ); |
| 186 | } |
| 187 | |
| 188 | $products_updated[] = $product->get_id(); |
| 189 | |
| 190 | } catch ( \Exception $exception ) { |
| 191 | |
| 192 | Records::set_record( |
| 193 | array( |
| 194 | 'type' => 'alert', |
| 195 | 'product_id' => $product->get_id(), |
| 196 | ) |
| 197 | ); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | $cursor = $response->get_data() instanceof SearchCatalogObjectsResponse ? $response->get_data()->getCursor() : null; |
| 205 | |
| 206 | $this->set_attr( 'update_product_data_cursor', $cursor ); |
| 207 | $this->set_attr( 'processed_product_ids', array_unique( $products_updated ) ); |
| 208 | |
| 209 | if ( ! $cursor ) { |
| 210 | $this->complete_step( 'update_product_data' ); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /** |
| 216 | * Updates the inventory counts from the latest in Square. |
| 217 | * |
| 218 | * Helper method, do not open to public. |
| 219 | * |
| 220 | * @since 2.0.0 |
| 221 | * |
| 222 | * @throws Framework\SV_WC_API_Exception |
| 223 | */ |
| 224 | protected function update_inventory_counts() { |
| 225 | |
| 226 | $products_updated = $this->get_attr( 'processed_product_ids' ); |
| 227 | $cursor = $this->get_attr( 'update_inventory_counts_cursor' ); |
| 228 | |
| 229 | $args = array( |
| 230 | 'location_ids' => array( wc_square()->get_settings_handler()->get_location_id() ), |
| 231 | 'cursor' => $cursor, |
| 232 | ); |
| 233 | |
| 234 | $last_synced_at = $this->get_attr( 'inventory_last_synced_at' ); |
| 235 | |
| 236 | if ( $last_synced_at ) { |
| 237 | |
| 238 | $date = new \DateTime(); |
| 239 | $date->setTimestamp( $last_synced_at ); |
| 240 | $date->setTimezone( new \DateTimeZone( 'UTC' ) ); |
| 241 | |
| 242 | $args['updated_after'] = $date->format( DATE_ATOM ); |
| 243 | } |
| 244 | |
| 245 | $response = wc_square()->get_api()->batch_retrieve_inventory_counts( $args ); |
| 246 | $cursor = $response->get_data() instanceof BatchRetrieveInventoryCountsResponse ? $response->get_data()->getCursor() : null; |
| 247 | |
| 248 | // store the start timestamp after the first API request was completed but do not save it now |
| 249 | // if cursor is present, then it is not the last page. So, use the inventory_last_synced_at time |
| 250 | // else use the current time |
| 251 | $last_sync_timestamp = $cursor ? $last_synced_at : current_time( 'timestamp', true ); |
| 252 | |
| 253 | foreach ( $response->get_counts() as $count ) { |
| 254 | |
| 255 | // Square can return multiple "types" of counts, WooCommerce only distinguishes whether a product is in stock or not |
| 256 | if ( 'IN_STOCK' === $count->getState() ) { |
| 257 | |
| 258 | $product = Product::get_product_by_square_variation_id( $count->getCatalogObjectId() ); |
| 259 | |
| 260 | if ( $product instanceof \WC_Product ) { |
| 261 | |
| 262 | $product->set_stock_quantity( $count->getQuantity() ); |
| 263 | $product->save(); |
| 264 | |
| 265 | $products_updated[] = $product->get_id(); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | $this->set_attr( 'update_inventory_counts_cursor', $cursor ); |
| 271 | $this->set_attr( 'processed_product_ids', array_unique( $products_updated ) ); |
| 272 | |
| 273 | if ( ! $cursor ) { |
| 274 | // When all the inventory counts are synced then set the last sync time to the start time that was stored |
| 275 | wc_square()->get_sync_handler()->set_inventory_last_synced_at( $last_sync_timestamp ); |
| 276 | $this->complete_step( 'update_inventory_counts' ); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | |
| 281 | } |
| 282 |