# Cart 기능

Hotopay에는 카트 기능이 있습니다.

<figure><img src="/files/aaAJcVr0RRQuz1SFJhzz" alt=""><figcaption></figcaption></figure>

이 기능을 일반 게시판에서 사용하기 위한 문서입니다

### 상품 추가 예시

게시판 스킨(게시글 조회 페이지)에 넣을 예시입니다.

<figure><img src="/files/kXwDrT9dtRAAddoWCSFF" alt=""><figcaption><p>예시</p></figcaption></figure>

```
{@
  $oHotopayModel = HotopayModel::getInstance();
  $oProduct = $oHotopayModel->getProductByDocumentSrl($oDocument->get('document_srl'));
  $options = $oProduct->product_option;
}

<select id="option_srl">
  <!--@foreach($options as $option)-->
  {@
      $sub_price = $option->price - $oProduct->product_sale_price;
      $sub_price = $sub_price + round($sub_price * ($oProduct->tax_rate / 100));
      if($sub_price > 0) $sub_price = "(+".number_format($sub_price)."원)";
      else if($sub_price < 0) $sub_price = "(".number_format($sub_price)."원)";
      else $sub_price = "";

      $stock_text = "";
      if($option->infinity_stock != 'Y'):
          if($option->stock < 1) $stock_text = " (품절)";
          else $stock_text = " (".$option->stock."개)";
      endif;
  }
  <option value="{$option->option_srl}" data-hotopay-option-price="{$option->price + round($option->price * ($product->tax_rate / 100))}" disabled|cond="$option->infinity_stock != 'Y' && $option->stock < 1">{$option->title} {$sub_price}{$stock_text}</option>
  <!--@endforeach-->
</select>
<p><input type="number" id="quantity" value="1"></p>
<p><button class="addCart" data-product-srl="{$oProduct->product_srl}"><i class="ico cart"></i> 카트에 담기</button></p>

<script>
jQuery(document).ready(function($) {
  // addCart
  $('.addCart').click(function() {
    var product_srl = $(this).data('product-srl');
    var option_srl = $('#option_srl').val();
    var quantity = $('#quantity').val();

    console.log(product_srl, option_srl, quantity);
    $.ajax({
      url: '/hotopay/cart/add',
      type: 'POST',
      data: {
        'product_srl': product_srl,
        'option_srl': option_srl,
        'quantity': quantity
      },
      success: function(data) {
        if (data.error == 0)
        {
          alert('장바구니에 추가되었습니다.');
          window.location = '/hotopay/cart';
        }
        else
        {
          if (data.message == '이미 장바구니에 담겨있는 상품입니다.')
          {
            window.location = '/hotopay/cart';
            return;
          }

          alert(data.message);
        }
      }
    });
  });
});
</script>
```

***

## API 문서

### 카트에 아이템 추가

`/hotopay/cart/add` : 카트에 추가&#x20;

Request Type: `application/json`

```
{
    "product_srl": Hotopay 상품 번호 (int)
    "option_srl": Hotopay 옵션 번호 (int)
    "quantity": 수량 (int)
}
```

Response Type: `application/json`

```
{
    "error": 0,
    "message": "장바구니에 추가되었습니다."
}
```

<table><thead><tr><th width="297.5">메세지 종류</th><th>설명</th></tr></thead><tbody><tr><td>장바구니에 추가되었습니다.</td><td>장바구니에 아이템이 정상적으로 추가됨.</td></tr><tr><td>필수 정보가 없습니다.</td><td><code>product_srl</code>, <code>option_srl</code>, <code>quantity</code> 중 하나 이상의 값이 누락됨.</td></tr><tr><td>로그인이 필요합니다.</td><td>로그인이 되지 않은 상태로 아이템을 추가하려함.</td></tr><tr><td>장바구니에는 최대 n개의 상품만 담을 수 있습니다.</td><td><p>장바구니 한도를 넘어서 아이템을 추가하려고 시도함.</p><p>상향이 필요한 경우 Hotopay 설정에서 장바구니 한도를 변경할 수 있음.</p></td></tr><tr><td>상품 정보가 없습니다.</td><td><code>product_srl</code>로 정보를 찾을 수 없음.</td></tr><tr><td>옵션 정보가 없습니다.</td><td><code>option_srl</code>로 정보를 찾을 수 없음.</td></tr><tr><td>상품과 옵션이 일치하지 않습니다.</td><td>선택한 옵션이 상품의 옵션과 일치하지 않음.</td></tr><tr><td>이미 장바구니에 담겨있는 상품입니다.</td><td>이미 장바구니에서 해당 상품, 옵션 조합이 추가되어 있음.</td></tr></tbody></table>

### 카트에서 아이템 삭제

Request Type: `application/json`

```
{
    "cart_item_srl": 카트 아이템 번호 (int)
}
```

Response Type: `application/json`

```
{
    "error": 0,
    "message": "장바구니에서 삭제되었습니다."
}
```

<table><thead><tr><th width="297.5">메세지 종류</th><th>설명</th></tr></thead><tbody><tr><td>장바구니에서 삭제되었습니다.</td><td>장바구니에서 아이템을 삭제함.</td></tr><tr><td>필수 정보가 없습니다.</td><td><code>cart_item_srl</code>이 누락됨.</td></tr><tr><td>로그인이 필요합니다.</td><td>로그인이 되지 않은 상태로 아이템을 추가하려함.</td></tr></tbody></table>

### 카트에서 아이템 업데이트

Request Type: `application/json`

```
{
    "cart_item_srl": 카트 아이템 번호 (int)
    "option_srl": Hotopay 옵션 번호 (int)
    "quantity": 수량 (int)
}
```

Response Type: `application/json`

```
{
    "error": 0,
    "message": "장바구니가 수정되었습니다."
}
```

<table><thead><tr><th width="297.5">메세지 종류</th><th>설명</th></tr></thead><tbody><tr><td>장바구니가 수정되었습니다.</td><td>장바구니에서 아이템을 수정함.</td></tr><tr><td>필수 정보가 없습니다.</td><td><code>product_srl</code>, <code>option_srl</code>, <code>quantity</code> 중 하나 이상의 값이 누락됨.</td></tr><tr><td>로그인이 필요합니다.</td><td>로그인이 되지 않은 상태로 아이템을 추가하려함.</td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://develop-hotopay.potatosoft.kr/cart.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
