PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 5.3.2
WooCommerce Square v5.3.2
5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Sync / Helper.php
woocommerce-square / includes / Sync Last commit date
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