Records
1 year ago
Catalog_Item.php
8 months ago
Helper.php
1 year ago
Interval_Polling.php
6 months ago
Job.php
2 years ago
Manual_Synchronization.php
6 months ago
Order_Importer.php
10 months ago
Order_Mapper.php
10 months ago
Order_Polling.php
10 months ago
Product_Import.php
6 months ago
Records.php
2 years ago
Stepped_Job.php
2 years ago
Helper.php
151 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 GNU General Public License v3.0 or later |
| 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 or later |
| 22 | */ |
| 23 | |
| 24 | namespace WooCommerce\Square\Sync; |
| 25 | |
| 26 | use Square\Models\BatchRetrieveCatalogObjectsResponse; |
| 27 | use Square\Models\BatchRetrieveInventoryCountsResponse; |
| 28 | |
| 29 | defined( 'ABSPATH' ) || exit; |
| 30 | |
| 31 | /** |
| 32 | * Square Sync Helper Class |
| 33 | * |
| 34 | * The purpose of this class is to centralize common sync utility functions. |
| 35 | * |
| 36 | * @since 3.8.2 |
| 37 | */ |
| 38 | class Helper { |
| 39 | |
| 40 | /** |
| 41 | * Get the inventory tracking value for the given catalog object ids. |
| 42 | * |
| 43 | * @param array $catalog_object_ids The catalog object ids. |
| 44 | * @return array Array of inventory tracking for given catalog object ids. |
| 45 | */ |
| 46 | public static function get_catalog_objects_inventory_stats( $catalog_object_ids ) { |
| 47 | if ( empty( $catalog_object_ids ) ) { |
| 48 | return array(); |
| 49 | } |
| 50 | |
| 51 | $response = wc_square()->get_api()->batch_retrieve_inventory_counts( |
| 52 | array( |
| 53 | 'catalog_object_ids' => $catalog_object_ids, |
| 54 | 'location_ids' => array( wc_square()->get_settings_handler()->get_location_id() ), |
| 55 | 'states' => array( 'IN_STOCK' ), // Get only in stock counts. |
| 56 | ) |
| 57 | ); |
| 58 | |
| 59 | if ( ! $response->get_data() instanceof BatchRetrieveInventoryCountsResponse ) { |
| 60 | throw new \Exception( 'Response data missing or invalid' ); |
| 61 | } |
| 62 | |
| 63 | $inventory_hash = array(); |
| 64 | foreach ( $response->get_counts() as $inventory_count ) { |
| 65 | $inventory_hash[ $inventory_count->getCatalogObjectId() ] = $inventory_count->getQuantity(); |
| 66 | } |
| 67 | |
| 68 | return $inventory_hash; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get the inventory tracking value for the given catalog object ids. |
| 73 | * |
| 74 | * @param array $catalog_object_ids The catalog object ids. |
| 75 | * @return array Array of inventory tracking for given catalog object ids. |
| 76 | */ |
| 77 | public static function get_catalog_objects_tracking_stats( $catalog_object_ids ) { |
| 78 | if ( empty( $catalog_object_ids ) ) { |
| 79 | return array(); |
| 80 | } |
| 81 | |
| 82 | $catalog_response = wc_square()->get_api()->batch_retrieve_catalog_objects( $catalog_object_ids ); |
| 83 | if ( ! $catalog_response->get_data() instanceof BatchRetrieveCatalogObjectsResponse ) { |
| 84 | throw new \Exception( 'Response data is missing' ); |
| 85 | } |
| 86 | |
| 87 | $objects = $catalog_response->get_data()->getObjects() ? $catalog_response->get_data()->getObjects() : array(); |
| 88 | |
| 89 | return self::get_catalog_inventory_tracking( $objects ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get the inventory tracking value for the given catalog objects. |
| 94 | * |
| 95 | * @param \Square\Models\CatalogObject[] $catalog_objects The catalog objects. |
| 96 | * @return array Array of inventory tracking for given catalog objects. |
| 97 | */ |
| 98 | public static function get_catalog_inventory_tracking( $catalog_objects ) { |
| 99 | $catalog_objects_tracking = array(); |
| 100 | |
| 101 | /** @var \Square\Models\CatalogObject $catalog_object */ |
| 102 | foreach ( $catalog_objects as $catalog_object ) { |
| 103 | $variation_data = $catalog_object->getItemVariationData(); |
| 104 | $location_overrides = $variation_data->getLocationOverrides(); |
| 105 | $configured_location = wc_square()->get_settings_handler()->get_location_id(); |
| 106 | |
| 107 | $default_data = array( |
| 108 | 'track_inventory' => $variation_data->getTrackInventory(), |
| 109 | 'sold_out' => false, |
| 110 | ); |
| 111 | |
| 112 | if ( ! empty( $location_overrides ) ) { |
| 113 | $location_ids = array_map( |
| 114 | function ( $location_override ) { |
| 115 | return $location_override->getLocationId(); |
| 116 | }, |
| 117 | $location_overrides |
| 118 | ); |
| 119 | |
| 120 | if ( ! in_array( $configured_location, $location_ids, true ) ) { |
| 121 | $catalog_objects_tracking[ $catalog_object->getId() ] = $default_data; |
| 122 | continue; |
| 123 | } |
| 124 | |
| 125 | foreach ( $location_overrides as $location_override ) { |
| 126 | $location_id = $location_override->getLocationId(); |
| 127 | |
| 128 | if ( $configured_location === $location_id ) { |
| 129 | $sold_out = $location_override->getSoldOut() ?? false; |
| 130 | if ( ! is_null( $location_override->getTrackInventory() ) ) { |
| 131 | $catalog_objects_tracking[ $catalog_object->getId() ] = array( |
| 132 | 'track_inventory' => $location_override->getTrackInventory(), |
| 133 | 'sold_out' => $sold_out, |
| 134 | ); |
| 135 | } else { |
| 136 | $catalog_objects_tracking[ $catalog_object->getId() ] = array( |
| 137 | 'track_inventory' => $variation_data->getTrackInventory(), |
| 138 | 'sold_out' => $sold_out, |
| 139 | ); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } else { |
| 144 | $catalog_objects_tracking[ $catalog_object->getId() ] = $default_data; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return $catalog_objects_tracking; |
| 149 | } |
| 150 | } |
| 151 |