123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- Basic Implementation of Cart
-
- 1. Create cart using mutation
-
- mutation CreateCart {
- cartCreate {
- cart {
- id
- createdAt
- updatedAt
- }
- userErrors {
- field
- message
- }
- }
- }
-
- response:
-
- {
- "data": {
- "cartCreate": {
- "cart": {
- "id": "gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSkZFVDNONktKMjJNUlE4NTI2UVo0Tlcz?key=6979c8734cf8208ce9021ba5947673d7",
- "createdAt": "2024-12-19T06:36:30Z",
- "updatedAt": "2024-12-19T06:36:30Z"
- },
- "userErrors": []
- }
- }
- }
-
- 2. Add a line item to the cart
-
- mutation AddItemToCart($cartId: ID!, $lines: [CartLineInput!]!) {
- cartLinesAdd(cartId: $cartId, lines: $lines) {
- cart {
- id
- lines(first: 10) {
- edges {
- node {
- id
- quantity
- merchandise {
- ... on ProductVariant {
- id
- title
- }
- }
- }
- }
- }
- }
- userErrors {
- field
- message
- }
- }
- }
-
- {
- "cartId": "gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSkZFVDNONktKMjJNUlE4NTI2UVo0Tlcz?key=6979c8734cf8208ce9021ba5947673d7",
- "lines": [
- {
- "quantity": 2,
- "merchandiseId": "gid://shopify/ProductVariant/42372238213203"
- }
- ]
- }
-
- response:
- {
- "data": {
- "cartLinesAdd": {
- "cart": {
- "id": "gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSkZFVDNONktKMjJNUlE4NTI2UVo0Tlcz?key=6979c8734cf8208ce9021ba5947673d7",
- "lines": {
- "edges": [
- {
- "node": {
- "id": "gid://shopify/CartLine/1fef0eff-d077-4d50-8c03-36c748d245a3?cart=Z2NwLXVzLWVhc3QxOjAxSkZFVDNONktKMjJNUlE4NTI2UVo0Tlcz",
- "quantity": 2,
- "merchandise": {
- "id": "gid://shopify/ProductVariant/42372238213203",
- "title": "M"
- }
- }
- }
- ]
- }
- },
- "userErrors": []
- }
- }
- }
-
- 3. Get back the cart
- query GetCart($cartId: ID!) {
- cart(id: $cartId) {
- id
- createdAt
- updatedAt
- lines(first: 10) {
- edges {
- node {
- id
- quantity
- merchandise {
- ... on ProductVariant {
- id
- title
- }
- }
- }
- }
- }
- }
- }
-
- {
- "cartId": "gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSkZFVDNONktKMjJNUlE4NTI2UVo0Tlcz?key=6979c8734cf8208ce9021ba5947673d7"
- }
-
- response:
- {
- "data": {
- "cart": {
- "id": "gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSkZFVDNONktKMjJNUlE4NTI2UVo0Tlcz?key=6979c8734cf8208ce9021ba5947673d7",
- "createdAt": "2024-12-19T06:36:30Z",
- "updatedAt": "2024-12-19T06:49:00Z",
- "lines": {
- "edges": [
- {
- "node": {
- "id": "gid://shopify/CartLine/1fef0eff-d077-4d50-8c03-36c748d245a3?cart=Z2NwLXVzLWVhc3QxOjAxSkZFVDNONktKMjJNUlE4NTI2UVo0Tlcz",
- "quantity": 2,
- "merchandise": {
- "id": "gid://shopify/ProductVariant/42372238213203",
- "title": "M"
- }
- }
- }
- ]
- }
- }
- }
- }
-
- 4. Update the item quantiy based on cart line
-
- mutation {
- cartLinesUpdate(
- cartId: "gid://shopify/Cart/Z2NwLXVzLWV4YW1wbGU6MDEyMzQ1Njc4OTAxMjM0NTY3ODkw?key=examplekey1234567890"
- lines: {
- id: "gid://shopify/CartLine/1fef0eff-d077-4d50-8c03-36c748d245a3?cart=Z2NwLXVzLWVhc3QxOjAxSkZFVDNONktKMjJNUlE4NTI2UVo0Tlcz"
- quantity: 3
- }
- ) {
- cart {
- id
- lines(first: 10) {
- edges {
- node {
- id
- quantity
- merchandise {
- ... on ProductVariant {
- id
- }
- }
- }
- }
- }
- cost {
- totalAmount {
- amount
- currencyCode
- }
- subtotalAmount {
- amount
- currencyCode
- }
- totalTaxAmount {
- amount
- currencyCode
- }
- totalDutyAmount {
- amount
- currencyCode
- }
- }
- }
- }
- }
-
- 5. Get cart with the total price
-
- query GetCart($cartId: ID!) {
- cart(id: $cartId) {
- id
- createdAt
-
- cost {
- subtotalAmount {
- amount
- currencyCode
- }
- totalAmount {
- amount
- currencyCode
- }
- totalTaxAmount {
- amount
- currencyCode
- }
- }
- lines(first: 10) {
- edges {
- node {
- id
- quantity
- merchandise {
- ... on ProductVariant {
- id
- title
- price {
- amount
- currencyCode
- }
- product {
- title
- }
- }
- }
- }
- }
- }
- }
- }
-
- {
- "cartId": "gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSkZFVDNONktKMjJNUlE4NTI2UVo0Tlcz?key=6979c8734cf8208ce9021ba5947673d7"
- }
-
- response:
- {
- "data": {
- "cart": {
- "id": "gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSkZFVDNONktKMjJNUlE4NTI2UVo0Tlcz?key=6979c8734cf8208ce9021ba5947673d7",
- "createdAt": "2024-12-19T06:36:30Z",
- "cost": {
- "subtotalAmount": {
- "amount": "1750.0",
- "currencyCode": "MYR"
- },
- "totalAmount": {
- "amount": "1925.0",
- "currencyCode": "MYR"
- },
- "totalTaxAmount": {
- "amount": "175.0",
- "currencyCode": "MYR"
- }
- },
- "lines": {
- "edges": [
- {
- "node": {
- "id": "gid://shopify/CartLine/1fef0eff-d077-4d50-8c03-36c748d245a3?cart=Z2NwLXVzLWVhc3QxOjAxSkZFVDNONktKMjJNUlE4NTI2UVo0Tlcz",
- "quantity": 5,
- "merchandise": {
- "id": "gid://shopify/ProductVariant/42372238213203",
- "title": "M",
- "price": {
- "amount": "350.0",
- "currencyCode": "MYR"
- },
- "product": {
- "title": "KEMBOJA IN BEIGE"
- }
- }
- }
- }
- ]
- }
- }
- }
- }
-
- 6. Generate checkout url
-
- query checkoutURL {
- cart(id: "gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSkZFVDNONktKMjJNUlE4NTI2UVo0Tlcz?key=6979c8734cf8208ce9021ba5947673d7") {
- checkoutUrl
- }
- }
-
- response:
- {
- "data": {
- "cart": {
- "checkoutUrl": "https://amberdevstore.myshopify.com/cart/c/Z2NwLXVzLWVhc3QxOjAxSkZFVDNONktKMjJNUlE4NTI2UVo0Tlcz?key=6979c8734cf8208ce9021ba5947673d7"
- }
- }
- }
|