Edits history of script submission #4377 for ' Updates an existing discount code (shopify)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type Shopify = {
      token: string;
      store_name: string;
    };
    /**
     * Updates an existing discount code
     * Updates an existing discount code
     */
    export async function main(
      auth: Shopify,
      api_version: string = "2023-10",
      price_rule_id: string,
      discount_code_id: string,
      body: {
        discount_code?: {
          code?: string;
          created_at?: string;
          id?: number;
          updated_at?: string;
          usage_count?: number;
          [k: string]: unknown;
        };
        [k: string]: unknown;
      }
    ) {
      const url = new URL(
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/price_rules/${price_rule_id}/discount_codes/${discount_code_id}.json`
      );
    
      const response = await fetch(url, {
        method: "PUT",
        headers: {
          "Content-Type": "application/json",
          "X-Shopify-Access-Token": auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 396 days ago

  • nativets
    type Shopify = {
      token: string;
      store_name: string;
    };
    /**
     * Updates an existing discount code
     * Updates an existing discount code
     */
    export async function main(
      auth: Shopify,
      api_version: string = "2023-10",
      price_rule_id: string,
      discount_code_id: string,
      body: {
        discount_code?: {
          code?: string;
          created_at?: string;
          id?: number;
          updated_at?: string;
          usage_count?: number;
          [k: string]: unknown;
        };
        [k: string]: unknown;
      }
    ) {
      const url = new URL(
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/price_rules/${price_rule_id}/discount_codes/${discount_code_id}.json`
      );
    
      const response = await fetch(url, {
        method: "PUT",
        headers: {
          "Content-Type": "application/json",
          "X-Shopify-Access-Token": auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 942 days ago