<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
	<channel>
				<title><![CDATA[코스모스팜 정기결제와 우커머스를 연동하고자 합니다.]]></title>
		<link>https://www.cosmosfarm.com/threads/rss/document/62467</link>
		<description><![CDATA[<p><strong>1. 정확한 제품 또는 플러그인 이름</strong></p>

<p>코스모스팜 회원관리 최신버전</p>

<p>우커머스 최신버전</p>

<p> </p>

<p><strong>2. 상세 내용</strong></p>

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

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

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

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

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

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

<p> </p>

<p>&lt;오류 로그&gt;</p>

<p>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</p>

<p>Notice: 함수 wc_get_product이(가) &lt;strong&gt;바르지 않게&lt;/strong&gt; 호출됐습니다. 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-&gt;do_action, WP_Hook-&gt;apply_filters, cosmosfarm_members_init, Cosmosfarm_Members_Controller-&gt;__construct, Cosmosfarm_Members_Controller-&gt;subscription_request_pay, Cosmosfarm_Members_Controller-&gt;subscription_request_pay_order, do_action('cosmosfarm_members_subscription_request_pay'), WP_Hook-&gt;do_action, WP_Hook-&gt;apply_filters, create_order_for_product, wc_get_product, wc_doing_it_wrong 더 자세한 정보는 &lt;a href="https://wordpress.org/support/article/debugging-in-wordpress/"&gt;워드프레스 디버깅하기&lt;/a&gt;를 보세요. (이 메세지는 버전 3.9에서 추가되었습니다.) in /home/835504.cloudwaysapps.com/mjrfqsgxvx/public_html/wp-includes/functions.php on line 5835</p>

<p> </p>

<p>&lt;작성한 코드&gt;</p>

<pre>
<code>//코스모스팜 정기결제 시 자동으로 관련 우커머스 주문 생성
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-&gt;User();
	
	// Set the product ID and quantity
	$product_id = $product-&gt;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' =&gt; $user-&gt;first_name,
			'last_name' =&gt; $user-&gt;last_name,
			'email' =&gt; $user-&gt;user_email,
			'country' =&gt; 'KR'
		);
		$shipping_address = array(
			'first_name' =&gt; $user-&gt;first_name,
			'last_name' =&gt; $user-&gt;last_name,
			'country' =&gt; 'KR'
		);
		
		// Create a new order for the product
		$wc_order = wc_create_order();
		$wc_product = wc_get_product( $wc_product_id );
		$wc_order-&gt;add_product( $wc_product, $quantity );
		$wc_order-&gt;set_billing_address( $billing_address );
		$wc_order-&gt;set_shipping_address( $shipping_address );
		$wc_order-&gt;calculate_totals();

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

		// Set the payment method and process the payment
		$payment_method = 'check';
		$wc_order-&gt;set_payment_method( $payment_method );
		$wc_order-&gt;payment_complete();
		$wc_order-&gt;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-&gt;User();
		
		// Get the product ID of the subscription order
		$product_id = $order-&gt;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' =&gt; $user-&gt;ID,
				'limit'       =&gt; 1,
				'orderby'     =&gt; 'date',
				'order'       =&gt; 'DESC',
				'product'     =&gt; $wc_product_id,
			) );
			
			if ( ! empty( $orders ) ) {
				$latest_order = reset( $orders );
				
				// Cancel the latest order
				$latest_order-&gt;update_status( 'cancelled' );
			}
		}
	}
}</code></pre>

<p> </p>]]></description>
		<copyright>Copyright 2026, 코스모스팜</copyright>
				<item>
			<title><![CDATA[안녕하세요~^^

init 액션에 입력된 함수의 순서 중

우커머스에서 입력되는 함수보다 회원관리 플러그...]]></title>
			<link>https://www.cosmosfarm.com/threads/document/62547</link>
			<description><![CDATA[<p>안녕하세요~^^</p>

<p>init 액션에 입력된 함수의 순서 중</p>

<p>우커머스에서 입력되는 함수보다 회원관리 플러그인의 함수가</p>

<p>먼저 실행되어 발생하는 오류입니다.</p>

<p> </p>

<p>plugins/cosmosfarm-members/cosmosfarm-members.php 파일에서</p>

<p>아래의 코드를 찾아</p>

<pre>
<code>add_action('init', 'cosmosfarm_members_init', 5);</code></pre>

<p>아래의 코드로 변경한 후 동작이 되는지 확인해보시겠어요?</p>

<pre>
<code>add_action('init', 'cosmosfarm_members_init', 10);</code></pre>

<p>고맙습니다.</p>]]></description>
			<author>스레드봇</author>
			<pubDate>Mon, 02 Jan 2023 01:13:20 +0000</pubDate>
			<category>우커머스(WooCommerce)</category>
		</item>
			</channel>
</rss>