Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Shopify cart context

import React, { Component } from "react";
import { client } from "../utils/shopifyStore";
const ShopContext = React.createContext();

class ShopProvider extends Component {
  state = {
    products: [],
    product: {},
    checkout: {},
    isCartOpen: false,
  };
  componentDidMount() {
    if (localStorage.checkout_id) {
      this.fetchCheckout(localStorage.checkout_id);
    } else {
      this.createCheckout();
    }
  }
  createCheckout = async () => {
    const checkout = await client.checkout.create();
    localStorage.setItem("checkout_id", checkout.id);
    this.setState({ checkout: checkout });
  };
  fetchCheckout = async (checkoutId) => {
    client.checkout.fetch(checkoutId).then((checkout) => {
      this.setState({ checkout: checkout });
    });
  };
  addItemTocheckout = async (variantId, quantity) => {
    const lineItemToAdd = [
      {
        variantId,
        quantity: parseInt(quantity, 10),
      },
    ];
    const checkout = await client.checkout.addLineItems(
      this.state.checkout.id,
      lineItemToAdd
    );
    this.setState({ checkout: checkout });
    console.log("added", checkout);
  };
  closeCart = () => {
    this.setState({ isCartOpen: false });
  };
  openCart = () => {
    this.setState({ isCartOpen: true });
  };
  render() {
    return (
      <ShopContext.Provider
        value={{
          ...this.state,
          closeCart: this.closeCart,
          openCart: this.openCart,
          addItemTocheckout: this.addItemTocheckout,
        }}
      >
        {this.props.children}
      </ShopContext.Provider>
    );
  }
}
const ShopConsumer = ShopContext.Consumer;
export { ShopConsumer, ShopContext };
export default ShopProvider;
Comment

PREVIOUS NEXT
Code Example
Javascript :: react form validation with logic boolean, string and number 
Javascript :: prisma multiple queries in one query 
Javascript :: react native password qwerty 
Javascript :: javascript foreach next iteration 
Javascript :: what is prototype-based in javascreipt 
Javascript :: mvc return view with query string 
Javascript :: FirebaseError: Unknown field filter op. 
Javascript :: facebook access token 
Javascript :: telerik jquery grid set page size 
Javascript :: sort list by likes in javascript 
Javascript :: multi command run in one in terminal npm 
Javascript :: node package manager 
Javascript :: prisma.db yaml 
Javascript :: Include Path reactjs in VS code in in urud 
Javascript :: jquery code convert into javascript online 
Javascript :: click mouseup mousedown 
Javascript :: symfony iterate over entity 
Javascript :: single data class value api respone 
Javascript :: how to get value of selected radio button in javascript 
Javascript :: javascript declaring variables 
Javascript :: get time in between two dates javascript dayjs 
Javascript :: copy value from one sheet to anotehr 
Javascript :: js how to shuffle array algoritm. The Fisher-Yates algorith 
Javascript :: laravel , json Why it shows Cannot access offset of type string on string error 
Javascript :: how do i set CORS policy for nodejs sever 
Javascript :: angularjs Add aria-label to table header in datatable 
Javascript :: Calling $http.post in batches and chaining promises 
Javascript :: When doing a booking system, where would it be better to do? Back end or front end 
Javascript :: js generate pnh 
Javascript :: Using javascript code in Jade views - if(variable) shows undefined instead of passing 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =