Set Shopify product quantity to zero using PowerShell and API

In this post, we take a look at how one can set a specific vendor’s products to zero. Make sure to update your apitoken, apikey, basestoreurl and vendor in the script below. When you run, the script will pull all the inventory from your store, then only filter out the specific vendor and set the quantity of those items to zero. This has only been tested with products with no variations.

## Set Quanty to zero by Vendor / Manufacturer
##
##
## 
$startTime = get-date 
#################################
## Set API Variables
#################################
$apiToken = "<your-api-token>"
$apiKey = "<your-key>"
$basestoreURL = "<your-store>.myshopify.com/"
$library = "2023-10"
$productURL = "products.json"
$vendor = "<your-vendor-to-target>"
########################################################################################################
## Section 1
## Gathering Product data from Shopify
#########################################################################################################

#################################
## API Base URL for Connection
#################################
$uri = "https://" + $basestoreURL + "admin/api/" + $library + "/" + $productURL

#################################
## API Header
#################################
$headers = @{
    "Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($apikey+":"+$apiToken))
    "Content-Type" = "application/json"
} 
#################################
## Set Variables to use
#################################
$tableData = $null
$productDataFromShopify = $null
$productDataFromShopify = @()
$pageCount = 1
$pagination = $null
#################################
## Run through each page
## To pull 50 items 
## from each page
#################################
do {
    $results = Invoke-WebRequest -Method Get -Uri $uri -Header $headers -UseBasicParsing
    Write-host "Working on page:" $pageCount
    $pageCount++
    #############################
    ## Check Returned Status Code
    $statusCode = ($results).StatusCode
    if($statusCode -notLike "200"){
        Write-host "Error Code" $statusCode -ForegroundColor Red
        Pause
    }
    else{
        Write-host "Returned code" $statusCode "Continue" -ForegroundColor Green
    }
    #########################################################################
    ## Returned Header Call Limit Check Only available when Limit is not used
    $returnLimit = (($results).Headers).HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT
    write-Host "API Limit:" $returnLimit -ForegroundColor Red
    if($returnLimit -like "40/40"){
        write-host "Limit reached. Pausing for 5 Seconds"
        Start-Sleep -Seconds 5
    }
    $tableData = $null
    $tableData = $results.Content | Convertfrom-Json
    $tableData = $tableData.products
    foreach($dd in $tableData){
        $id = $dd.id
        $title = $dd.title 
        $vendor = $dd.vendor
        $productID = $dd.variants.product_id
        $price = $dd.variants.price
        $sku = $dd.variants.sku
        $barcode = $dd.variants.barcode
        $variantInventoryItemID = $dd.variants.inventory_item_id
        $variantInventoryQuantity = $dd.variants.inventory_quantity
        
        ###################
        ## Build Data Table
        $productDataFromShopify += [pscustomobject] @{
            'ID' = $id
            'Title' = $title
            'Vendor' = $vendor
            'Product ID' = $productID
            'Price' = $price
            'Sku' = $sku
            'Barcode' = $barcode              
            'Variant Inventory Item ID' = $variantInventoryItemID
            'Variant Inventory Quantity' = $variantInventoryQuantity
        }
    }

    ## Get next page link
    $pagination = ($results).headers.link
    $nextPage = $results.Headers.link
    $nextPage = ($nextPage).split(",") | Select-Object -last 1
    $nextPage = ($nextPage).replace("<","")
    $nextPage = ($nextPage).replace('>; rel="next"',"")
    $uri = $nextPage
} while (
    $pagination -like '*rel="next"*'
)

########################################################################################################
## Section 2
## Check data from Section and then update back to Shopity
#########################################################################################################

###################################
## Set Variables
###################################
$inventoryItemsURL = "inventory_levels/set.json"
$locationID = "<your-location-id>"
###################################
## Base URL reset
###################################
$uri = "https://" + $basestoreURL + "admin/api/" + $library + "/" + $inventoryItemsURL


## Import New QTY to be updated in Shopify
$newQtyData = $productDataFromShopify | Where-Object {$_.vendor -like "$vendor"}
$i = 1
$numberToUpdate = ($newQtyData).count
foreach($newQty in $newQtyData){
    $productName = $null
    $productName = $newQty.Title
    $qty = "0"
    Write-host "Working on" $i "of" $numberToUpdate -foregroundcolor Green
    Write-Host "Updating" $productName with "QTY:" $qty -ForegroundColor Green
    #################################
    ## Post to Shopify
    #################################
    $results = $inventoryItemID = $postbody = $null
    ## Get Item ID by Sku
    $inventoryItemID = $newQty.'Variant Inventory Item ID'
            
    ## Body
    $postbody = [pscustomobject]@{
        location_id = $locationID
        inventory_item_id = $inventoryItemID
        available = $qty
    } | convertTo-json
    $results = Invoke-WebRequest -Method POST -Uri $uri -Header $headers -Body $postbody -UseBasicParsing
    Write-host "Response code:" $results.StatusCode
    
    if($results.StatusCode -notlike "200"){
        Write-host "Something is wrong. Pausing run" -ForegroundColor Red
        Pause
    }
    write-host "API Call LIMIT:" $results.Headers.HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT -ForegroundColor Blue
    if(($results.Headers).HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT -like "40/40"){
        Write-Host "Pausing for 5 seconds due to high API CALL LIMIT" -ForegroundColor Red
        Start-Sleep -Seconds 5
    }
    Write-Host ""
     
    $i++ 
}
Write-Host "Start Time:" $startTime
Write-Host "End Time:" $(get-date)
Sign Up to get updates on the latest article

Get the latest from my blog when you sign up.  Special offers are only offered to those who sign up.  Specific DIY are emailed to users when they become available.  Sign up today to get notified.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.