Catalog.php
4 months ago
Customers.php
3 years ago
Inventory.php
3 years ago
Locations.php
3 years ago
Catalog.php
303 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\API\Requests; |
| 25 | |
| 26 | use WooCommerce\Square\API\Request; |
| 27 | |
| 28 | defined( 'ABSPATH' ) || exit; |
| 29 | |
| 30 | /** |
| 31 | * WooCommerce Square Catalog API Request class. |
| 32 | * |
| 33 | * @since 2.0.0 |
| 34 | */ |
| 35 | class Catalog extends Request { |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * Initializes a new Catalog request. |
| 40 | * |
| 41 | * @since 2.0.0 |
| 42 | * |
| 43 | * @param \Square\SquareClient $api_client the API client |
| 44 | */ |
| 45 | public function __construct( $api_client ) { |
| 46 | $this->square_api = $api_client->getCatalogApi(); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * Sets the data for a batchDeleteCatalogObjects request. |
| 52 | * |
| 53 | * @see https://docs.connect.squareup.com/api/connect/v2#endpoint-catalog-batchdeletecatalogobjects |
| 54 | * @see \Square\Apis\CatalogApi::batchDeleteCatalogObjects() |
| 55 | * |
| 56 | * @since 2.0.0 |
| 57 | * |
| 58 | * @param string[] $object_ids the square catalog object IDs to delete |
| 59 | */ |
| 60 | public function set_batch_delete_catalog_objects_data( array $object_ids ) { |
| 61 | |
| 62 | $this->square_api_method = 'batchDeleteCatalogObjects'; |
| 63 | $body = new \Square\Models\BatchDeleteCatalogObjectsRequest(); |
| 64 | $body->setObjectIds( $object_ids ); |
| 65 | $this->square_api_args = array( $body ); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * Sets the data for a batchRetrieveCatalogObjects request. |
| 71 | * |
| 72 | * @see https://docs.connect.squareup.com/api/connect/v2#endpoint-catalog-batchretrievecatalogobjects |
| 73 | * @see \Square\Apis\CatalogApi::batchRetrieveCatalogObjects() |
| 74 | * |
| 75 | * @since 2.0.0 |
| 76 | * |
| 77 | * @param string[] $object_ids the square catalog object IDs to delete |
| 78 | * @param bool $include_related_objects whether or not to include related objects in the response |
| 79 | */ |
| 80 | public function set_batch_retrieve_catalog_objects_data( array $object_ids, $include_related_objects = false ) { |
| 81 | |
| 82 | $this->square_api_method = 'batchRetrieveCatalogObjects'; |
| 83 | $body = new \Square\Models\BatchRetrieveCatalogObjectsRequest( $object_ids ); |
| 84 | $body->setIncludeRelatedObjects( (bool) $include_related_objects ); |
| 85 | |
| 86 | $this->square_api_args = array( $body ); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * Sets the data for a batchUpsertCatalogObjects request. |
| 92 | * |
| 93 | * @see https://docs.connect.squareup.com/api/connect/v2#endpoint-catalog-batchupsertcatalogobjects |
| 94 | * @see \Square\Apis\CatalogApi::batchUpsertCatalogObjects() |
| 95 | * |
| 96 | * @since 2.0.0 |
| 97 | * |
| 98 | * @param string $idempotency_key the UUID for this request |
| 99 | * @param \Square\Models\CatalogObjectBatch[] $batches array of catalog object batches |
| 100 | */ |
| 101 | public function set_batch_upsert_catalog_objects_data( $idempotency_key, array $batches ) { |
| 102 | |
| 103 | $this->square_api_method = 'batchUpsertCatalogObjects'; |
| 104 | $this->square_api_args = array( |
| 105 | new \Square\Models\BatchUpsertCatalogObjectsRequest( |
| 106 | $idempotency_key, |
| 107 | $batches |
| 108 | ), |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * Sets the data for a catalogInfo request. |
| 115 | * |
| 116 | * @see https://docs.connect.squareup.com/api/connect/v2#endpoint-catalog-cataloginfo |
| 117 | * @see \Square\Apis\CatalogApi::catalogInfo() |
| 118 | * |
| 119 | * @since 2.0.0 |
| 120 | * |
| 121 | * @param string $cursor |
| 122 | * @param array $types |
| 123 | */ |
| 124 | public function set_catalog_info_data( $cursor = '', $types = array() ) { |
| 125 | |
| 126 | $this->square_api_method = 'catalogInfo'; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /** |
| 131 | * Sets the data for a deleteCatalogObject request. |
| 132 | * |
| 133 | * @see https://docs.connect.squareup.com/api/connect/v2#endpoint-catalog-deletecatalogobject |
| 134 | * @see \Square\Apis\CatalogApi::deleteCatalogObject() |
| 135 | * |
| 136 | * @since 2.0.0 |
| 137 | * |
| 138 | * @param string $object_id the Square catalog object ID to delete |
| 139 | */ |
| 140 | public function set_delete_catalog_object_data( $object_id ) { |
| 141 | |
| 142 | $this->square_api_method = 'deleteCatalogObject'; |
| 143 | $this->square_api_args = array( $object_id ); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * Sets the data for a listCatalog request. |
| 149 | * |
| 150 | * @see https://docs.connect.squareup.com/api/connect/v2#endpoint-catalog-listcatalog |
| 151 | * @see \Square\Apis\CatalogApi::listCatalog() |
| 152 | * |
| 153 | * @since 2.0.0 |
| 154 | * |
| 155 | * @param string $cursor (optional) the pagination cursor |
| 156 | * @param array $types (optional) the catalog item types to filter by |
| 157 | */ |
| 158 | public function set_list_catalog_data( $cursor = '', $types = array() ) { |
| 159 | |
| 160 | $this->square_api_method = 'listCatalog'; |
| 161 | $this->square_api_args = array( |
| 162 | 'cursor' => $cursor, |
| 163 | 'types' => is_array( $types ) ? implode( ',', array_map( 'strtoupper', $types ) ) : '', |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /** |
| 169 | * Sets the data for a retrieveCatalogObject request. |
| 170 | * |
| 171 | * @see https://docs.connect.squareup.com/api/connect/v2#endpoint-catalog-retrievecatalogobject |
| 172 | * @see \Square\Apis\CatalogApi::retrieveCatalogObject() |
| 173 | * |
| 174 | * @since 2.0.0 |
| 175 | * |
| 176 | * @param string $object_id the Square catalog object ID to retrieve |
| 177 | * @param bool $include_related_objects Whether or not to include related objects (such as categories) |
| 178 | * @param int|null $object_version The specific version of the object to retrieve. Optional - defaults to latest. |
| 179 | * If the specified version of the object does not exist, the Square API will |
| 180 | * return the latest version. If the version provided is not known to exist consumers |
| 181 | * of this function should validate the version returned by the API. |
| 182 | */ |
| 183 | public function set_retrieve_catalog_object_data( $object_id, $include_related_objects = false, $object_version = null ) { |
| 184 | |
| 185 | $this->square_api_method = 'retrieveCatalogObject'; |
| 186 | $this->square_api_args = array( $object_id, (bool) $include_related_objects, $object_version ); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | /** |
| 191 | * Sets the data for a searchCatalogObjects request. |
| 192 | * |
| 193 | * @see https://docs.connect.squareup.com/api/connect/v2#endpoint-catalog-searchcatalogobjects |
| 194 | * @see \Square\Apis\CatalogApi::searchCatalogObjects() |
| 195 | * |
| 196 | * @since 2.0.0 |
| 197 | * |
| 198 | * @param array $args see Square documentation for full list of args allowed |
| 199 | */ |
| 200 | public function set_search_catalog_objects_data( array $args = array() ) { |
| 201 | |
| 202 | // convert object types to array |
| 203 | if ( isset( $args['object_types'] ) && ! is_array( $args['object_types'] ) ) { |
| 204 | $args['object_types'] = array( $args['object_types'] ); |
| 205 | } |
| 206 | |
| 207 | $defaults = array( |
| 208 | 'cursor' => null, |
| 209 | 'object_types' => null, |
| 210 | 'include_deleted_objects' => null, |
| 211 | 'include_related_objects' => null, |
| 212 | 'begin_time' => null, |
| 213 | 'query' => null, |
| 214 | 'limit' => null, |
| 215 | ); |
| 216 | |
| 217 | // apply defaults and remove any keys that aren't recognized |
| 218 | $args = array_intersect_key( wp_parse_args( $args, $defaults ), $defaults ); |
| 219 | |
| 220 | $body = new \Square\Models\SearchCatalogObjectsRequest(); |
| 221 | $body->setCursor( $args['cursor'] ); |
| 222 | $body->setObjectTypes( $args['object_types'] ); |
| 223 | $body->setIncludeDeletedObjects( $args['include_deleted_objects'] ); |
| 224 | $body->setIncludeRelatedObjects( $args['include_related_objects'] ); |
| 225 | $body->setBeginTime( $args['begin_time'] ); |
| 226 | $body->setQuery( $args['query'] ); |
| 227 | $body->setLimit( $args['limit'] ); |
| 228 | |
| 229 | $this->square_api_method = 'searchCatalogObjects'; |
| 230 | $this->square_api_args = array( $body ); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | /** |
| 235 | * Sets the data for a updateItemModifierLists request. |
| 236 | * |
| 237 | * @see https://docs.connect.squareup.com/api/connect/v2#endpoint-catalog-updateitemmodifierlists |
| 238 | * @see \Square\Apis\CatalogApi::updateItemModifierLists() |
| 239 | * |
| 240 | * @since 2.0.0 |
| 241 | * |
| 242 | * @param string[] $item_ids array of item IDs to update |
| 243 | * @param string[] $modifier_lists_to_enable array of list IDs to enable |
| 244 | * @param string[] $modifier_lists_to_disable array of list IDs to disable |
| 245 | */ |
| 246 | public function set_update_item_modifier_lists_data( array $item_ids, array $modifier_lists_to_enable = array(), array $modifier_lists_to_disable = array() ) { |
| 247 | |
| 248 | $this->square_api_method = 'updateItemModifierLists'; |
| 249 | $body = new \Square\Models\UpdateItemModifierListsRequest( $item_ids ); |
| 250 | $body->setModifierListsToEnable( $modifier_lists_to_enable ); |
| 251 | $body->setModifierListsToDisable( $modifier_lists_to_disable ); |
| 252 | |
| 253 | $this->square_api_args = array( $body ); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | /** |
| 258 | * Sets the data for an updateItemTaxes request. |
| 259 | * |
| 260 | * @see https://docs.connect.squareup.com/api/connect/v2#endpoint-catalog-updateitemtaxes |
| 261 | * @see \Square\Apis\CatalogApi::updateItemTaxes() |
| 262 | * |
| 263 | * @since 2.0.0 |
| 264 | * |
| 265 | * @param string[] $item_ids array of item IDs to update |
| 266 | * @param string[] $taxes_to_enable array of catalog tax IDs to enable |
| 267 | * @param string[] $taxes_to_disable array of catalog tax IDs to disable |
| 268 | */ |
| 269 | public function set_update_item_taxes_data( array $item_ids, array $taxes_to_enable = array(), array $taxes_to_disable = array() ) { |
| 270 | |
| 271 | $this->square_api_method = 'updateItemTaxes'; |
| 272 | $body = new \Square\Models\UpdateItemTaxesRequest( $item_ids ); |
| 273 | $body->setTaxesToEnable( $taxes_to_enable ); |
| 274 | $body->setTaxesToDisable( $taxes_to_disable ); |
| 275 | $this->square_api_args = array( $body ); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | /** |
| 280 | * Sets the data for an upsertCatalogObject request. |
| 281 | * |
| 282 | * @see https://docs.connect.squareup.com/api/connect/v2#endpoint-catalog-upsertcatalogobject |
| 283 | * @see \Square\Apis\CatalogApi::upsertCatalogObject() |
| 284 | * |
| 285 | * @since 2.0.0 |
| 286 | * |
| 287 | * @param string $idempotency_key a UUID for this request |
| 288 | * @param \Square\Models\CatalogObject $object the object to update |
| 289 | */ |
| 290 | public function set_upsert_catalog_object_data( $idempotency_key, $object ) { |
| 291 | |
| 292 | $this->square_api_method = 'upsertCatalogObject'; |
| 293 | $this->square_api_args = array( |
| 294 | new \Square\Models\UpsertCatalogObjectRequest( |
| 295 | $idempotency_key, |
| 296 | $object |
| 297 | ), |
| 298 | ); |
| 299 | } |
| 300 | |
| 301 | |
| 302 | } |
| 303 |