Remix

How to Update Product with Variants using Rest API in Shopify Remix App?

Hello Shopify Friends,

In this article, we will learn about how to update the product and its variant using Rest API via Shopify Remix.

Keeping your product listings up-to-date is crucial for a seamless customer experience. Shopify offers a powerful REST API that allows developers to efficiently manage product updates, especially when dealing with variants.

Certainly! To update the product, you need to have the product ID and its variant ID. Below is a step-by-step guide to getting product ID and variant ID.

Steps to Update Product with Variants using Rest API in Shopify Remix App:

Step 1: Go to the Products field in the Shopify store.

Step 2: Select any product from the list and get your product ID from the URL.

Step 3: For the same, if you click on the variant, then you will get the variant ID from the URL.

Step 4: Now, we can update the product with API.

For Updating products, just create a page in your app named app.restapi.jsx in the App/Route folder. Create a loader function and return null from it.

export const loader = () => null;

Now, create the default index function and design form into it.

export default function Index() {
  return (
   <>
    <Form method="POST">
     <Button variant="primary" submit>
      Click
     </Button>
    <Form>
   </>
  );}

Now, create an action function and pass data into it for process.

export const action = async ({ request }) => {
  try {
   const { admin, session } = await authenticate.admin(request);
   const product = new admin.rest.resources.Product({ session: session });
   product.id = 8782945288486;  // Place here your actual Product id
   product.variants = [
   {
    "id": 47493656215846, // Place here your actual Variant id
    "price": "1499.99",
    "sku": "Updated Sku"
   }];

   await product.save({
   update: true,
  });

  return null;
  } 
  catch (error) {
   console.error("Error in action:", error);
   throw error;
  }
 };

Note: Donโ€™t Forget to Import the Required Libraries for your code.ย ย 

Step 5: After this, run your code with a button click.

Step 6: Now, see the product that you processed.

Conclusion:

In this article, we learned how to update the product and its variant data using rest API with the help of remix js. Using rest API, you can also customise multiple products at a time dynamically in less time.

Note: You can also customize the form and pass product data dynamically to the rest API.

Click to rate this post!
[Total: 0 Average: 0]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ๐Ÿ… with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess โ™Ÿ๏ธ or Cricket ๐Ÿ.

View Comments

  • Hi I have followed the same steps but ended up with this error

    react-dom.development.js:4312 Uncaught TypeError: onSubmit is not a function
    at Form.js:25:5
    at HTMLUnknownElement.callCallback2 (react-dom.development.js:4164:14)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:16)
    at invokeGuardedCallback (react-dom.development.js:4277:31)
    at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:4291:25)
    at executeDispatch (react-dom.development.js:9041:3)
    at processDispatchQueueItemsInOrder (react-dom.development.js:9073:7)
    at processDispatchQueue (react-dom.development.js:9086:5)
    at dispatchEventsForPlugins (react-dom.development.js:9097:3)
    at react-dom.development.js:9288:12

    This is my code

    import {
    Button,
    Form
    } from "@shopify/polaris";
    import { authenticate } from "../shopify.server";
    export const loader = () => null;
    export const action = async ({ request }) => {
    try {

    const { admin, session } = await authenticate.admin(request);
    const product = new admin.rest.resources.Product({ session: session });
    product.id = 9000507277590; // Place here your actual Product id
    product.
    product.variants = [
    {
    "id": 47506645483798, // Place here your actual Variant id
    "price": "1499.99",
    "sku": "Updated Sku"
    }];

    await product.save({
    update: true,
    });

    return null;
    }
    catch (error) {
    console.error("Error in action:", error);
    throw error;
    }
    };

    export default function Index() {
    return (

    Click

    );
    }

    • First "Click" is not a proper return of the app route file. Also, You may set up the "Form" component to call the Remix app's action function with the POST request method. (as per our example) So you can get better responses.

Recent Posts

Mastering Tailwind CSS in Laravel: A Comprehensive Guide

Tailwind CSS has emerged as a powerful utility-first CSS framework, offering developers a unique approach…

2 days ago

React Native or Flutter in 2024

The mobile app development field has witnessed a rapid revolution over the past few years.…

4 days ago

Magento 2: How To Call JS on the Checkout Page?

Hello Magento mates, Today we will learn to add a call JS on the checkout…

7 days ago

Boost Your SEM Game: Unveiling the Top 10 Tools for Marketers in 2024

Business survival in todayโ€™s digital world has become extremely difficult. Using traditional marketing techniques is…

1 week ago

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

1 week ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

1 week ago