CartJS - 범위 지정됨

  • 릴리스 버전: Yokohama
  • 업데이트 날짜 2025년 01월 30일
  • 읽기23분
  • CartJS API는 사용자의 장바구니에 액세스할 수 있는 메서드를 제공합니다.

    이 API는 sn_sc 네임스페이스에서 실행됩니다. CartJS API에 접근하려면 기본적으로 활성화되는 서비스 카탈로그 범위가 지정된 API 플러그인(ID: com.glideapp.servicecatalog.scoped.api)이 필요합니다.

    CartJS - CartJS(문자열 cartName)

    현재 로그인한 사용자에 대해 정의된 카트 이름으로 CartJS 클래스의 인스턴스를 만듭니다.

    표 1. 매개변수
    이름 유형 설명
    cartName 문자열 옵션입니다. 현재 로그인한 사용자에 대해 정의된 카트의 이름입니다.
    var cart = new sn_sc.CartJS(cart1);

    CartJS - addToCart(맵 요청)

    카탈로그 항목에 대한 요청을 현재 카트에 추가합니다.

    표 2. 매개변수
    이름 유형 설명
    요청 지도 카트에 추가할 카탈로그 항목의 세부 정보를 포함하는 JSON 객체입니다.

    요청 객체의 구조는 다음과 같습니다.

    {
    'sysparm_id': item_id,
    'sysparm_quantity': item_quantity,
    'variables':{
    'var_name': 'var_value',
    ...
    }
    }
    • item_id: 카트에 추가할 항목의 sys_id
    • item_quantity: 추가할 항목 수입니다. 기본값은 1입니다.
    • var_name: 질문의 이름입니다.
    • var_value: 대답의 값입니다(표시 값이 아님).
    표 3. 반환
    유형 설명
    JSON 현재 카트의 구조입니다.
    {
    'subtotal': value,
    'items':[
    {
    itemName:'',
    quantity:'', 
    price:'', 
    recurring_price:''
    } ...]
    }

    이 예는 카트에 여러 항목을 추가하는 방법을 보여줍니다. var cart = new sn_sc. 카트JS(); 루프 안에 있습니다. sn_sc 전화해야 합니다 . CartJS() 를 여러 번 사용하여 장바구니에 여러 항목을 추가할 수 있습니다.

    for (i = 0; i < 2; i++) { 
    var cart = new sn_sc.CartJS();
    var item =
    {
    'sysparm_id': '0d08837237153000158bbfc8bcbe5d02',
    'sysparm_quantity': '1',
    'variables':{
    'carrier': 'at_and_t_mobility',
    'data_plan': '500MB',
    'duration': 'eighteen_months',
    'color': 'slate',
    'storage': 'sixtyfour' 
    }};
    var cartDetails = cart.addToCart(item);
    gs.info(JSON.stringify(cartDetails));
    }

    출력

    //The cart after the first item is added
    {
      "cart_id": "c0f5828a1b476010593876a61a4bcb71",
      "subtotal": "$599.99",
      "items": [
        {
          "catalog_item_id": "0d08837237153000158bbfc8bcbe5d02",
          "quantity": "1",
          "localized_price": "$599.99",
          "price": "$599.99",
          "recurring_frequency": "Monthly",
          "localized_recurring_price": "$30.00",
          "recurring_price": "$29.00",
          "item_name": "Apple iPhone 5",
          "cart_item_id": "40f5828a1b476010593876a61a4bcb72"
        }
      ]
    }
    
    //The cart after the second item is added
    {
      "cart_id": "c0f5828a1b476010593876a61a4bcb71",
      "subtotal": "$1,199.98",
      "items": [
        {
          "catalog_item_id": "0d08837237153000158bbfc8bcbe5d02",
          "quantity": "1",
          "localized_price": "$599.99",
          "price": "$599.99",
          "recurring_frequency": "Monthly",
          "localized_recurring_price": "$30.00",
          "recurring_price": "$29.00",
          "item_name": "Apple iPhone 5",
          "cart_item_id": "40f5828a1b476010593876a61a4bcb72"
        },
        {
          "catalog_item_id": "0d08837237153000158bbfc8bcbe5d02",
          "quantity": "1",
          "localized_price": "$599.99",
          "price": "$599.99",
          "recurring_frequency": "Monthly",
          "localized_recurring_price": "$30.00",
          "recurring_price": "$29.00",
          "item_name": "Apple iPhone 5",
          "cart_item_id": "98f5828a1b476010593876a61a4bcb75"
        }
      ]
    }

    CartJS - canViewRF()

    현재 사용자에게 요청 필드를 편집하는 데 필요한 역할이 있는지 지정합니다.

    표 4. 매개변수
    이름 유형 설명
    없음
    표 5. 반환
    유형 설명
    부울 사용자에게 요청 대상 필드를 편집하는 데 필요한 역할이 있는지 여부를 나타내는 플래그입니다.

    가능한 값:

    • true: 사용자에게 필요한 역할이 있습니다.
    • false: 필요한 역할이 사용자에게 없습니다.

    데이터 유형: 부울

    이 백그라운드 스크립트 코드 예제는 현재 사용자에게 요청 대상 필드를 편집하는 데 필요한 역할이 있는지 확인하는 방법을 보여줍니다.

    gs.log(sn_sc.CartJS.canViewRF());

    응답:

    true

    CartJS - checkoutCart()

    카트 체크아웃을 수행합니다. 2단계 체크아웃이 활성화된 경우 주문 요약을 반환합니다. 2단계 체크아웃이 비활성화되면 카트가 제출되고 생성된 요청의 상세 정보가 반환됩니다.

    표 6. 매개변수
    이름 유형 설명
    없음
    표 7. 반환
    유형 설명
    JSON 2단계 체크아웃이 활성화되면 카트에 있는 항목의 요약이 반환됩니다.
    { "subtotal_price":"",
      "subtotal_recurring_frequency":"",
      "delivery_address":"",
      "special_instructions":"",
      "total_title":"",
      "requested_for_user":"System Administrator",
      "requested_for":"6816f79cc0a8016401c5a33be04be441",
      "daily": ["frequency_subtotal":"", "items":[{}, {}, ...], …],
      "monthly":["frequency_subtotal":"", "items":[{}, {}, ...], …],
      "annually":["frequency_subtotal":"", "items":[{}, {}, ...], …],
      "none":["frequency_subtotal":"", "items":[{}, {}, ...], …],
    }
    2단계 체크아웃이 비활성화된 경우:
    {
      'request_id' : "sys_id of the generated request",
      "request_number" : "Number of the generated request"
    }
    var cart = new sn_sc.CartJS();
    var checkoutInfo = cart.checkoutCart();
    gs.info(checkoutInfo);
    

    출력

    If two step checkout enabled:
    {"subtotal_price":"$2,748.49","subtotal_recurring_frequency":"","delivery_address":"","special_instructions":"","total_title":"Total","requested_for_user":"System Administrator","requested_for":"6816f79cc0a8016401c5a33be04be441","weekly":{"subtotal_price":"$399.50","subtotal_recurring_frequency":"Weekly","subtotal_recurring_price":"$0.00","total_title":"Total","items":[{"catalog_item_id":"e90a0f7237153000158bbfc8bcbe5d7f","variables":{},"quantity":"1","localized_price":"$399.50","price":"$399.50","recurring_frequency":"Weekly","localized_recurring_price":"$0.00","recurring_price":"$0.00","item_name":"Brother Network-Ready Color Laser Printer","cart_item_id":"f3f5c9b3c322320076173b0ac3d3ae00","delivery_time":"2 Days"}],"show_subtotal_price":"true","subtotal_title":"Subtotal"},"show_subtotal_price":"true","cart_id":"251c0562c326320076173b0ac3d3aeb4","subtotal_recurring_price":"$131.00","monthly":{"subtotal_price":"$849.99","subtotal_recurring_frequency":"Monthly","subtotal_recurring_price":"$31.00","total_title":"Total","items":[{"catalog_item_id":"d82ea08510247200964f77ffeec6c4ee","variables":{"Monthly data allowance":"500MB","Storage":"64GB","Color":"Space Gray","Is this a replacement for a lost or broken iPhone?":"No","What was the original phone number?":""},"quantity":"1","localized_price":"$799.99","price":"$849.99","recurring_frequency":"Monthly","localized_recurring_price":"$30.00","recurring_price":"$31.00","item_name":"Apple iPhone 6s Plus","cart_item_id":"18c509b3c322320076173b0ac3d3aef4","delivery_time":"2 Days"}],"show_subtotal_price":"true","subtotal_title":"Subtotal"},"yearly":{"subtotal_price":"$1,499.00","subtotal_recurring_frequency":"Annually","subtotal_recurring_price":"$100.00","total_title":"Total","items":[{"catalog_item_id":"774906834fbb4200086eeed18110c737","variables":{"Adobe Photoshop":"false","Adobe Acrobat":"false","Optional Software":"","Additional software requirements":"","Eclipse IDE":"true"},"quantity":"1","localized_price":"$1,499.00","price":"$1,499.00","recurring_frequency":"Annually","localized_recurring_price":"$100.00","recurring_price":"$100.00","item_name":"Developer Laptop (Mac)","cart_item_id":"6306c9b3c322320076173b0ac3d3ae89","delivery_time":"5 Days"}],"show_subtotal_price":"true","subtotal_title":"Subtotal"},"subtotal_title":"Subtotal"}
    If two step checkout disabled:
    {"request_number":"REQ0010001","request_id":"2f2789f3c322320076173b0ac3d3ae62"}
    

    CartJS - 비어 있음()

    현재 카트를 삭제합니다.

    표 8. 매개변수
    이름 유형 설명
    없음
    표 9. 반환
    유형 설명
    void
    var cart = new sn_sc.CartJS();
          cart.empty();

    CartJS - getCartDetails()

    카트 상세 정보를 반환합니다.

    표 10. 매개변수
    이름 유형 설명
    없음
    표 11. 반환
    유형 설명
    객체 현재 카트 상세 정보를 가리키는 객체입니다.

    예제:

    var cart=new sn_sc.CartJS();
    	console.log (cart.getCartDetails());

    출력:

    delivery_address : "Brasilia,Brasil" 
            name : "DEFAULT" 
            requested_for : "62826bf03710200044e0bfc8bcbe5df1" 
            requested_for_display_name : "Abel Tuter" 
            special_instructions : "" 
            sys_id : "c2ac090087150300318d05a888cb0bd7" 
    

    CartJS - getCartID()

    현재 카트의 ID를 반환합니다.

    표 12. 매개변수
    이름 유형 설명
    없음
    표 13. 반환
    유형 설명
    문자열 현재 카트의 Sys_id입니다.

    현재 카트의 sys_id 반환합니다.

    var cart = new sn_sc.CartJS();
    var cartId = cart.getCartID();
    gs.info(cartId);

    출력

    039c516237b1300054b6a3549dbe5dfc

    CartJS - getCartItems()

    현재 카트에 있는 항목에 대한 기록을 포함하는 GlideRecord 객체를 반환합니다.

    표 14. 매개변수
    이름 유형 설명
    없음
    표 15. 반환
    유형 설명
    GlideRecord 현재 카트에 있는 항목을 설명하는 항목 [sc_cart_item] 테이블의 기록과 필드를 포함하는 GlideRecord 객체입니다.

    현재 카트에 있는 항목의 수량 및 카탈로그 항목 필드 값을 반환합니다.

    var cart = new sn_sc.CartJS();
    
    var cartItems = cart.getCartItems();
    while (cartItems.next()) {
      gs.info(cartItems.getElement('quantity').getDisplayValue()
      + " x "
      + cartItems.getElement('cat_item').getDisplayValue());
    }

    출력

    
    1 x Apple iPhone 6s Plus
    1 x Apple iPhone 6s
    1 x Apple MacBook Pro 15"

    CartJS - getDeliveryAddress()

    현재 카트의 제공 주소를 반환합니다.

    표 16. 매개변수
    이름 유형 설명
    없음
    표 17. 반환
    유형 설명
    문자열 현재 카트의 제공 주소입니다.

    현재 카트의 제공 주소를 설정한 다음 반환합니다.

    var cart = new sn_sc.CartJS();
    cart.setDeliveryAddress("Brasilia, Brasil");
    var deliveryAddress = cart.getDeliveryAddress();
    gs.info(deliveryAddress);

    출력

    Brasilia, Brasil

    CartJS - getRequestedFor()

    카트를 요청한 사용자의 sys_id 반환합니다.

    표 18. 매개변수
    이름 유형 설명
    없음
    표 19. 반환
    유형 설명
    문자열 카트를 요청한 사용자 기록의 Sys_id입니다.

    테이블: 사용자 [sys_user]

    카트와 연결된 사용자 sys_id 반환합니다.

    var cart = new sn_sc.CartJS();
    cart.setRequestedFor("039c516237b1300054b6a3549dbe5dfc")
    var userId = cart.getRequestedFor();
    gs.info(userId);

    출력:

    039c516237b1300054b6a3549dbe5dfc

    CartJS - getRequestedForDisplayName()

    현재 카트를 요청한 사용자의 이름을 반환합니다.

    표 20. 매개변수
    이름 유형 설명
    없음
    표 21. 반환
    유형 설명
    문자열 현재 카트를 요청한 사용자의 이름입니다.

    테이블: 사용자 [sys_user]

    카트와 연결된 사용자의 이름을 반환합니다.

    var cart = new sn_sc.CartJS();
    var userName = cart.getRequestedForDisplayName();
    gs.info(userName);

    출력:

    Abel Tuter

    CartJS - getSpecialInstructions()

    현재 카트에 대한 특별 지침을 반환합니다.

    표 22. 매개변수
    이름 유형 설명
    없음
    표 23. 반환
    유형 설명
    문자열 현재 카트에 대한 특수 지침입니다.

    이 예는 현재 카트에 대한 특수 지침을 설정하고 검색하는 방법을 보여줍니다.

    var cart = new sn_sc.CartJS();
    cart.setSpecialInstructions("Delivery before 8 AM.");
    var specInstruction = cart.getSpecialInstructions();
    gs.info(specInstruction);

    출력

    Delivery before 8 AM.

    CartJS - orderNow(맵 요청)

    단일 항목을 주문합니다. 2단계 체크아웃이 활성화된 경우 메서드는 지정된 항목을 카트에 추가하고 카트의 sys_id 반환합니다. 2단계 체크 아웃이 비활성화된 경우 메서드는 지정된 항목의 구매를 완료하고 생성된 요청의 sys_id 반환합니다.

    표 24. 매개변수
    이름 유형 설명
    요청 지도 주문할 카탈로그 항목의 상세 정보를 포함하는 JSON 객체입니다.
    request.delivery_address 문자열 항목을 전송할 주소입니다.

    기본값: 사용자 주소

    request.sysparm_id 문자열 필수 구매할 항목의 sys_id입니다.
    request.special_instructions 문자열 주문을 처리할 때 따라야 할 지침입니다.
    request.sysparm_quantity 문자열 구매할 지정된 항목의 수량입니다.

    기본값: 1

    request.sysparm_requested_for 문자열 항목을 요청한 사용자의 sys_id.

    기본값: 세션 사용자

    request.variables 배열 항목과 관련된 질문 및 고객 답변입니다.
    request.variables.var_name 문자열 질문의 이름입니다.
    request.variables.var_value 문자열 관련 질문에 대한 고객의 응답입니다.
    표 25. 반환
    유형 설명
    JSON 키/값 쌍 2단계 체크 아웃이 활성화된 경우 출력:
    {
      'cart_id' : '<sys_id of the cart to which the items were added>'
    }

    2단계 체크 아웃이 비활성화된 경우 출력:

    {
      'request_id' : '<sys_id of the generated request>',
      'request_number' : '<Number of the generated request>'
    }
    
    var cart = new sn_sc.CartJS();
    var request =
    {
      'sysparm_id': '0d08837237153000158bbfc8bcbe5d02',
      'sysparm_quantity': '1',
      'variables':{
        'carrier': 'at_and_t_mobility',
        'data_plan': '500MB',
        'duration': 'eighteen_months',
        'color': 'slate',
        'storage': 'sixtyfour'
      }
    }
    var cartDetails = cart.orderNow(request);
    gs.info(cartDetails);

    출력

    
    // If two-step checkout is enabled:
    {"cart_id":"55384df3c322320076173b0ac3d3aec5"}
    
    // If two-step checkout is disabled:
    {"request_id":"4c690137c322320076173b0ac3d3ae03", "request_number": "REQ0010003"}

    CartJS - setDeliveryAddress(문자열 주소)

    현재 카트의 제공 주소를 설정합니다.

    표 26. 매개변수
    이름 유형 설명
    address 문자열 현재 카트의 제공 주소입니다.
    표 27. 반환
    유형 설명
    void
    
          var cart = new sn_sc.CartJS();
          cart.setDeliveryAddress("Brasilia, Brasil"); 
          

    CartJS - setRequestedFor(문자열 사용자)

    카트를 요청한 사용자의 sys_user 기록에서 sys_id 설정합니다.

    표 28. 매개변수
    이름 유형 설명
    사용자 문자열 카트를 요청한 사용자의 sys_user 기록에서 설정할 sys_id입니다.
    표 29. 반환
    유형 설명
    void
    
    var cart = new sn_sc.CartJS();
    cart.setRequestedFor("039c516237b1300054b6a3549dbe5dfc")
    

    CartJS - setSpecialInstructions(String specialInstructions)

    현재 카트에 대한 특별 지침을 설정합니다.

    표 30. 매개변수
    이름 유형 설명
    특수 지침 문자열 현재 카트에 대한 특수 지침입니다.
    표 31. 반환
    유형 설명
    void
    
          var cart = new sn_sc.CartJS();
          cart.setSpecialInstructions("Delivery before 8 AM."); 
       

    CartJS - submitOrder(맵 요청)

    매개변수에서 request 특별 지침, 요청 대상 및 제공 주소를 업데이트하고 카트 체크아웃을 수행합니다. 이 API를 사용하여 카트에 언급된 매개변수를 수정하고 카트 체크아웃을 동시에 수행합니다. 객체에서 request 누락된 매개변수는 기본값을 갖습니다.

    표 32. 매개변수
    이름 유형 설명
    요청 지도 제출할 카트의 세부 정보를 포함하는 JSON 객체입니다.
    {
      'special_instructions': "String",
      'requested_for': '"String",
      'delivery_address': "String"
    }
    request.special_instructions 문자열 주문에 포함할 특별 지침입니다.
    request.requested_for 문자열 주문을 요청한 사용자의 Sys_id입니다.

    기본값: 현재 사용자입니다.

    테이블: 사용자 [sys_user]

    request.delivery_address 문자열 주문의 배송 주소입니다.
    표 33. 반환
    유형 설명
    JSON 카트의 구조입니다.
    {
      'request_id' : 'sys_id of the generated Request',
      'request_number' : 'Number of the generated Request'
    }

    다음 코드 예제에서는 이 메서드를 호출하는 방법을 보여 줍니다.

    var cart = new sn_sc.CartJS();
    var request =
    {
      'special_instructions' : 'Delivery only in working hours',
      'requested_for' : '62826bf03710200044e0bfc8bcbe5df1',
      'delivery_address' : "Brasilia, Brasil",
    };
    var requestDetails = cart.submitOrder(request);
    gs.info(JSON.stringify(requestDetails));

    출력:

    {"request_id":"6eed229047801200e0ef563dbb9a71c2", "request_number": "REQ0000001"}

    CartJS - updateItem(맵 요청, 문자열 cart_item_id)

    카트의 항목을 업데이트합니다.

    표 34. 매개변수
    이름 유형 설명
    요청 지도 업데이트할 카탈로그 항목의 세부 정보를 포함하는 JSON 객체입니다.

    요청 객체의 구조는 다음과 같습니다.

    {
        'sysparm_quantity' : item_quantity,
      'sysparm_requested_for' : requested_for,
      'variables' : {
        'var_name' : 'var_value',
        ...
      }
    }
    • item_quantity: 추가할 항목 수입니다. 기본값은 1입니다.
    • var_name: 질문의 이름입니다.
    • var_value: 대답의 값입니다(표시 값이 아님).
    cart_item_id 문자열 수정할 카트 항목의 sys_id입니다.
    표 35. 반환
    유형 설명
    JSON 카트의 상세 정보입니다.
    {
      'subtotal': value,
      'items':[
        {
        itemName:'',
        quantity:'',
        price:'',
        recurring_price:''
        }
        ...],
      ...
    }
    var cart = new sn_sc.CartJS();
    var request =
    {
      'sysparm_quantity': '1',
      'variables':{
        'carrier': 'at_and_t_mobility',
        'data_plan': '500MB',
        'duration': 'eighteen_months',
        'color': 'slate',
        'storage': 'sixtyfour'
      }
    };
    var cart_item_id = "4d69b672c322320076173b0ac3d3ae79";
    var cartDetails = cart.updateItem(request, cart_item_id);
    gs.info(cartDetails);
    

    출력

    
    { "cart_id":"35ec9e8947a13200e0ef563dbb9a7109", "items":[ { "cart_item_id":"35ec9e8947a13200e0ef563dbb9a710a", "catalog_item_id":"0d08837237153000158bbfc8bcbe5d02", "item_name":"Apple iPhone 5", "localized_price":"$799.99", "localized_recurring_price":"$30.00", "price":"$799.99", "quantity":"1", "recurring_frequency":"Monthly", "recurring_price":"$29.00" } ] "subtotal":"$799.99" }