특정상품을 각 회원ID당 한번씩만 구매할 수 있게 중복구매방지 설정하는 방법

안녕하세요~ 우커머스 기능을 사용중입니다.

특정상품을 회원ID당 한번씩만 구매할 수 있게 중복구매방지 설정하는 방법이 있을까요? 

 

워드프레스 에러 기술지원 서비스 전문가에게 맡기세요
워드프레스 에러 기술지원 서비스 전문가에게 맡기세요
  • 안녕하세요~^^

    우커머스 플러그인에서 한 상품에 대해

    하나의 수량만 구매할 수 있게 하시려면 우커머스의 개별 판매 기능을 이용해보시겠어요?

    워드프레스 관리자 -> 상품(Products) -> 모든 상품(All Products) -> 상품 선택 -> 재고(Inventory) -> 개별 판매(Sold individually) 설정을 체크해보시겠어요?

     

    만약, 구매 자체를 중복으로 불가능하게 하시는 거라면

    아래 관련 링크 참고 부탁드립니다.

    https://www.thewordcracker.com/intermediate/prevent-repeat-purchase-in-woocommerce-in-wordpress/

    고맙습니다.

  • 안녕하세요. 답변 감사합니다. 

    /* 특정상품 중복 구매 금지 */
        function sv_disable_repeat_purchase( $purchasable, $product ) {
            /* 중복 구매를 금지할 상품의 ID */
            $non_purchasable = 17132;
    
            /* 현재 상품의 ID 체크 */
            $product_id = $product->is_type( 'variation' ) ? $product->variation_id : $product->id;
            /* 중복 구매 금지 상품의 ID와 일치하지 않는 경우 */
            if ( $non_purchasable != $product_id ) {
                return $purchasable;
            }
            /* 고객이 상품을 이전에 구매한 경우 False 반환 */
            if ( wc_customer_bought_product( get_current_user()->user_email, get_current_user_id(), $product_id ) ) {
                $purchasable = false;
            }
            /* 옵션 더블 체크: 상위 상품이 구매 불가능한 경우 옵션도 구매 불가능 */
            if ( $purchasable && $product->is_type( 'variation' ) ) {
                $purchasable = $product->parent->is_purchasable();
            }
            return $purchasable;
        }
        add_filter( 'woocommerce_variation_is_purchasable', 'sv_disable_repeat_purchase', 10, 2 );
        add_filter( 'woocommerce_is_purchasable', 'sv_disable_repeat_purchase', 10, 2 );
    
        /* 중복구매 불가능 메세지 띄우기 */
        function sv_purchase_disabled_message() {
            /* 중복 구매 금지 상품의 ID 입력 */
            $no_repeats_id = 17132;
            $no_repeats_product = wc_get_product( $no_repeats_id );
    
            /* 구매가 불가능한지를 체크하기 위해 현재 상품 정보를 가져옴 */
            global $product;
            if ( $no_repeats_product->is_type( 'variation' ) ) {
                /* 현재 상품 페이지가 구매 금지 상품의 상품 페이지가 아닌 경우 */
                if ( ! $no_repeats_product->parent->id === $product->id ) {
                    return;
                }
                /* 구매 금지 상품 페이지에 있는 경우 메시지 출력 */
                if ( wc_customer_bought_product( get_current_user()->user_email, get_current_user_id(), $no_repeats_id ) ) {
                    sv_render_variation_non_purchasable_message( $product, $no_repeats_id );
                }
            } elseif ( $no_repeats_id === $product->id ) {
                if ( wc_customer_bought_product( get_current_user()->user_email, get_current_user_id(), $no_repeats_id ) ) {
                    /* 고객에게 표시할 메시지 */
                    echo '<div class="woocommerce"><div class="woocommerce-info wc-nonpurchasable-message">이 상품을 이미 구매하셨습니다. 이 상품은 한 번만 구매 가능합니다.</div></div>';
                }
            }
        }
        add_action( 'woocommerce_single_product_summary', 'sv_purchase_disabled_message', 31 );
    
    
    

    관련링크를 참고해서 추가한 코드입니다. 수정한 부분은 상품 ID값 뿐인데 작동이 안되어서요. 

    혹시 아이디값을 제가 잘못 입력했을까요?ㅜㅜ 한번 확인부탁드립니다.

    감사합니다. 

  • 이전 댓글에 안내해드린 링크의 코드는 오래된 버전의 코드인 듯합니다.

    아래의 코드로 교체해서 확인해보시겠어요?

    /* 특정상품 중복 구매 금지 */
    function sv_disable_repeat_purchase( $purchasable, $product ) {
    	/* 중복 구매를 금지할 상품의 ID */
    	$non_purchasable = 17132;
    	
    	/* 현재 상품의 ID 체크 */
    	$product_id = $product->is_type( 'variation' ) ? $product->variation_id : $product->get_id();
    	/* 중복 구매 금지 상품의 ID와 일치하지 않는 경우 */
    	if ( $non_purchasable != $product_id ) {
    		return $purchasable;
    	}
    	/* 고객이 상품을 이전에 구매한 경우 False 반환 */
    	if ( wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $product_id ) ) {
    		$purchasable = false;
    	}
    	/* 옵션 더블 체크: 상위 상품이 구매 불가능한 경우 옵션도 구매 불가능 */
    	if ( $purchasable && $product->is_type( 'variation' ) ) {
    		$purchasable = $product->parent->is_purchasable();
    	}
    	
    	return $purchasable;
    }
    add_filter( 'woocommerce_variation_is_purchasable', 'sv_disable_repeat_purchase', 10, 2 );
    add_filter( 'woocommerce_is_purchasable', 'sv_disable_repeat_purchase', 10, 2 );
    
    /* 중복구매 불가능 메세지 띄우기 */
    function sv_purchase_disabled_message() {
    	/* 중복 구매 금지 상품의 ID 입력 */
    	$no_repeats_id = 17132;
    	$no_repeats_product = wc_get_product( $no_repeats_id );
    	
    	/* 구매가 불가능한지를 체크하기 위해 현재 상품 정보를 가져옴 */
    	global $product;
    	if ( $no_repeats_product->is_type( 'variation' ) ) {
    		/* 현재 상품 페이지가 구매 금지 상품의 상품 페이지가 아닌 경우 */
    		if ( ! $no_repeats_product->parent->id === $product->get_id() ) {
    			return;
    		}
    	} elseif ( $no_repeats_id === $product->get_id() ) {
    		echo $no_repeats_id;
    		var_dump(wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $no_repeats_id ));
    		if ( wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $no_repeats_id ) ) {
    			/* 고객에게 표시할 메시지 */
    			echo '<div class="woocommerce"><div class="woocommerce-info wc-nonpurchasable-message">이 상품을 이미 구매하셨습니다. 이 상품은 한 번만 구매 가능합니다.</div></div>';
    		}
    	}
    }
    add_action( 'woocommerce_single_product_summary', 'sv_purchase_disabled_message', 31 );

     

    그리고 상황에 따라서는 코드가 더 추가될 수도 있습니다.

    코드 수정이 어려우시다면

    프로젝트 의뢰에 상세 내용을 올려보세요.

    https://www.cosmosfarm.com/project

    고맙습니다.

  • 수정해주신 코드를 넣었는데 작동하지않습니다ㅜ

    위 이미지처럼 false를 반환하는 거 같긴한데 그냥 결제로 넘어갑니다. 

  • 이전 댓글에 안내해드린 코드 중

    아래의 코드는 지워보세요.

    echo $no_repeats_id;
    var_dump(wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $no_repeats_id ));

     

    wc_customer_bought_product 함수의 결과가 false로 표시된다면

    이미 구매한 적이 없거나 주문 상태가 처리중 또는 완료됨으로 적용되지 않은 듯합니다.

    확인해보시겠어요?

     

    그리고 커뮤니티에서는 100% 해결을 보장해드리긴 어렵습니다.

    직접 코드를 수정하기 어려우신 상황이라면

    적절한 비용을 지불하고 전문 업체 혹은 개발자에게 의뢰를 하셔야 합니다.

    고맙습니다.

  • 안내해주신 코드를 삭제했지만 적용은 되지않았습니다.ㅜ

    의뢰를 고려해보겠습니다. 도움 감사했습니다~ 

워드프레스 에러 기술지원 서비스 전문가에게 맡기세요