Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Woocommerce product not pushed #1190

Open
1 task done
cdesp opened this issue Feb 7, 2024 · 3 comments
Open
1 task done

Woocommerce product not pushed #1190

cdesp opened this issue Feb 7, 2024 · 3 comments
Assignees
Labels
needs:feedback This requires reporter feedback to better understand the request. type:bug Something isn't working.

Comments

@cdesp
Copy link

cdesp commented Feb 7, 2024

Describe the bug

If a product is sold on the master site then the remaining stock after the sale doesn't get automatically propagated to the other site.
If i edit the product myself like changing the description or whatever eerything works ok.
Is this how it is supposed to work? and if so how i can trigger the push myself every time a sale is done?

Steps to Reproduce

If a product is sold the new stock is not pushed to the other site

Screenshots, screen recording, code snippet

No response

Environment information

No response

WordPress information

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@cdesp cdesp added the type:bug Something isn't working. label Feb 7, 2024
@jeffpaul
Copy link
Member

jeffpaul commented Feb 7, 2024

@cdesp you'll likely want to review the following comments to see how to extend more deeply for Woo integrations: #1153 (comment), #1142 (comment), #1027 (comment).

@jeffpaul jeffpaul added needs:feedback This requires reporter feedback to better understand the request. Reporter Feedback labels Feb 7, 2024
@cdesp
Copy link
Author

cdesp commented Feb 8, 2024

I already saw these but they are referring to a variable product not a simple one. Should i hook to product update and then sent a command to update the remote product or this is done automatically whenever the product stock updates after an order is placed?

@cdesp
Copy link
Author

cdesp commented Feb 9, 2024

I assumed that distributor only pushes when something is changed via user interface so i managed to manually push a product using the following code.
I changed a function residing on subscriptions.php to fit my needs.

add_action( 'woocommerce_product_set_stock', 'stock_changed' );
add_action( 'woocommerce_variation_set_stock', 'stock_changed' );
function stock_changed( $product ) {
    // Do something
    // $product->get_id(); //postid
    notify_product($product->get_id());
}

/**
 * Send notifications on post update to each subscription for that post
 *
 * @param  int|WP_Post $post Post ID or WP_Post, depending on which action the method is hooked to.
 * @since  1.0
 */
function notify_product( $post_id ) {
	

	$subscriptions = get_post_meta( $post_id, 'dt_subscriptions', true );

	if ( empty( $subscriptions ) ) {
		return;
	}

	$post = get_post( $post_id );

	$update_subscriptions = false;

	foreach ( $subscriptions as $subscription_key => $subscription_id ) {
		$signature      = get_post_meta( $subscription_id, 'dt_subscription_signature', true );
		$remote_post_id = get_post_meta( $subscription_id, 'dt_subscription_remote_post_id', true );
		$target_url     = get_post_meta( $subscription_id, 'dt_subscription_target_url', true );

		if ( empty( $signature ) || empty( $remote_post_id ) || empty( $target_url ) ) {
			continue;
		}

		$post_body = [
			'post_id'   => $remote_post_id,
			'signature' => $signature,
			'post_data' => [
				'title'             => html_entity_decode( get_the_title( $post_id ), ENT_QUOTES, get_bloginfo( 'charset' ) ),
				'slug'              => $post->post_name,
				'post_type'         => $post->post_type,
				'content'           => '',//\Distributor\Utils\get_processed_content( $post->post_content ),
				'excerpt'           => $post->post_excerpt,
				'distributor_media' => \Distributor\Utils\prepare_media( $post_id ),
				'distributor_terms' => \Distributor\Utils\prepare_taxonomy_terms( $post_id, array( 'show_in_rest' => true ) ),
				'distributor_meta'  => \Distributor\Utils\prepare_meta( $post_id ),
			],
		];

		if ( \Distributor\Utils\is_using_gutenberg( $post ) ) {
			if ( \Distributor\Utils\dt_use_block_editor_for_post_type( $post->post_type ) ) {
				$post_body['post_data']['distributor_raw_content'] = $post->post_content;
			}
		}
		/**
		 * Filter the timeout used when calling `\Distributor\Subscriptions\send_notifications`
		 *
		 * @hook dt_subscription_post_timeout
		 *
		 * @param {int}     $timeout The timeout to use for the remote post. Default `5`.
		 * @param {WP_Post} $post    The post object
		 *
		 * @return {int} The timeout to use for the remote post.
		 */
		$request_timeout = apply_filters( 'dt_subscription_post_timeout', 5, $post );

		/**
		 * Filter the arguments sent to the remote server during a subscription update.
		 *
		 * @since 1.3.0
		 * @hook dt_subscription_post_args
		 *
		 * @param  {array}   $post_body The request body to send.
		 * @param  {WP_Post} $post      The WP_Post that is being pushed.
		 *
		 * @return {array} The request body to send.
		 */
		$post_body = apply_filters( 'dt_subscription_post_args', $post_body, $post );

		$post_arguments = [
			'timeout' => $request_timeout,
			'body'    => wp_json_encode( $post_body ),
			'headers' => [
				'Content-Type'          => 'application/json',
				'X-Distributor-Version' => DT_VERSION,
			],
		];

		$request = wp_remote_post(
			untrailingslashit( $target_url ) . '/wp/v2/dt_subscription/receive',
			$post_arguments
		);

		if ( ! is_wp_error( $request ) ) {
			$response_code = wp_remote_retrieve_response_code( $request );
			$headers       = wp_remote_retrieve_headers( $request );

			if ( 404 === $response_code && ! empty( $headers['X-Distributor-Post-Deleted'] ) ) {
				/**
				 * Post on receiving end has been deleted.
				 */
				unset( $subscriptions[ $subscription_key ] );

				$update_subscriptions = true;

				wp_delete_post( $subscription_id, true );
			}
		}
	}

	if ( $update_subscriptions ) {
		update_post_meta( $post_id, 'dt_subscriptions', $subscriptions );
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs:feedback This requires reporter feedback to better understand the request. type:bug Something isn't working.
Projects
Status: Incoming
Development

No branches or pull requests

3 participants