PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, June 30, 2022

[FIXED] How to set a policy/flag to stop a product from selling when out of stock

 June 30, 2022     javascript, node.js, shopify     No comments   

Issue

I'm really new to the Shopify API, and I'm working with someone else's code. I'm uploading a product trough my Shopify store, but I want to set some sort of policy where a product can't be sold when it's out of stock, is there a way this can be done on code?

Every time I create a product on Shopify, I do it like this:

static async createProductInShopify(product) {
    const createProductsURL = SHOPIFY_URL + '/products.json';
    const latestPrice = product.prices[product.prices.length - 1];

    const requestBody = {
        product: {
            title: product.name,
            body_html: product.body,
            vendor: 'My Store',
            product_type: 'Sample Product',
            handle: product.handle,
            tags: product.categories,
            images: [
                {
                    src: product.imageURL
                }
            ],
            variants: [
                {
                    option1: product.size ? 'Big' : 'Normal',
                    price: latestPrice,
                    requires_shipping: true,
                    taxable: false,
                    inventory_quantity: product.quantity,
                    inventory_management: 'shopify',
                    inventory_policy: 'continue'
                }
            ]
        }
    };

    const response = await ShopifyController.fetch(createProductsURL, requestBody, 'POST', product);

    const shopifyJSON = await response.json();
    product.productID = `${shopifyJSON.product.id}`;
    product.productVariantID = `${shopifyJSON.product.variants[0].id}`;
    product.inventoryLevelID = `${shopifyJSON.product.variants[0].inventory_item_id}`;
    await product.save();
    return product;
}

I assume that the inventory_policy is where that flag can be set, however, I need some confirmation to see if that is true.


Solution

yes, the inventory_policy for product variant has to be set to something. i am sharing the screenshot for the value.



Answered By - Lakshay Kalra
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing