|
2 | 2 | {% assign exclusion_tags = options.exclude_products_with_any_of_these_tags__array %}
|
3 | 3 | {% assign only_process_paid_orders = options.only_process_paid_orders__boolean %}
|
4 | 4 | {% assign wait_until_any_other_shippable_items_are_fulfilled = options.wait_until_any_other_shippable_items_are_fulfilled__boolean %}
|
| 5 | +{% assign fulfill_gift_cards = options.fulfill_gift_cards__boolean %} |
5 | 6 |
|
6 | 7 | {% if event.topic contains "shopify/orders/" %}
|
7 | 8 | {% comment %}
|
8 |
| - -- get all open or in progress fulfillment orders |
| 9 | + -- get all open or in progress fulfillment orders for this order |
9 | 10 | {% endcomment %}
|
10 | 11 |
|
11 | 12 | {% capture query %}
|
|
32 | 33 | remainingQuantity
|
33 | 34 | requiresShipping
|
34 | 35 | variant {
|
| 36 | + id |
| 37 | + displayName |
35 | 38 | product {
|
| 39 | + id |
| 40 | + isGiftCard |
36 | 41 | tags
|
37 | 42 | }
|
38 | 43 | }
|
|
76 | 81 | }
|
77 | 82 | nodes {
|
78 | 83 | id
|
| 84 | + name |
79 | 85 | displayFinancialStatus
|
80 | 86 | displayFulfillmentStatus
|
81 | 87 | fulfillmentOrders(
|
|
95 | 101 | remainingQuantity
|
96 | 102 | requiresShipping
|
97 | 103 | variant {
|
| 104 | + id |
| 105 | + displayName |
98 | 106 | product {
|
| 107 | + id |
| 108 | + isGiftCard |
99 | 109 | tags
|
100 | 110 | }
|
101 | 111 | }
|
|
152 | 162 | "tags": {{ inclusion_tags.first | json }}
|
153 | 163 | }
|
154 | 164 | }
|
| 165 | + }, |
| 166 | + { |
| 167 | + "id": "gid://shopify/FulfillmentOrderLineItem/2345678901", |
| 168 | + "remainingQuantity": 1, |
| 169 | + "requiresShipping": false, |
| 170 | + "variant": { |
| 171 | + "product": { |
| 172 | + "isGiftCard": true |
| 173 | + } |
| 174 | + } |
155 | 175 | }
|
156 | 176 | ]
|
157 | 177 | }
|
|
186 | 206 |
|
187 | 207 | {% comment %}
|
188 | 208 | -- fulfillments can only be created for one location at a time, so need to group fulfillment orders by location
|
| 209 | + -- Shopify will also throw an error if gift cards are fulfilled with any other item types |
189 | 210 | {% endcomment %}
|
190 | 211 |
|
191 | 212 | {% assign fulfillment_orders_by_location = hash %}
|
|
197 | 218 |
|
198 | 219 | {% for fulfillment_order_line_item in fulfillment_order.lineItems.nodes %}
|
199 | 220 | {% comment %}
|
200 |
| - -- skip items that do not require shipping, but set flag if any are unfulfilled |
| 221 | + -- skip items that require shipping, but set flag if any are unfulfilled in case the option to wait on them is enabled |
201 | 222 | {% endcomment %}
|
202 | 223 |
|
203 | 224 | {% if fulfillment_order_line_item.requiresShipping %}
|
|
255 | 276 | {% endif %}
|
256 | 277 |
|
257 | 278 | {% comment %}
|
258 |
| - -- save unfulfilled line items that do not require shipping |
| 279 | + -- save unfulfilled line items that do not require shipping; gift cards must be grouped separately |
259 | 280 | {% endcomment %}
|
260 | 281 |
|
261 | 282 | {% if fulfillment_order_line_item.remainingQuantity > 0 %}
|
262 |
| - {% assign fulfillment_order_data["unfulfilled_line_items"] |
263 |
| - = fulfillment_order_data["unfulfilled_line_items"] |
264 |
| - | default: array |
265 |
| - | push: fulfillment_order_line_item |
266 |
| - %} |
| 283 | + {% if fulfillment_order_line_item.variant.product.isGiftCard %} |
| 284 | + {% if fulfill_gift_cards %} |
| 285 | + {% assign fulfillment_order_data["gift_cards_to_fulfill"] |
| 286 | + = fulfillment_order_data["gift_cards_to_fulfill"] |
| 287 | + | default: array |
| 288 | + | push: fulfillment_order_line_item |
| 289 | + %} |
| 290 | + {% endif %} |
| 291 | + |
| 292 | + {% else %} |
| 293 | + {% assign fulfillment_order_data["line_items_to_fulfill"] |
| 294 | + = fulfillment_order_data["line_items_to_fulfill"] |
| 295 | + | default: array |
| 296 | + | push: fulfillment_order_line_item |
| 297 | + %} |
| 298 | + {% endif %} |
267 | 299 | {% endif %}
|
268 | 300 | {% endfor %}
|
269 | 301 |
|
270 | 302 | {% comment %}
|
271 |
| - -- group unfulfilled line items by location for fulfillment |
| 303 | + -- group fulfillment orders by location |
272 | 304 | {% endcomment %}
|
273 | 305 |
|
274 |
| - {% if fulfillment_order_data.unfulfilled_line_items != blank %} |
| 306 | + {% if fulfillment_order_data.line_items_to_fulfill != blank or fulfillment_order_data.gift_cards_to_fulfill != blank %} |
275 | 307 | {% assign fulfillment_orders_by_location[fulfillment_order.assignedLocation.location.id]
|
276 | 308 | = fulfillment_orders_by_location[fulfillment_order.assignedLocation.location.id]
|
277 | 309 | | default: array
|
|
280 | 312 | {% endif %}
|
281 | 313 | {% endfor %}
|
282 | 314 |
|
283 |
| - {% if wait_until_any_other_shippable_items_are_fulfilled and has_unfulfilled_shippable_items and fulfillment_orders_by_location != blank %} |
| 315 | + {% if wait_until_any_other_shippable_items_are_fulfilled |
| 316 | + and has_unfulfilled_shippable_items |
| 317 | + and fulfillment_orders_by_location != blank |
| 318 | + %} |
284 | 319 | {% log
|
285 | 320 | message: "Unfulfilled shippable items exist on this order and the 'Wait until any other shippable items are fulfilled' option is checked; no auto fulfillments will be made in this task run.",
|
286 | 321 | order: order
|
|
289 | 324 | {% endif %}
|
290 | 325 |
|
291 | 326 | {% comment %}
|
292 |
| - -- fulfill the items that don't require shipping |
| 327 | + -- fulfill the line items that don't require shipping; fulfill any gift cards separately |
293 | 328 | {% endcomment %}
|
294 | 329 |
|
| 330 | + {% unless event.preview %} |
| 331 | + {% log |
| 332 | + order_name: order.name, |
| 333 | + fulfillment_orders_by_location: fulfillment_orders_by_location |
| 334 | + %} |
| 335 | + {% endunless %} |
| 336 | + |
295 | 337 | {% for keyval in fulfillment_orders_by_location %}
|
296 |
| - {% action "shopify" %} |
297 |
| - mutation { |
298 |
| - fulfillmentCreate( |
299 |
| - fulfillment: { |
300 |
| - lineItemsByFulfillmentOrder: [ |
301 |
| - {% for fulfillment_order_data in keyval[1] %} |
302 |
| - { |
303 |
| - fulfillmentOrderId: {{ fulfillment_order_data.fulfillment_order_id | json }} |
304 |
| - fulfillmentOrderLineItems: [ |
305 |
| - {% for unfulfilled_line_item in fulfillment_order_data.unfulfilled_line_items %} |
306 |
| - { |
307 |
| - id: {{ unfulfilled_line_item.id | json }} |
308 |
| - quantity: {{ unfulfilled_line_item.remainingQuantity }} |
309 |
| - } |
310 |
| - {% endfor %} |
311 |
| - ] |
312 |
| - } |
313 |
| - {% endfor %} |
314 |
| - ] |
315 |
| - notifyCustomer: false |
316 |
| - } |
317 |
| - ) { |
318 |
| - fulfillment { |
319 |
| - id |
320 |
| - status |
| 338 | + {% for fulfillment_order_data in keyval[1] %} |
| 339 | + {% if fulfillment_order_data.gift_cards_to_fulfill != blank %} |
| 340 | + {% action "shopify" %} |
| 341 | + mutation { |
| 342 | + fulfillmentCreate( |
| 343 | + fulfillment: { |
| 344 | + lineItemsByFulfillmentOrder: [ |
| 345 | + { |
| 346 | + fulfillmentOrderId: {{ fulfillment_order_data.fulfillment_order_id | json }} |
| 347 | + fulfillmentOrderLineItems: [ |
| 348 | + {% for line_item in fulfillment_order_data.gift_cards_to_fulfill %} |
| 349 | + { |
| 350 | + id: {{ line_item.id | json }} |
| 351 | + quantity: {{ line_item.remainingQuantity }} |
| 352 | + } |
| 353 | + {% endfor %} |
| 354 | + ] |
| 355 | + } |
| 356 | + ] |
| 357 | + notifyCustomer: false |
| 358 | + } |
| 359 | + ) { |
| 360 | + fulfillment { |
| 361 | + id |
| 362 | + status |
| 363 | + } |
| 364 | + userErrors { |
| 365 | + field |
| 366 | + message |
| 367 | + } |
| 368 | + } |
321 | 369 | }
|
322 |
| - userErrors { |
323 |
| - field |
324 |
| - message |
| 370 | + {% endaction %} |
| 371 | + {% endif %} |
| 372 | + |
| 373 | + {% if fulfillment_order_data.line_items_to_fulfill != blank %} |
| 374 | + {% action "shopify" %} |
| 375 | + mutation { |
| 376 | + fulfillmentCreate( |
| 377 | + fulfillment: { |
| 378 | + lineItemsByFulfillmentOrder: [ |
| 379 | + { |
| 380 | + fulfillmentOrderId: {{ fulfillment_order_data.fulfillment_order_id | json }} |
| 381 | + fulfillmentOrderLineItems: [ |
| 382 | + {% for line_item in fulfillment_order_data.line_items_to_fulfill %} |
| 383 | + { |
| 384 | + id: {{ line_item.id | json }} |
| 385 | + quantity: {{ line_item.remainingQuantity }} |
| 386 | + } |
| 387 | + {% endfor %} |
| 388 | + ] |
| 389 | + } |
| 390 | + ] |
| 391 | + notifyCustomer: false |
| 392 | + } |
| 393 | + ) { |
| 394 | + fulfillment { |
| 395 | + id |
| 396 | + status |
| 397 | + } |
| 398 | + userErrors { |
| 399 | + field |
| 400 | + message |
| 401 | + } |
| 402 | + } |
325 | 403 | }
|
326 |
| - } |
327 |
| - } |
328 |
| - {% endaction %} |
| 404 | + {% endaction %} |
| 405 | + {% endif %} |
| 406 | + {% endfor %} |
329 | 407 | {% endfor %}
|
330 | 408 | {% endfor %}
|
0 commit comments