How to update shipping zone with custom Python code?

Solved

How to update shipping zone with custom Python code?

komailnoori
Shopify Partner
6 0 0

I want to import a custom Shopify app Python code that updates whenever the currency rate goes up or down in the market. I want to run the code and connect it directly to the Shipping zone to update it. I have been trying a lot, but I'm unable to do it. I feel like I'm missing something. I kindly request your help. Thank you.


import csv
import requests
import json

# Insert your Shopify store access token and domain
headers = {
    'Content-Type': 'application/json',
    'X-Shopify-Access-Token': 'Your_Access-TOken' 
}

# The Shopify shipping rate creation GraphQL mutation
RATE_CREATION_MUTATION = '''
mutation createShippingRate($profileId: ID!, $rate: ShippingRateInput!) {
  shippingRateCreate(profileId: $profileId, rate: $rate) {
    rate {
      id
      title
    }
    userErrors {
      field
      message
    }
  }
}
'''

# Function to import shipping rates from a CSV file
def import_shipping_rates():
    profile_id = '106387996951'  # Replace with your shipping profile ID
    csv_file = '1.csv'  # Replace with your CSV file path

    with open(csv_file, 'r') as file:
        reader = csv.DictReader(file)
        for row in reader:
            rate = {
                'title': row['Shipping method'],
                'carrier': row['Carrier'],
                'minWeight': float(row['Minimal weight']),
                'maxWeight': float(row['Maximal weight']),
                'price': row['Price'],
                'countryCode': row['Country code (ISO 2)'],
                'provinceCode': row['Province code']
            }

            response = requests.post(
                f'https://{Shop}.myshopify.com/admin/api/2023-07/graphql.json',
                headers=headers,
                data=json.dumps({'query': RATE_CREATION_MUTATION, 'variables': {'profileId': profile_id, 'rate': rate}})
            )

            data = response.json()

            if 'errors' in data:
                for error in data['errors']:
                    if isinstance(error, dict):
                        print("GraphQL Error: %s" % (error['message'],))
                    else:
                        print("GraphQL Error: %s" % (error,))
                return

            if 'data' in data and 'shippingRateCreate' in data['data']:
                rate_result = data['data']['shippingRateCreate']['rate']

                if rate_result is None:
                    print("Failed to create the shipping rate.")
                else:
                    rate_id = rate_result['id']
                    rate_title = rate_result['title']
                    print("Shipping rate created successfully with ID: %s, Title: %s" % (rate_id, rate_title))
            else:
                print("Invalid response received from the API.")

# Call the function to import the shipping rates
import_shipping_rates()

Attached CSV file it's correct?
Please help me

 

Accepted Solution (1)

sarfaraz1101
Shopify Partner
1 1 0

This is an accepted solution.

Reply 1 (1)

sarfaraz1101
Shopify Partner
1 1 0

This is an accepted solution.