npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sb-m/shopify-provider

v1.1.1

Published

A React hook for managing Shopify line-items and checkout.

Downloads

6

Readme

@sb-m/shopify-provider

Overview

@sb-m/shopify-provider is a React provider for managing Shopify line items and checkout. It allows you to add, remove, and update line items, and prepare a Shopify checkout to redirect to.

Installation

npm install @sb-m/shopify-provider

Provider

The provider manages lineItems and checkout data.

import { Provider as ShopifyProvider } from '@sb-m/shopify-provider'

const credentials = {
	domain: 'YOUR_SHOPIFY_DOMAIN',
	storefrontAccessToken: 'YOUR_SHOPIFY_STOREFRONT_ACCESS_TOKEN',
}

const App = () => (
	<ShopifyProvider credentials={credentials}>
		// App
	</ShopifyProvider>
)

useLineItems

Hook to manage line items. You can use useLineItems anywhere inside the Provider.

Syntax

import { useLineItems } from '@sb-m/shopify-provider'

const Component = () => {
	const { add, lineItems } = useLineItems({
		variantData: // pass in array of variant data add to lineItem data
	})

	return (
		<ul>
			{lineItems.map(({ variantId, quantity }) => (
				<li key={variantId}>
					Variant ID: {variantId}
					Quantity: {quantity}
				</li>
			))}s
		</ul>
	)
}

Functions

Functions returned by useLineItems

add

Adds variant to line items.

Syntax
const { add } = useLineItems()

add(variantId, quantity)

remove

Removes line items.

Syntax
const { remove } = useLineItems()

remove(variantId)

setQuantity

Sets a line item quantity.

Syntax
const { setQuantity } = useLineItems()

setQuantity(variantId, quantity)

increment

Increments a line item quantity.

Syntax
const { increment } = useLineItems()

increment(variantId)

decrement

Decrements a line item quantity.

Syntax
const { decrement } = useLineItems()

decrement(variantId)

Data

Data returned from useLineItems

lineItems

An array of current line items.

Syntax
const { lineItems } = useLineItems()

console.log(lineItems)
/*
	Example data: [
		{
			variantId: 'variantId',
			quantity: 1,
		}
	]
*/

useCheckout

Hook to manage Shopify checkout. You can use useCheckout anywhere inside the Provider. useCheckout uses line items set by useLineItems.

Functions

prepare

Prepares a checkout either by creating a new one, or updating the existing one (it does this automatically). It will return a checkout.

Syntax
const { prepare } = useCheckout()

const checkout = await prepare()

console.log(checkout.webUrl) // URL to redirect to Shopify Checkout

Data

id

The current Shopify checkout ID.

Syntax
const { id } = useCheckout()

console.log(id) // Shopify Checkout

loading

Will be true if the checkout is loading.

Syntax
const { loading } = useCheckout()

console.log(loading) // true if loading

client

The Shopify client returned from shopify-buy.

Syntax
const { client } = useCheckout()

// Use client (see shopify-buy documentation). For example:
client.checkout.addDiscount(checkoutId, discountCode)