get_api()->batch_retrieve_inventory_counts( array( 'catalog_object_ids' => $catalog_object_ids, 'location_ids' => array( wc_square()->get_settings_handler()->get_location_id() ), 'states' => array( 'IN_STOCK' ), // Get only in stock counts. ) ); if ( ! $response->get_data() instanceof BatchRetrieveInventoryCountsResponse ) { throw new \Exception( 'Response data missing or invalid' ); } $inventory_hash = array(); foreach ( $response->get_counts() as $inventory_count ) { $inventory_hash[ $inventory_count->getCatalogObjectId() ] = $inventory_count->getQuantity(); } return $inventory_hash; } /** * Get the inventory tracking value for the given catalog object ids. * * @param array $catalog_object_ids The catalog object ids. * @return array Array of inventory tracking for given catalog object ids. */ public static function get_catalog_objects_tracking_stats( $catalog_object_ids ) { if ( empty( $catalog_object_ids ) ) { return array(); } $catalog_response = wc_square()->get_api()->batch_retrieve_catalog_objects( $catalog_object_ids ); if ( ! $catalog_response->get_data() instanceof BatchRetrieveCatalogObjectsResponse ) { throw new \Exception( 'Response data is missing' ); } $objects = $catalog_response->get_data()->getObjects() ? $catalog_response->get_data()->getObjects() : array(); return self::get_catalog_inventory_tracking( $objects ); } /** * Get the inventory tracking value for the given catalog objects. * * @param \Square\Models\CatalogObject[] $catalog_objects The catalog objects. * @return array Array of inventory tracking for given catalog objects. */ public static function get_catalog_inventory_tracking( $catalog_objects ) { $catalog_objects_tracking = array(); /** @var \Square\Models\CatalogObject $catalog_object */ foreach ( $catalog_objects as $catalog_object ) { $variation_data = $catalog_object->getItemVariationData(); $location_overrides = $variation_data->getLocationOverrides(); $configured_location = wc_square()->get_settings_handler()->get_location_id(); if ( ! empty( $location_overrides ) ) { $location_ids = array_map( function ( $location_override ) { return $location_override->getLocationId(); }, $location_overrides ); if ( ! in_array( $configured_location, $location_ids, true ) ) { $catalog_objects_tracking[ $catalog_object->getId() ] = $variation_data->getTrackInventory(); continue; } foreach ( $location_overrides as $location_override ) { $location_id = $location_override->getLocationId(); if ( $configured_location === $location_id ) { if ( ! is_null( $location_override->getTrackInventory() ) ) { $catalog_objects_tracking[ $catalog_object->getId() ] = $location_override->getTrackInventory(); } else { $catalog_objects_tracking[ $catalog_object->getId() ] = $variation_data->getTrackInventory(); } } } } else { $catalog_objects_tracking[ $catalog_object->getId() ] = $variation_data->getTrackInventory(); } } return $catalog_objects_tracking; } }