Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Changing Shopify store product prices programmatically

Solved

Changing Shopify store product prices programmatically

mgiara
Visitor
2 0 4

Hello!

 

I'm looking for a way to change the prices of items in my storefront programmatically.

 

I'm sure there's an API for it, I'm just a bit overwhelmed with all of the lingo I'm seeing, "apps" and whatnot.

 

Also noticed that the API's are broken in to many subcategories (Admin, Partner, App Bridge, Remix, Payments, Shopify Function, and Multipass) so was wondering someone could guide me towards which might be best suited for changing item prices?

Accepted Solution (1)

Jclewis1989
Shopify Partner
18 1 8

This is an accepted solution.

Hey there Mgiara!

Let's break down the components involved in programmatically changing item prices on a Shopify storefront and understand the relevant APIs and technologies:

  1. Shopify Storefront:

    • A Shopify storefront is the public-facing online store where customers browse and purchase products.
    • It typically includes product listings, product details, a shopping cart, and a checkout process.
  2. Admin API:

    • The Shopify Admin API allows you to interact with the administrative backend of your Shopify store.
    • It provides access to manage products, collections, orders, customers, and other store-related data.
    • You can use the Admin API to change item prices programmatically.
  3. Storefront API:

    • The Shopify Storefront API is designed for building custom, client-side storefronts.
    • It allows you to fetch store data like product information, collections, and customer details.
    • While it's essential for building custom storefronts, it doesn't have the capability to directly modify item prices.
  4. Remix:

    • Remix is not a Shopify API; it is a modern framework for building web applications.
    • It's used for creating interactive and engaging web experiences.
    • You may use Remix alongside Shopify APIs to enhance your storefront, but it doesn't handle price changes directly.
    • Remix is built on top of React and is used to combine front and backend technologies, simplifying the building process for embedded Shopify apps in general.
  5. App Bridge:

    • Shopify App Bridge is a JavaScript library that helps embedded apps seamlessly integrate into the Shopify Admin.
    • It allows your app to interact with the Shopify Admin without leaving the Shopify environment.
    • Useful if you're building an app that modifies prices through the admin interface but not directly on the storefront.
  6. Shopify Functions:

    • Shopify Functions is a feature that enables serverless functions to be deployed within Shopify.
    • These functions can be used to customize your storefront's behavior.
    • While they can enhance your storefront, they are not typically used to change item prices directly.
  7. Multipass:

    • Multipass is a feature that helps with single sign-on (SSO) between your Shopify store and other applications.
    • It's not directly related to changing item prices on your storefront.

To programmatically change item prices on your Shopify storefront, you'll primarily want to use the Shopify Admin API. Here's a high-level overview of the steps involved:

 

Here is an example of how to authenticate to a Shopify API in Python / Flask with an Access Token in the header of the request. This is a GET and a PUT request, for your edification.

@Anonymous_bp.route('/')
def index():
    return render_template('index.html')

@products_bp.route('/products', methods=['GET'])
def get_all_products():
    # Define the URL for the REST API request
    url = f'https://{SHOP_NAME}.myshopify.com/admin/api/{ADMIN_API_VERSION}/products.json'

    # Headers for the REST API request
    headers = {
        'Content-Type': 'application/json',
        'X-Shopify-Access-Token': ADMIN_API_ACCESS_TOKEN
    }

    # Make the REST API request
    response = requests.get(url, headers=headers)

    # Check if the request was successful
    if response.status_code == 200:
        return jsonify(response.json())
    else:
        return jsonify({'error': 'Unable to fetch products'}), response.status_code

@products_bp.route('/products/<product_id>', methods=['GET'])
def get_product_by_id(product_id):
    try:
        # Define the URL for the REST API request
        url = f'https://{SHOP_NAME}.myshopify.com/admin/api/{ADMIN_API_VERSION}/products/{product_id}.json'

        # Headers for the REST API request
        headers = {
            'Content-Type': 'application/json',
            'X-Shopify-Access-Token': ADMIN_API_ACCESS_TOKEN
        }

        # Make the REST API request
        response = requests.get(url, headers=headers)

        # Check if the request was successful
        if response.status_code == 200:
            return jsonify(response.json())
        elif response.status_code == 404:
            return jsonify({'error': 'Product not found'}), 404
        else:
            return jsonify({'error': 'Unable to fetch product'}), response.status_code
    except Exception as e:
        return jsonify({'error': 'An error occurred', 'message': str(e)}), 500


@products_bp.route('/update_product/<product_id>', methods=['PUT'])
def update_product(product_id):
    try:
        # Extract the product details from the request body
        product_details = request.json
        # Define the URL for the REST API request
        url = f"https://{SHOP_NAME}.myshopify.com/admin/api/{ADMIN_API_VERSION}/products/{product_id}.json"

        # Headers for the REST API request
        headers = {
            "Content-Type": "application/json",
            "X-Shopify-Access-Token": ADMIN_API_ACCESS_TOKEN
        }

        # Prepare the data payload with the new product details
        payload = {
            "product": product_details
        }
        
        # Send the request to Shopify via REST API
        response = requests.put(url, json=payload, headers=headers)

        # Log response for debugging
        print(f"Response Status Code: {response.status_code}")
        print(f"Response Body: {response.text}")

        # Check if the request was successful
        if response.status_code in [200, 201, 202]:
            return jsonify({"success": True, "message": "Product updated successfully"})
        else:
            return jsonify({"success": False, "message": "Failed to update product", "error": response.text}), response.status_code
    except Exception as e:
        return jsonify({"success": False, "message": "An error occurred", "error": str(e)}), 500
James Lewis

View solution in original post

Replies 4 (4)

Jclewis1989
Shopify Partner
18 1 8

This is an accepted solution.

Hey there Mgiara!

Let's break down the components involved in programmatically changing item prices on a Shopify storefront and understand the relevant APIs and technologies:

  1. Shopify Storefront:

    • A Shopify storefront is the public-facing online store where customers browse and purchase products.
    • It typically includes product listings, product details, a shopping cart, and a checkout process.
  2. Admin API:

    • The Shopify Admin API allows you to interact with the administrative backend of your Shopify store.
    • It provides access to manage products, collections, orders, customers, and other store-related data.
    • You can use the Admin API to change item prices programmatically.
  3. Storefront API:

    • The Shopify Storefront API is designed for building custom, client-side storefronts.
    • It allows you to fetch store data like product information, collections, and customer details.
    • While it's essential for building custom storefronts, it doesn't have the capability to directly modify item prices.
  4. Remix:

    • Remix is not a Shopify API; it is a modern framework for building web applications.
    • It's used for creating interactive and engaging web experiences.
    • You may use Remix alongside Shopify APIs to enhance your storefront, but it doesn't handle price changes directly.
    • Remix is built on top of React and is used to combine front and backend technologies, simplifying the building process for embedded Shopify apps in general.
  5. App Bridge:

    • Shopify App Bridge is a JavaScript library that helps embedded apps seamlessly integrate into the Shopify Admin.
    • It allows your app to interact with the Shopify Admin without leaving the Shopify environment.
    • Useful if you're building an app that modifies prices through the admin interface but not directly on the storefront.
  6. Shopify Functions:

    • Shopify Functions is a feature that enables serverless functions to be deployed within Shopify.
    • These functions can be used to customize your storefront's behavior.
    • While they can enhance your storefront, they are not typically used to change item prices directly.
  7. Multipass:

    • Multipass is a feature that helps with single sign-on (SSO) between your Shopify store and other applications.
    • It's not directly related to changing item prices on your storefront.

To programmatically change item prices on your Shopify storefront, you'll primarily want to use the Shopify Admin API. Here's a high-level overview of the steps involved:

 

Here is an example of how to authenticate to a Shopify API in Python / Flask with an Access Token in the header of the request. This is a GET and a PUT request, for your edification.

@Anonymous_bp.route('/')
def index():
    return render_template('index.html')

@products_bp.route('/products', methods=['GET'])
def get_all_products():
    # Define the URL for the REST API request
    url = f'https://{SHOP_NAME}.myshopify.com/admin/api/{ADMIN_API_VERSION}/products.json'

    # Headers for the REST API request
    headers = {
        'Content-Type': 'application/json',
        'X-Shopify-Access-Token': ADMIN_API_ACCESS_TOKEN
    }

    # Make the REST API request
    response = requests.get(url, headers=headers)

    # Check if the request was successful
    if response.status_code == 200:
        return jsonify(response.json())
    else:
        return jsonify({'error': 'Unable to fetch products'}), response.status_code

@products_bp.route('/products/<product_id>', methods=['GET'])
def get_product_by_id(product_id):
    try:
        # Define the URL for the REST API request
        url = f'https://{SHOP_NAME}.myshopify.com/admin/api/{ADMIN_API_VERSION}/products/{product_id}.json'

        # Headers for the REST API request
        headers = {
            'Content-Type': 'application/json',
            'X-Shopify-Access-Token': ADMIN_API_ACCESS_TOKEN
        }

        # Make the REST API request
        response = requests.get(url, headers=headers)

        # Check if the request was successful
        if response.status_code == 200:
            return jsonify(response.json())
        elif response.status_code == 404:
            return jsonify({'error': 'Product not found'}), 404
        else:
            return jsonify({'error': 'Unable to fetch product'}), response.status_code
    except Exception as e:
        return jsonify({'error': 'An error occurred', 'message': str(e)}), 500


@products_bp.route('/update_product/<product_id>', methods=['PUT'])
def update_product(product_id):
    try:
        # Extract the product details from the request body
        product_details = request.json
        # Define the URL for the REST API request
        url = f"https://{SHOP_NAME}.myshopify.com/admin/api/{ADMIN_API_VERSION}/products/{product_id}.json"

        # Headers for the REST API request
        headers = {
            "Content-Type": "application/json",
            "X-Shopify-Access-Token": ADMIN_API_ACCESS_TOKEN
        }

        # Prepare the data payload with the new product details
        payload = {
            "product": product_details
        }
        
        # Send the request to Shopify via REST API
        response = requests.put(url, json=payload, headers=headers)

        # Log response for debugging
        print(f"Response Status Code: {response.status_code}")
        print(f"Response Body: {response.text}")

        # Check if the request was successful
        if response.status_code in [200, 201, 202]:
            return jsonify({"success": True, "message": "Product updated successfully"})
        else:
            return jsonify({"success": False, "message": "Failed to update product", "error": response.text}), response.status_code
    except Exception as e:
        return jsonify({"success": False, "message": "An error occurred", "error": str(e)}), 500
James Lewis
mgiara
Visitor
2 0 4

This has to be the best reply I've ever received from a question on any kind of forum.

 

Can't thank you enough James.

saisaikrishna
Shopify Partner
2 0 0

Hi - Can you elaborate on REST API's and GRAPHQL API's and where does these stand between ADMIN API and STOREFRONT API?

shashank-a
Shopify Partner
8 0 5

That is an amazing reply. Shows your dedication in the community. One thing, what is the difference between Storefront API, AJAX API and Headless Storefront API?