nozzlegear / ShopifySharp

ShopifySharp is a .NET library that helps developers easily authenticate with and manage Shopify stores.
https://nozzlegear.com/shopify-development-handbook
MIT License
742 stars 309 forks source link

FulfillmentService function not working after update to latest version ! #948

Open BasitBulbulia opened 10 months ago

BasitBulbulia commented 10 months ago

This is my code !


'' <summary>
'' Set an existing Shopify Order to be complete
'' </summary>
'' <param name="orderId">The id of the Order to complete</param>
'' <returns>True if successful</returns>
Public Async Function SetOrderAsComplete(orderId As Long) As Threading.Tasks.Task(Of Boolean)
    Try
        Dim locationService = New LocationService(urlFromUser, shopAccessToken)
        'set the RetryExecutionPolicy to handle the API limits
        locationService.SetExecutionPolicy(New LeakyBucketExecutionPolicy())
        Dim locations = Await locationService.ListAsync()

        Dim fulfillmentService = New FulfillmentService(urlFromUser, shopAccessToken)
        'set the RetryExecutionPolicy to handle the API limits
        fulfillmentService.SetExecutionPolicy(New LeakyBucketExecutionPolicy())

        'update the orderId to be complete
        Dim result = Await fulfillmentService.CreateAsync(orderId, New ShopifySharp.Fulfillment With {.LocationId = locations(0).Id})

        Return True
    Catch ex As Exception
        If DoAllStockUpdate = False Then MsgBox("SetOrdersAsComplete--->" & ex.Message)
        Return False
    End Try
End Function

The error is :- ListResult(Of Location)' cannot be indexed because it has no default property.

The error location is @ locations(0).Id

I am totally lost as to how I could fix this code

error-ordercomplete

nozzlegear commented 10 months ago

Hey @BasitBulbulia! I think the problem is the LocationService.ListAsync does not return a list directly, instead it returns this ListResult<Location> object, so you can't do locations(0).Id.

You should be able to fix that error by using locations.Items(0).Id.

BasitBulbulia commented 10 months ago

I tried that this is the errors in IDE error-two

nozzlegear commented 10 months ago

Oh I see, which version were you using before @BasitBulbulia? It looks like you're using the older method for creating fulfillments that Shopify has deprecated and removed. They require apps to use a new "Fulfillment Order" based approach for fulfilling line items and entire orders, we're not allowed to create fulfillments with the old API anymore.

I don't know much VB unfortunately, so this may not be entirely correct. I think you want something like this:

Try
    Dim fulfillmentOrderService As New FulfillmentOrderService(domain, accessToken)
    Dim fulfillmentService As New FulfillmentService(domain, accessToken)

    Dim policy As New LeakyBucketExecutionPolicy()

    fulfillmentOrderService.SetExecutionPolicy(policy)
    fulfillmentService.SetExecutionPolicy(policy)

    ' Find open fulfillment orders for this order
    Dim openFulfillmentOrders = Await fulfillmentOrderService.ListAsync(orderId)
    openFulfillmentOrders = openFulfillmentOrders.Where(Function(f) f.Status = "open").ToList()

    ' Fulfill all line items across all open fulfillment orders in this Shopify order
    Dim lineItems = openFulfillmentOrders.Select(Function(o) New LineItemsByFulfillmentOrder With {
        .FulfillmentOrderId = o.Id.Value
        ' Optionally specify a list of line items if you're doing a partial fulfillment
        ' .FulfillmentRequestOrderLineItems = ...
    })

    ' Fulfill the line items
    Dim fulfillment = Await fulfillmentService.CreateAsync(New FulfillmentShipping With {
        .Message = "items are shipping!",
        ' Set to true to send an email to the customer that their items have shipped
        .NotifyCustomer = False,
        ' You can leave the tracking info out if you have no tracking info
        .TrackingInfo = New TrackingInfo With {
            .Company = "UPS",
            .Url = "...",
            .Number = "..."
        }
    })
Catch ex As Exception
    If DoAllStockUpdate = False Then MsgBox("SetOrdersAsComplete-->" & ex.Message)
    Return False
End Try

Check this thread here for more info on the Fulfillment Orders workflow: #828