项目作者: Gomah

项目描述 :
🛍 Seamless Shopify Buy SDK integration with Nuxt.js.
高级语言: TypeScript
项目地址: git://github.com/Gomah/nuxt-shopify.git
创建时间: 2019-03-31T09:14:50Z
项目社区:https://github.com/Gomah/nuxt-shopify

开源协议:MIT License

下载


nuxt-shopify

🛍 Shopify Module

npm version
Dependencies
npm downloads
code style: prettier
License: MIT
FOSSA Status

Easy Shopify Buy client integration with Nuxt.js

Setup

Install with yarn:

  1. yarn add nuxt-shopify

Install with npm:

  1. npm install nuxt-shopify

nuxt.config.js

  1. module.exports = {
  2. modules: ['nuxt-shopify'],
  3. shopify: {
  4. /**
  5. * Your shopify domain
  6. */
  7. domain: 'your-shop-name.myshopify.com',
  8. /**
  9. * Your shopify storefront access token
  10. */
  11. storefrontAccessToken: 'your-storefront-access-token',
  12. /**
  13. * This will be larger than the optimized version, as it will contain all fields that are available in the
  14. * Storefront API. (https://help.shopify.com/en/api/custom-storefronts/storefront-api/reference)
  15. * This should only be used when you need to add custom queries to supplement the JS Buy SDK queries.
  16. */
  17. unoptimized: false,
  18. /**
  19. * Set language to return translated content (optional)
  20. */
  21. language: 'ja-JP',
  22. },
  23. };

OR

  1. module.exports = {
  2. modules: ['nuxt-shopify'],
  3. env: {
  4. SHOPIFY_DOMAIN: 'your-shop-name.myshopify.com', // your shopify domain
  5. SHOPIFY_ACCESS_TOKEN: 'your-storefront-access-token', // your shopify storefront access token
  6. },
  7. };

Don’t have a Storefront Access Token yet? Get started.

Usage

Component

asyncData

  1. async asyncData({ $shopify, params }) {
  2. const product = await $shopify.product.fetch(params.id);
  3. return { product };
  4. }

methods/created/mounted/etc

  1. methods: {
  2. async fetchProduct(productId) {
  3. this.product = await this.$shopify.product.fetch(productId);
  4. }
  5. }

Store actions (including nuxtServerInit)

  1. // In store
  2. {
  3. actions: {
  4. async fetchAllProducts ({ commit }) {
  5. const products = await this.$shopify.product.fetchAll();
  6. commit('SET_PRODUCTS', products)
  7. }
  8. }
  9. }

Shopify Client

Examples from the official shopify-buy sdk library

Fetching products

  1. // Fetch all products in your shop
  2. this.$shopify.product.fetchAll().then((products) => {
  3. // Do something with the products
  4. console.log(products);
  5. });
  6. // Fetch a single product by ID
  7. const productId = 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0Lzc4NTc5ODkzODQ=';
  8. this.$shopify.product.fetch(productId).then((product) => {
  9. // Do something with the product
  10. console.log(product);
  11. });

Fetching Collections

  1. // Fetch all collections, including their products
  2. this.$shopify.collection.fetchAllWithProducts().then((collections) => {
  3. // Do something with the collections
  4. console.log(collections);
  5. console.log(collections[0].products);
  6. });
  7. // Fetch a single collection by ID, including its products
  8. const collectionId = 'Z2lkOi8vc2hvcGlmeS9Db2xsZWN0aW9uLzM2OTMxMjU4NA==';
  9. this.$shopify.collection.fetchWithProducts(collectionId).then((collection) => {
  10. // Do something with the collection
  11. console.log(collection);
  12. console.log(collection.products);
  13. });

Creating a checkout

  1. // Create an empty checkout
  2. this.$shopify.checkout.create().then((checkout) => {
  3. // Do something with the checkout
  4. console.log(checkout);
  5. });

Adding Line Items

  1. const checkoutId = 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0SW1hZ2UvMTgyMTc3ODc1OTI='; // ID of an existing checkout
  2. const lineItemsToAdd = [
  3. {
  4. variantId: 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8yOTEwNjAyMjc5Mg==',
  5. quantity: 5,
  6. customAttributes: [{ key: 'MyKey', value: 'MyValue' }],
  7. },
  8. ];
  9. // Add an item to the checkout
  10. this.$shopify.checkout.addLineItems(checkoutId, lineItemsToAdd).then((checkout) => {
  11. // Do something with the updated checkout
  12. console.log(checkout.lineItems); // Array with one additional line item
  13. });

Updating checkout attributes

  1. const checkoutId = 'Z2lkOi8vc2hvcGlmeS9DaGVja291dC9kMTZmM2EzMDM4Yjc4N=';
  2. const input = { customAttributes: [{ key: 'MyKey', value: 'MyValue' }] };
  3. this.$shopify.checkout.updateAttributes(checkoutId, input).then((checkout) => {
  4. // Do something with the updated checkout
  5. });

Updating Line Items

  1. const checkoutId = 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0SW1hZ2UvMTgyMTc3ODc1OTI='; // ID of an existing checkout
  2. const lineItemsToUpdate = [{ id: 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0Lzc4NTc5ODkzODQ=', quantity: 2 }];
  3. // Update the line item on the checkout (change the quantity or variant)
  4. this.$shopify.checkout.updateLineItems(checkoutId, lineItemsToUpdate).then((checkout) => {
  5. // Do something with the updated checkout
  6. console.log(checkout.lineItems); // Quantity of line item 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0Lzc4NTc5ODkzODQ=' updated to 2
  7. });

Removing Line Items

  1. const checkoutId = 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0SW1hZ2UvMTgyMTc3ODc1OTI='; // ID of an existing checkout
  2. const lineItemIdsToRemove = ['Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0Lzc4NTc5ODkzODQ='];
  3. // Remove an item from the checkout
  4. this.$shopify.checkout.removeLineItems(checkoutId, lineItemIdsToRemove).then((checkout) => {
  5. // Do something with the updated checkout
  6. console.log(checkout.lineItems); // Checkout with line item 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0Lzc4NTc5ODkzODQ=' removed
  7. });

Fetching a Checkout

  1. const checkoutId = '2U4NWNkYzI4ZWEyOTdlOD9rZXk9MDVjMzY3Zjk3YWM0YWJjNGRhMTkwMDgwYTUzOGJmYmI=';
  2. this.$shopify.checkout.fetch(checkoutId).then((checkout) => {
  3. // Do something with the checkout
  4. console.log(checkout);
  5. });

Adding a Discount

  1. const checkoutId = 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0SW1hZ2UvMTgyMTc3ODc1OTI='; // ID of an existing checkout
  2. const discountCode = 'best-discount-ever';
  3. // Add a discount code to the checkout
  4. this.$shopify.checkout.addDiscount(checkoutId, discountCode).then((checkout) => {
  5. // Do something with the updated checkout
  6. console.log(checkout);
  7. });

Removing a Discount

  1. const checkoutId = 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0SW1hZ2UvMTgyMTc3ODc1OTI='; // ID of an existing checkout
  2. // Removes the applied discount from an existing checkout.
  3. this.$shopify.checkout.removeDiscount(checkoutId).then((checkout) => {
  4. // Do something with the updated checkout
  5. console.log(checkout);
  6. });

Updating a Shipping Address

  1. const checkoutId = 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0SW1hZ2UvMTgyMTc3ODc1OTI='; // ID of an existing checkout
  2. const shippingAddress = {
  3. address1: 'Chestnut Street 92',
  4. address2: 'Apartment 2',
  5. city: 'Louisville',
  6. company: null,
  7. country: 'United States',
  8. firstName: 'Bob',
  9. lastName: 'Norman',
  10. phone: '555-625-1199',
  11. province: 'Kentucky',
  12. zip: '40202',
  13. };
  14. // Update the shipping address for an existing checkout.
  15. this.$shopify.checkout.updateShippingAddress(checkoutId, shippingAddress).then((checkout) => {
  16. // Do something with the updated checkout
  17. });

Development

  1. Clone this repository
  2. Install dependencies using yarn install or npm install
  3. Build the module using yarn build or npm run build
  4. Start development server using yarn dev or npm run dev

📑 License

MIT License

FOSSA Status