실제 가격에서 일정 금액을 더해서 저장할 수 있는 방법이 있을까요?

매출이 오르면 내리는 수수료! 지금 수수료센터에서 전자결제(PG)수수료 비교견적 신청해 보세요!
실제 가격에서 일정 금액을 더해서 저장할 수 있는 방법이 있을까요?

QA

실제 가격에서 일정 금액을 더해서 저장할 수 있는 방법이 있을까요?

본문

1982257987_1717565734.8551.png

상품 상세페이지 총 금액(164,000원)에서 배송일에 따라 가격을 더하고(1~10만원) 있습니다.

문제는 shop.js에서 price_calculate(인자값)으로 수정하면 상세페이지에서 총 금액은 원하는대로 변하지만

말그대로 겉만 보여주고 실제적으로 주문서나 장바구니로 이동했을 때는 +가격이 안되고 그대로 164,000원입니다.

주문서나 장바구니도 가격이 제가 원하는대로 수정이 되어 있으려면

price, qty, total 이걸 특정 페이지에서 변경해야될 듯 한데 어디서 해야될까요?

price_calculate()에서 price, qty, total는 수정해봤자, 장바구니나 주문서에는 가격이 적용되지 않습니다.

 

1982257987_1717565779.4239.png

이 질문에 댓글 쓰기 :

답변 1

안녕하세요.

아래의 내용을 한번 참고해 보시겠어요?

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>상품 상세 페이지</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <!-- 상품 상세 정보 -->
    <div>
        <input type="hidden" id="it_price" value="164000">
        <!-- 가격 계산 -->
        <div id="price_calculator">
            <input type="hidden" name="ct_qty" value="1">
            <input type="hidden" class="io_price" value="10000">
            <input type="hidden" name="io_type" value="0">
        </div>
        <!-- 총 금액 -->
        <div id="sit_tot_price">
            <span>총 금액 :</span><strong>164,000</strong> 원
        </div>
    </div>

    <script>
        function price_calculate() {
            var it_price = parseInt($("#it_price").val());

            if (isNaN(it_price)) return;

            var $el_prc = $("input.io_price");
            var $el_qty = $("input[name=ct_qty]");
            var $el_type = $("input[name=io_type]");
            var price, type, qty, total = 0;

            $el_prc.each(function(index) {
                price = parseInt($(this).val());
                qty = parseInt($el_qty.eq(index).val());
                type = $el_type.eq(index).val();

                if (type == "0") { // 선택옵션
                    total += (it_price + price) * qty;
                } else { // 추가옵션
                    total += price * qty;
                }
            });

            $("#sit_tot_price").empty().html("<span>총 금액 :</span><strong>" + number_format(String(total)) + "</strong> 원");
            $("#sit_tot_price").trigger("price_calculate", [total]);

            // AJAX로 서버에 변경된 금액 전송
            $.ajax({
                url: 'update_price.php',
                type: 'POST',
                data: { total_price: total },
                success: function(response) {
                    console.log("Price updated successfully");
                },
                error: function(xhr, status, error) {
                    console.error("Error updating price:", error);
                }
            });
        }

        function number_format(number) {
            return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        }

        // 페이지 로드 시 가격 계산 함수 호출
        $(document).ready(function() {
            price_calculate();
        });
    </script>
</body>
</html>

 

 

<?php
// update_price.php 파일

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    session_start();
    $_SESSION['total_price'] = $_POST['total_price'];
}
?>

 

 

 

<?php
// 장바구니 또는 주문서 페이지

session_start();
if (isset($_SESSION['total_price'])) {
    $total_price = $_SESSION['total_price'];
    echo "총 금액: " . number_format($total_price) . " 원";
} else {
    echo "총 금액을 계산할 수 없습니다.";
}

function number_format($number) {
    return number_format($number);
}
?>

 

답변을 작성하시기 전에 로그인 해주세요.
전체 0
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT