코스모스팜 정기결제와 우커머스를 연동하고자 합니다.

1. 정확한 제품 또는 플러그인 이름

코스모스팜 회원관리 최신버전

우커머스 최신버전

 

2. 상세 내용

게시글 하단에 작성한 코드를 적용하여 코스모스팜 정기결제 시 자동으로 관련된 우커머스 주문이 생성되도록 했습니다.

반대로 정기결제 취소 시에도 관련된 우커머스 주문이 자동으로 취소되도록 했습니다.

취소할 때야 프로그램 단에서 작동되어서 이상이 없는데,
정기결제를 실행할 때 관리자가 정기결제 주문을 생성하면 잘 작동하는 코드가
고객이 프론트엔드에서 정기결제를 결제하면 오류가 납니다.

오류 증상은 고객이 정기결제 페이지에서 결제하기 버튼을 눌러도 기다려달라는 문구만 뜰 뿐 더 이상 진행이 되지 않습니다.
이후 관리자 페이지에서 확인해보면 해당 정기결제 주문은 잘 생성되어 있지만, 우커머스 주문이 생성되지 않는 문제가 발생합니다.
관리자가 주문을 생성하면 괜찮은데 왜 고객이 결제하면 에러가 나는지 모르겠습니다.

cosmosfarm_members_subscription_again_success 또한 프로그램 단에서 작동되기에 아무 이상 없이 잘 작동합니다.

cosmosfarm_members_subscription_request_pay 훅만 고객이 정기결제 시 문제가 발생합니다.

 

<오류 로그>

Fatal error: Uncaught Error: Call to a member function get_customer_id() on bool in /home/835504.cloudwaysapps.com/mjrfqsgxvx/public_html/wp-content/plugins/woocommerce/includes/wc-user-functions.php:291

Notice: 함수 wc_get_product이(가) <strong>바르지 않게</strong> 호출됐습니다. woocommerce_init, woocommerce_after_register_taxonomy 및 woocommerce_after_register_post_type 작업을 완료하기 전까지 wc_get_product 작업을 호출할 수 없습니다. Backtrace: require('wp-blog-header.php'), require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), do_action('init'), WP_Hook->do_action, WP_Hook->apply_filters, cosmosfarm_members_init, Cosmosfarm_Members_Controller->__construct, Cosmosfarm_Members_Controller->subscription_request_pay, Cosmosfarm_Members_Controller->subscription_request_pay_order, do_action('cosmosfarm_members_subscription_request_pay'), WP_Hook->do_action, WP_Hook->apply_filters, create_order_for_product, wc_get_product, wc_doing_it_wrong 더 자세한 정보는 <a href="https://wordpress.org/support/article/debugging-in-wordpress/">워드프레스 디버깅하기</a>를 보세요. (이 메세지는 버전 3.9에서 추가되었습니다.) in /home/835504.cloudwaysapps.com/mjrfqsgxvx/public_html/wp-includes/functions.php on line 5835

 

<작성한 코드>

//코스모스팜 정기결제 시 자동으로 관련 우커머스 주문 생성
add_action( 'cosmosfarm_members_subscription_request_pay', 'create_order_for_product', 5, 3 );
add_action( 'cosmosfarm_members_subscription_again_success', 'create_order_for_product', 5, 3 );
function create_order_for_product( $order, $product, $custom_data ) {
	// Get the customer
	$user = $order->User();
	
	// Set the product ID and quantity
	$product_id = $product->ID();
	$wc_product_id = 0;
	$quantity = 1;
	
	// Set the WC product ID based on the $product_id value
	if ( $product_id == '948' ) {
		$wc_product_id = 1133;
	}/* else if ( $product_id == '' ) {
		$wc_product_id = ;
	} else if ( $product_id == '' ) {
		$wc_product_id = ;
	}*/
	
	// If a valid WC product ID was set, create a new order
	if ( $wc_product_id ) {
		// Set the billing and shipping information
		$billing_address = array(
			'first_name' => $user->first_name,
			'last_name' => $user->last_name,
			'email' => $user->user_email,
			'country' => 'KR'
		);
		$shipping_address = array(
			'first_name' => $user->first_name,
			'last_name' => $user->last_name,
			'country' => 'KR'
		);
		
		// Create a new order for the product
		$wc_order = wc_create_order();
		$wc_product = wc_get_product( $wc_product_id );
		$wc_order->add_product( $wc_product, $quantity );
		$wc_order->set_billing_address( $billing_address );
		$wc_order->set_shipping_address( $shipping_address );
		$wc_order->calculate_totals();

		// Set the customer information
		$wc_order->set_customer_id( $user->ID );
		$wc_order->set_customer_note( '이 주문은 정기결제에 의해 자동으로 생성되었으며, 단순 기록 목적일 뿐 실제 정기결제에 대한 내역이 아닙니다.' );

		// Set the payment method and process the payment
		$payment_method = 'check';
		$wc_order->set_payment_method( $payment_method );
		$wc_order->payment_complete();
		$wc_order->update_status( 'completed' );
	}
}




//정기결제 취소 시 연결된 우커머스 주문도 취소처리
add_action( 'cosmosfarm_members_subscription_order_status_update', 'cancel_latest_order', 10, 2 );
function cancel_latest_order( $order, $status ) {
	if ( $status == 'cancelled' ) {
		// Get the customer
		$user = $order->User();
		
		// Get the product ID of the subscription order
		$product_id = $order->product_id();
		
		// Set the WC product ID based on the product ID of the subscription order
		if ( $product_id == '948' ) {
			$wc_product_id = 1133;
		}
		
		// If a valid WC product ID was set, cancel the latest order for that product
		if ( $wc_product_id ) {
			// Get the latest order for the user that contains the WC product ID
			$orders = wc_get_orders( array(
				'customer_id' => $user->ID,
				'limit'       => 1,
				'orderby'     => 'date',
				'order'       => 'DESC',
				'product'     => $wc_product_id,
			) );
			
			if ( ! empty( $orders ) ) {
				$latest_order = reset( $orders );
				
				// Cancel the latest order
				$latest_order->update_status( 'cancelled' );
			}
		}
	}
}

 

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