LoginSignup
1
0

shopifyのDiscount Functionアプリの作成

Posted at

shopifyのDiscount Functionアプリの作成

Shopifyでカスタムディスカウント機能を持つアプリの開発した。このアプリは、顧客メタフィールドに基づいて、特定の期間と商品に対してディスカウントを適用するものです。これにより、柔軟な顧客別ディスカウントが実現可能となります。

開発の過程で最も重要だったのは、RunInputQueryに関するドキュメントの理解でした。これにより、必要な情報の取得方法ができます。

具体的なクエリと関連ドキュメント

クエリ実装に関するドキュメント:Add configuration to your discounts experience

GraphQLの共通オブジェクトに関するドキュメント

時刻の取得に関する課題と解決策

クエリの実行時、現在の時刻を取得する必要がありました。最初はJavaScriptのDate.now()を使用しようと試みましたが、Unixエポック時間(1970年1月1日からの経過ミリ秒)を返すため、問題が発生しました。この問題を解決するため、Shopifyのインプットクエリで提供されているlocaltime(localtime)を使用して現在時刻を取得する方法に切り替えました。

クエリの実装

以下は、カートの中の商品と顧客メタフィールドから期間、対象商品、ディスカウント率を取得し、それらを基に割引を適用するクエリの例です。

query RunInput {
  cart {
    lines {
      quantity
      merchandise {
        __typename
        ... on ProductVariant {
          id
        }
      }
    }
    buyerIdentity {
      customer {
        timesaleProductMetafield: metafield(
          namespace: "custom"
          key: "timesaleproduct"
        ) {
          value
        }
        timesaleStartMetafield: metafield(
          namespace: "custom"
          key: "timesalestart"
        ) {
          value
        }
        timesaleEndMetafield: metafield(
          namespace: "custom"
          key: "timesaleend"
        ) {
          value
        }
        discountRateMetafield: metafield(
          namespace: "custom"
          key: "discountrate"
        ) {
          value
        }
      }
    }
  }
  shop {
    localTime {
      date
    }
  }
}
/**
 * The configured entrypoint for the 'purchase.product-discount.run' extension target
 * @param {RunInput} input
 * @returns {FunctionRunResult}
 */
export function run(input) {
  const { cart, shop } = input;
  const customerMetafields = cart.buyerIdentity?.customer;
  the shopTime = shop.localTime.date;
  if (!customerMetafields) {
    console.error("No customer metafields found.");
    return EMPTY_DISCOUNT;
  }
  // ここにディスカウント適用ロジックを実装
}


1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0